Отдельный 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.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; 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 namespace AsbCloudInfrastructure.Services.DailyReport
{ {
@ -45,14 +45,14 @@ namespace AsbCloudInfrastructure.Services.DailyReport
if (begin is not null) if (begin is not null)
{ {
var beginUTC = ExtractDate(begin.Value); 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); query = query.Where(d => d.StartDate >= beginDateOnly);
} }
if (end is not null) if (end is not null)
{ {
var endUTC = ExtractDate(end.Value); 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); query = query.Where(d => d.StartDate <= endDateOnly);
} }
@ -76,12 +76,12 @@ namespace AsbCloudInfrastructure.Services.DailyReport
if (well is null || well.Timezone is null) if (well is null || well.Timezone is null)
throw new ArgumentInvalidException("idWell doesn`t exist", nameof(idWell)); 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 var entity = await db.DailyReports
.FirstOrDefaultAsync(r => r.IdWell == idWell && r.StartDate == startDateOnly, token); .FirstOrDefaultAsync(r => r.IdWell == idWell && r.StartDate == startDateOnly, token);
if (entity is not null) 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 customer = well.Companies.FirstOrDefault(company => company.IdCompanyType == 1);
var contractor = well.Companies.FirstOrDefault(company => company.IdCompanyType == 2); var contractor = well.Companies.FirstOrDefault(company => company.IdCompanyType == 2);
@ -90,7 +90,8 @@ namespace AsbCloudInfrastructure.Services.DailyReport
{ {
IdWell = idWell, IdWell = idWell,
StartDate = startDateOnly, StartDate = startDateOnly,
Info = new DailyReportInfo() { Info = new DailyReportInfo()
{
Head = new Head() Head = new Head()
{ {
ReportDate = startDate.Date, ReportDate = startDate.Date,
@ -126,12 +127,8 @@ namespace AsbCloudInfrastructure.Services.DailyReport
public async Task<int> UpdateBlockAsync<Tdto>(int idWell, DateTime date, Tdto dto, CancellationToken token) public async Task<int> UpdateBlockAsync<Tdto>(int idWell, DateTime date, Tdto dto, CancellationToken token)
{ {
var dateOffset = date.Date; var startDateOnly = date.Date.ExtractDateOnly();
var entity = await db.DailyReports var entity = await db.DailyReports.FirstOrDefaultAsync(r => r.IdWell == idWell && r.StartDate == startDateOnly, token);
.FirstOrDefaultAsync(r => r.IdWell == idWell &&
r.StartDate.Year == dateOffset.Year &&
r.StartDate.DayOfYear == dateOffset.DayOfYear
, token);
if (entity is null) if (entity is null)
return 0; return 0;

View File

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