diff --git a/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs b/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs index 392b3f83..2a093f5b 100644 --- a/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs +++ b/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs @@ -76,19 +76,26 @@ 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 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)); + var customer = well.Companies.FirstOrDefault(company => company.IdCompanyType == 1); var contractor = well.Companies.FirstOrDefault(company => company.IdCompanyType == 2); - var entity = new AsbCloudDb.Model.DailyReport.DailyReport + entity = new AsbCloudDb.Model.DailyReport.DailyReport { IdWell = idWell, - StartDate = new DateOnly(startDate.Date.Year, startDate.Date.Month, startDate.Date.Day), + StartDate = startDateOnly, Info = new DailyReportInfo() { Head = new Head() { ReportDate = startDate.Date, WellName = well.Caption, - ClusterName = well.Cluster, + ClusterName = well?.Cluster ?? String.Empty, Customer = customer?.Caption ?? String.Empty, Contractor = contractor?.Caption ?? String.Empty, } diff --git a/AsbCloudWebApi/Controllers/DailyReportController.cs b/AsbCloudWebApi/Controllers/DailyReportController.cs index 1664832e..dde14461 100644 --- a/AsbCloudWebApi/Controllers/DailyReportController.cs +++ b/AsbCloudWebApi/Controllers/DailyReportController.cs @@ -1,10 +1,14 @@ 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; @@ -72,8 +76,17 @@ namespace AsbCloudWebApi.Controllers [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task AddAsync(int idWell, [Required] DateTime startDate, CancellationToken token = default) { - var result = await dailyReportService.AddAsync(idWell, startDate, token); - return Ok(result); + try + { + var result = await dailyReportService.AddAsync(idWell, startDate, token); + return Ok(result); + } + catch (ArgumentInvalidException ex) + { + return BadRequest(ex.Message); + } + + }