Формат даты в WellOperationDto - DateTimeOffset

This commit is contained in:
Olga Nemt 2023-12-29 11:24:46 +05:00
parent f9a6279d0d
commit a2f87591e8
4 changed files with 30 additions and 12 deletions

View File

@ -1,5 +1,5 @@
using AsbCloudApp.Validation;
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace AsbCloudApp.Data
@ -7,7 +7,7 @@ namespace AsbCloudApp.Data
/// <summary>
/// Операции на скважине (заведенные пользователем)
/// </summary>
public class WellOperationDto : ItemInfoDto, IId, IWellRelated
public class WellOperationDto : ItemInfoDto, IId, IWellRelated, IValidatableObject
{
/// <inheritdoc/>
public int Id { get; set; }
@ -86,8 +86,8 @@ namespace AsbCloudApp.Data
/// <summary>
/// Дата начала операции
/// </summary>
[DateValidation(GtDate = "2010-01-01T00:00:00")]
public DateTime DateStart { get; set; }
public DateTimeOffset DateStart { get; set; }
/// <summary>
/// Продолжительность, часы
@ -100,5 +100,24 @@ namespace AsbCloudApp.Data
/// </summary>
[StringLength(4096, ErrorMessage = "Комментарий не может быть длиннее 4096 символов")]
public string? Comment { get; set; }
/// <summary>
/// Валидация даты
/// </summary>
/// <param name="validationContext"></param>
/// <returns></returns>
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (!(DateStart is DateTimeOffset dateTimeOffset))
yield return new ValidationResult(
$"{nameof(DateStart)}: дата DateStart указана не в формате DateTimeOffset",
new[] { nameof(DateStart) });
var gtDate = new DateTimeOffset(2010, 1, 1, 0, 0, 0, TimeSpan.Zero);
if (DateStart <= gtDate)
yield return new ValidationResult(
$"{nameof(DateStart)}: DateStart не может быть меньше {gtDate}",
new[] { nameof(DateStart) });
}
}
}

View File

@ -347,7 +347,7 @@ public class WellOperationRepository : IWellOperationRepository
{
var entity = dto.Adapt<WellOperation>();
entity.Id = default;
entity.DateStart = dto.DateStart.ToUtcDateTimeOffset(timezone.Hours);
entity.DateStart = dto.DateStart.ToUniversalTime();
entity.IdWell = idWell;
db.WellOperations.Add(entity);
}
@ -365,7 +365,7 @@ public class WellOperationRepository : IWellOperationRepository
{
var timezone = wellService.GetTimezone(dto.IdWell);
var entity = dto.Adapt<WellOperation>();
entity.DateStart = dto.DateStart.ToUtcDateTimeOffset(timezone.Hours);
entity.DateStart = dto.DateStart.ToUniversalTime();
db.WellOperations.Update(entity);
var result = await db.SaveChangesAsync(token);
@ -449,7 +449,6 @@ public class WellOperationRepository : IWellOperationRepository
CategoryName = o.OperationCategory.Name,
WellSectionTypeName = o.WellSectionType.Caption,
DateStart = DateTime.SpecifyKind(o.DateStart.UtcDateTime + timeZoneOffset, DateTimeKind.Unspecified),
DepthStart = o.DepthStart,
DepthEnd = o.DepthEnd,

View File

@ -140,7 +140,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
.Select(t => t.Fact!)
.ToList();
DateTime lastFactDate = default;
DateTimeOffset lastFactDate = default;
var lastFactI = 0;
int i = 0;
@ -290,7 +290,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
static DateTime GetEndDate(WellOperationDto operation)
=> operation is not null
? operation.DateStart.AddHours(operation.DurationHours)
? operation.DateStart.Date.AddHours(operation.DurationHours)
: default;
var endDatePlan = planLast is not null ? GetEndDate(planLast) : default;

View File

@ -238,8 +238,8 @@ public class DailyReportServiceTest
fakeDatesRange = new DatesRangeDto
{
From = fakeFirstFactWellOperation.DateStart,
To = fakeLastFactWellOperation.DateStart
From = fakeFirstFactWellOperation.DateStart.Date,
To = fakeLastFactWellOperation.DateStart.Date
};
dailyReportService = new DailyReportService(wellServiceMock,