Отдельный extension-метод для дат - ExtractDateOnly

This commit is contained in:
Olga Nemt 2023-03-09 11:38:43 +05:00
parent 4545325a93
commit 29e34445be
3 changed files with 39 additions and 33 deletions

View File

@ -0,0 +1,15 @@
namespace System
{
public static class Extentions
{
public static DateOnly ExtractDateOnly(this DateTime date)
{
return new DateOnly(date.Year, date.Month, date.Day);
}
public static DateOnly ExtractDateOnly(this DateTimeOffset date)
{
return new DateOnly(date.Date.Year, date.Date.Month, date.Date.Day);
}
}
}

View File

@ -1,16 +1,16 @@
using System;
using AsbCloudApp.Data.DailyReport;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudDb.Model.DailyReport;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Mapster;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using System.Collections.Generic;
using AsbCloudApp.Data.DailyReport;
using AsbCloudApp.Exceptions;
using AsbCloudDb.Model.DailyReport;
namespace AsbCloudInfrastructure.Services.DailyReport
{
@ -38,21 +38,21 @@ namespace AsbCloudInfrastructure.Services.DailyReport
DateTimeOffset ExtractDate(DateTime dateTime)
{
var dateTimeOffset = dateTime.ToUtcDateTimeOffset(well!.Timezone.Hours);
var date = new DateTimeOffset(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day, 0,0,0,TimeSpan.Zero);
var date = new DateTimeOffset(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day, 0, 0, 0, TimeSpan.Zero);
return date;
}
if (begin is not null)
{
var beginUTC = ExtractDate(begin.Value);
var beginDateOnly = new DateOnly(beginUTC.Date.Year, beginUTC.Date.Month, beginUTC.Date.Day);
var beginDateOnly = beginUTC.ExtractDateOnly();
query = query.Where(d => d.StartDate >= beginDateOnly);
}
if (end is not null)
{
var endUTC = ExtractDate(end.Value);
var endDateOnly = new DateOnly(endUTC.Date.Year, endUTC.Date.Month, endUTC.Date.Day);
var endDateOnly = endUTC.ExtractDateOnly();
query = query.Where(d => d.StartDate <= endDateOnly);
}
@ -66,7 +66,7 @@ namespace AsbCloudInfrastructure.Services.DailyReport
{
var dateOnly = DateTime.SpecifyKind(date.Date, DateTimeKind.Utc);
var dailyReportDto = await GetOrDefaultAsync(idWell, dateOnly, token);
dailyReportDto ??= await MakeDefaultDailyReportAsync(idWell, dateOnly, token);
dailyReportDto ??= await MakeDefaultDailyReportAsync(idWell, dateOnly, token);
return dailyReportDto;
}
@ -76,12 +76,12 @@ namespace AsbCloudInfrastructure.Services.DailyReport
if (well is null || well.Timezone is null)
throw new ArgumentInvalidException("idWell doesn`t exist", nameof(idWell));
var startDateOnly = new DateOnly(startDate.Date.Year, startDate.Date.Month, startDate.Date.Day);
var startDateOnly = startDate.ExtractDateOnly();
var entity = await db.DailyReports
.FirstOrDefaultAsync(r => r.IdWell == idWell && r.StartDate == startDateOnly, token);
if (entity is not null)
throw new ArgumentInvalidException("daily report already exists", nameof(idWell));
throw new Exception($"daily report on {startDateOnly} already exists");
var customer = well.Companies.FirstOrDefault(company => company.IdCompanyType == 1);
var contractor = well.Companies.FirstOrDefault(company => company.IdCompanyType == 2);
@ -90,9 +90,10 @@ namespace AsbCloudInfrastructure.Services.DailyReport
{
IdWell = idWell,
StartDate = startDateOnly,
Info = new DailyReportInfo() {
Info = new DailyReportInfo()
{
Head = new Head()
{
{
ReportDate = startDate.Date,
WellName = well.Caption,
ClusterName = well?.Cluster ?? String.Empty,
@ -118,7 +119,7 @@ namespace AsbCloudInfrastructure.Services.DailyReport
if (entity is null)
return 0;
entity.Info = Convert(dto);
entity.Info = Convert(dto);
db.DailyReports.Update(entity);
var result = await db.SaveChangesAsync(token);
return result;
@ -126,12 +127,8 @@ namespace AsbCloudInfrastructure.Services.DailyReport
public async Task<int> UpdateBlockAsync<Tdto>(int idWell, DateTime date, Tdto dto, CancellationToken token)
{
var dateOffset = date.Date;
var entity = await db.DailyReports
.FirstOrDefaultAsync(r => r.IdWell == idWell &&
r.StartDate.Year == dateOffset.Year &&
r.StartDate.DayOfYear == dateOffset.DayOfYear
, token);
var startDateOnly = date.Date.ExtractDateOnly();
var entity = await db.DailyReports.FirstOrDefaultAsync(r => r.IdWell == idWell && r.StartDate == startDateOnly, token);
if (entity is null)
return 0;
@ -182,7 +179,7 @@ namespace AsbCloudInfrastructure.Services.DailyReport
private async Task<DailyReportDto> MakeDefaultDailyReportAsync(int idWell, DateTime date, CancellationToken token)
{
var well = await wellService.GetOrDefaultAsync(idWell, token);
var dto = new DailyReportDto()
{
Head = new HeadDto()
@ -213,7 +210,7 @@ namespace AsbCloudInfrastructure.Services.DailyReport
}
private static DailyReportDto Convert(AsbCloudDb.Model.DailyReport.DailyReport entity)
{
{
var dto = entity.Info.Adapt<DailyReportDto>();
dto.StartDate = entity.StartDate;
return dto;

View File

@ -1,14 +1,10 @@
using AsbCloudApp.Data.DailyReport;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Trajectory;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
@ -81,12 +77,10 @@ namespace AsbCloudWebApi.Controllers
var result = await dailyReportService.AddAsync(idWell, startDate, token);
return Ok(result);
}
catch (ArgumentInvalidException ex)
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}