From 29e34445be71b6387f93d5df255aac8ff8df7146 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 9 Mar 2023 11:38:43 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=B9=20extension-=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=B4=D0=B0=D1=82=20-=20ExtractDateOnly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudInfrastructure/Extensions.cs | 15 ++++++ .../DailyReport/DailyReportService.cs | 49 +++++++++---------- .../Controllers/DailyReportController.cs | 8 +-- 3 files changed, 39 insertions(+), 33 deletions(-) create mode 100644 AsbCloudInfrastructure/Extensions.cs diff --git a/AsbCloudInfrastructure/Extensions.cs b/AsbCloudInfrastructure/Extensions.cs new file mode 100644 index 00000000..acd67990 --- /dev/null +++ b/AsbCloudInfrastructure/Extensions.cs @@ -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); + } + } +} diff --git a/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs b/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs index 2a093f5b..c1587c45 100644 --- a/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs +++ b/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs @@ -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 UpdateBlockAsync(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 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(); dto.StartDate = entity.StartDate; return dto; diff --git a/AsbCloudWebApi/Controllers/DailyReportController.cs b/AsbCloudWebApi/Controllers/DailyReportController.cs index dde14461..ae6868a1 100644 --- a/AsbCloudWebApi/Controllers/DailyReportController.cs +++ b/AsbCloudWebApi/Controllers/DailyReportController.cs @@ -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); } - - }