forked from ddrilling/AsbCloudServer
Add DateOnly, TimeOnly support
This commit is contained in:
parent
c568fafa8f
commit
87391ad9b1
@ -25,12 +25,12 @@ namespace AsbCloudApp.Data
|
||||
/// <summary>
|
||||
/// Начало смены
|
||||
/// </summary>
|
||||
public TimeOnly ShiftStart { get; set; }
|
||||
public TimeDto ShiftStart { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Конец смены
|
||||
/// </summary>
|
||||
public TimeOnly ShiftEnd { get; set; }
|
||||
public TimeDto ShiftEnd { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Начало бурения
|
||||
|
79
AsbCloudApp/Data/TimeDto.cs
Normal file
79
AsbCloudApp/Data/TimeDto.cs
Normal file
@ -0,0 +1,79 @@
|
||||
using System;
|
||||
|
||||
namespace AsbCloudApp.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// DTO времени
|
||||
/// </summary>
|
||||
public class TimeDto
|
||||
{
|
||||
private int hour = 0;
|
||||
private int minute = 0;
|
||||
private int second = 0;
|
||||
|
||||
/// <summary>
|
||||
/// час
|
||||
/// </summary>
|
||||
public int Hour {
|
||||
get => hour;
|
||||
set {
|
||||
if (value > 23 || value < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(Hour), "hour should be in [0; 23]");
|
||||
hour = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// минута
|
||||
/// </summary>
|
||||
public int Minute
|
||||
{
|
||||
get => minute;
|
||||
set
|
||||
{
|
||||
if (value > 59 || value < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(minute), "minute should be in [0; 59]");
|
||||
minute = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// секунда
|
||||
/// </summary>
|
||||
public int Second
|
||||
{
|
||||
get => second;
|
||||
set
|
||||
{
|
||||
if (value > 59 || value < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(second), "second should be in [0; 59]");
|
||||
second = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public TimeDto()
|
||||
{ }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public TimeDto(TimeOnly time)
|
||||
{
|
||||
hour = time.Hour;
|
||||
minute = time.Minute;
|
||||
second = time.Second;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes System.TimeOnly
|
||||
/// </summary>
|
||||
/// <returns>System.TimeOnly</returns>
|
||||
public TimeOnly MakeTimeOnly() => new(Hour, Minute, Second);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string ToString()
|
||||
{
|
||||
var str = $"{Hour:00}:{Minute:00}:{Second:00}";
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
@ -40,6 +40,13 @@ namespace AsbCloudInfrastructure
|
||||
.ForType<DateTime, DateTimeOffset>()
|
||||
.MapWith((source) => source == default ? new DateTime(0, DateTimeKind.Utc) : source);
|
||||
|
||||
TypeAdapterConfig.GlobalSettings.Default.Config
|
||||
.ForType<TimeDto, TimeOnly>()
|
||||
.MapWith((source) => source == default ? default : source.MakeTimeOnly());
|
||||
|
||||
TypeAdapterConfig.GlobalSettings.Default.Config
|
||||
.ForType<TimeOnly, TimeDto>()
|
||||
.MapWith((source) => new(source));
|
||||
}
|
||||
|
||||
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
|
||||
|
@ -22,6 +22,7 @@ namespace AsbCloudInfrastructure.Validators
|
||||
// services.AddTransient<IValidator<UserRoleDto>, UserRoleDtoValidator>();
|
||||
// services.AddTransient<IValidator<WellDto>, WellDtoValidator>();
|
||||
// services.AddTransient<IValidator<WellOperationDto>, WellOperationDtoValidator>();
|
||||
// TimeDtoValidator
|
||||
|
||||
return services;
|
||||
}
|
||||
|
23
AsbCloudInfrastructure/Validators/TimeDtoValidator.cs
Normal file
23
AsbCloudInfrastructure/Validators/TimeDtoValidator.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using AsbCloudApp.Data;
|
||||
using FluentValidation;
|
||||
|
||||
namespace AsbCloudInfrastructure.Validators
|
||||
{
|
||||
public class TimeDtoValidator : AbstractValidator<TimeDto>
|
||||
{
|
||||
public TimeDtoValidator()
|
||||
{
|
||||
RuleFor(x=>x.Hour)
|
||||
.InclusiveBetween(0,23)
|
||||
.WithMessage("hour should be in [0; 23]");
|
||||
|
||||
RuleFor(x => x.Minute)
|
||||
.InclusiveBetween(0, 59)
|
||||
.WithMessage("minute should be in [0; 59]");
|
||||
|
||||
RuleFor(x => x.Second)
|
||||
.InclusiveBetween(0, 59)
|
||||
.WithMessage("second should be in [0; 59]");
|
||||
}
|
||||
}
|
||||
}
|
@ -8,12 +8,12 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="6.0.1" />
|
||||
<PackageReference Include="protobuf-net" Version="3.0.101" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.5" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="6.0.5" />
|
||||
<PackageReference Include="protobuf-net" Version="3.1.4" />
|
||||
<PackageReference Include="protobuf-net.AspNetCore" Version="3.0.101" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.2.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
46
AsbCloudWebApi/Converters/TimeOnlyTypeConverter.cs
Normal file
46
AsbCloudWebApi/Converters/TimeOnlyTypeConverter.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
|
||||
namespace AsbCloudWebApi.Converters
|
||||
{
|
||||
#nullable enable
|
||||
public class DateOnlyTypeConverter : TypeConverter
|
||||
{
|
||||
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
|
||||
{
|
||||
if (sourceType == typeof(string))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return base.CanConvertFrom(context, sourceType);
|
||||
}
|
||||
|
||||
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
|
||||
{
|
||||
if (value is string str)
|
||||
{
|
||||
return TimeOnly.Parse(str);
|
||||
}
|
||||
return base.ConvertFrom(context, culture, value);
|
||||
}
|
||||
|
||||
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
|
||||
{
|
||||
if (destinationType == typeof(string))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return base.CanConvertTo(context, destinationType);
|
||||
}
|
||||
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
|
||||
{
|
||||
if (destinationType == typeof(string) && value is TimeOnly date)
|
||||
{
|
||||
return date.ToString("O");
|
||||
}
|
||||
return base.ConvertTo(context, culture, value, destinationType);
|
||||
}
|
||||
}
|
||||
#nullable disable
|
||||
}
|
@ -20,16 +20,15 @@ namespace AsbCloudWebApi
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddControllers()
|
||||
.AddJsonOptions(new System.Action<Microsoft.AspNetCore.Mvc.JsonOptions>(opts =>
|
||||
.AddJsonOptions(new System.Action<Microsoft.AspNetCore.Mvc.JsonOptions>(options =>
|
||||
{
|
||||
opts.JsonSerializerOptions.NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals | System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString;
|
||||
}))
|
||||
.AddProtoBufNet(); //adds mediaType "application/protobuf"
|
||||
options.JsonSerializerOptions.NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals | System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString; }))
|
||||
.AddProtoBufNet();
|
||||
|
||||
ProtobufModel.EnshureRegistered();
|
||||
|
||||
services.AddSwagger();
|
||||
|
||||
|
||||
services.AddInfrastructure(Configuration);
|
||||
|
||||
services.AddJWTAuthentication();
|
||||
|
@ -1,5 +1,7 @@
|
||||
using AsbCloudDb.Model;
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudDb.Model;
|
||||
using AsbCloudInfrastructure.Services.DailyReport;
|
||||
using Mapster;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Linq;
|
||||
@ -23,6 +25,27 @@ namespace ConsoleApp1
|
||||
{
|
||||
static void Main(/*string[] args*/)
|
||||
{
|
||||
TypeAdapterConfig.GlobalSettings.Default.Config
|
||||
.ForType<DateTimeOffset, DateTime>()
|
||||
.MapWith((source) => source.DateTime);
|
||||
|
||||
TypeAdapterConfig.GlobalSettings.Default.Config
|
||||
.ForType<DateTime, DateTimeOffset>()
|
||||
.MapWith((source) => source == default ? new DateTime(0, DateTimeKind.Utc) : source);
|
||||
|
||||
TypeAdapterConfig.GlobalSettings.Default.Config
|
||||
.ForType<TimeDto, TimeOnly>()
|
||||
.MapWith((source) => source == default? default: source.MakeTimeOnly());
|
||||
|
||||
TypeAdapterConfig.GlobalSettings.Default.Config
|
||||
.ForType<TimeOnly, TimeDto>()
|
||||
.MapWith((source) => new (source));
|
||||
|
||||
var sh = new ScheduleDto{
|
||||
ShiftStart = new TimeDto { Hour = 11, Minute = 30, }
|
||||
};
|
||||
var en = sh.Adapt<Schedule>();
|
||||
var aa = en.Adapt<ScheduleDto>();
|
||||
// use ServiceFactory to make services
|
||||
var op = ServiceFactory.MakeWellOperationsService();
|
||||
var d = op.FirstOperationDate(90);
|
||||
|
Loading…
Reference in New Issue
Block a user