From cf98bd8a208ff36f0f5018dfbebec93c8a6c0880 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Thu, 16 Jun 2022 12:33:05 +0500 Subject: [PATCH] Crud services moved to Repository directory --- AsbCloudApp/Services/ICrudService.cs | 4 +- AsbCloudApp/Services/ISetpointsService.cs | 2 +- AsbCloudInfrastructure/DependencyInjection.cs | 1 + .../CrudCacheServiceBase.cs | 24 ++++----- .../CrudServiceBase.cs | 12 ++--- .../CrudWellRelatedCacheServiceBase.cs | 51 +++++++++++++++++++ .../CrudWellRelatedServiceBase.cs | 4 +- .../Repository/SetpointsRequestRepository.cs | 36 +++++++++++++ AsbCloudInfrastructure/Repository/readme.md | 9 ++++ .../Services/AuthService.cs | 2 +- .../DailyReport/DailyReportService.cs | 2 +- .../DetectedOperationService.cs | 4 +- .../Services/DrillFlowChartService.cs | 1 + .../Services/DrillParamsService.cs | 1 + .../Services/DrillerService.cs | 1 + .../DrillingProgram/DrillingProgramService.cs | 12 ++--- .../Services/OperationValueService.cs | 1 + .../Services/SAUB/SetpointsService.cs | 32 ++++++++---- .../Services/ScheduleService.cs | 1 + .../Services/UserRoleService.cs | 4 +- .../Services/UserService.cs | 4 +- .../ScheduleReportService.cs | 2 +- .../Services/WellService.cs | 13 ++--- .../ServicesTests/CrudServiceTestAbstract.cs | 2 +- .../DrillingProgramServiceTest.cs | 8 +-- .../Controllers/AdminUserRoleController.cs | 2 +- AsbCloudWebApi/Controllers/CrudController.cs | 2 +- .../Controllers/CrudWellRelatedController.cs | 2 +- .../Controllers/DailyReportController.cs | 2 +- .../Controllers/SAUB/SetpointsController.cs | 2 +- AsbCloudWebApi/Controllers/WellController.cs | 2 +- AsbCloudWebApi/wwwroot/asset-manifest.json | 12 ++--- AsbCloudWebApi/wwwroot/index.html | 2 +- 33 files changed, 185 insertions(+), 74 deletions(-) rename AsbCloudInfrastructure/{Services => Repository}/CrudCacheServiceBase.cs (81%) rename AsbCloudInfrastructure/{Services => Repository}/CrudServiceBase.cs (95%) create mode 100644 AsbCloudInfrastructure/Repository/CrudWellRelatedCacheServiceBase.cs rename AsbCloudInfrastructure/{Services => Repository}/CrudWellRelatedServiceBase.cs (92%) create mode 100644 AsbCloudInfrastructure/Repository/SetpointsRequestRepository.cs create mode 100644 AsbCloudInfrastructure/Repository/readme.md diff --git a/AsbCloudApp/Services/ICrudService.cs b/AsbCloudApp/Services/ICrudService.cs index 8114fbaa..d9a2ad37 100644 --- a/AsbCloudApp/Services/ICrudService.cs +++ b/AsbCloudApp/Services/ICrudService.cs @@ -31,14 +31,14 @@ namespace AsbCloudApp.Services /// /// /// null if not found - Task GetAsync(int id, CancellationToken token); + Task GetOrDefaultAsync(int id, CancellationToken token); /// /// Получить запись по id /// /// /// null if not found - TDto? Get(int id); + TDto? GetOrDefault(int id); /// /// Добавление новой записи diff --git a/AsbCloudApp/Services/ISetpointsService.cs b/AsbCloudApp/Services/ISetpointsService.cs index e11b5642..3b608683 100644 --- a/AsbCloudApp/Services/ISetpointsService.cs +++ b/AsbCloudApp/Services/ISetpointsService.cs @@ -11,7 +11,7 @@ namespace AsbCloudApp.Services Task> GetAsync(int idWell, CancellationToken token); Task> GetForPanelAsync(string uid, CancellationToken token); Task TryDelete(int id, CancellationToken token); - Task UpdateStateAsync(int id, SetpointsRequestDto setpointsRequestDto, CancellationToken token); + Task UpdateStateAsync(SetpointsRequestDto setpointsRequestDto, CancellationToken token); IEnumerable GetSetpointsNames(); } } diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 49602160..c4cab363 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -2,6 +2,7 @@ using AsbCloudApp.Data.SAUB; using AsbCloudApp.Services; using AsbCloudDb.Model; +using AsbCloudInfrastructure.Repository; using AsbCloudInfrastructure.Services; using AsbCloudInfrastructure.Services.Cache; using AsbCloudInfrastructure.Services.DailyReport; diff --git a/AsbCloudInfrastructure/Services/CrudCacheServiceBase.cs b/AsbCloudInfrastructure/Repository/CrudCacheServiceBase.cs similarity index 81% rename from AsbCloudInfrastructure/Services/CrudCacheServiceBase.cs rename to AsbCloudInfrastructure/Repository/CrudCacheServiceBase.cs index 3da52cd0..142367ec 100644 --- a/AsbCloudInfrastructure/Services/CrudCacheServiceBase.cs +++ b/AsbCloudInfrastructure/Repository/CrudCacheServiceBase.cs @@ -7,9 +7,8 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -namespace AsbCloudInfrastructure.Services +namespace AsbCloudInfrastructure.Repository { -#nullable enable /// /// CRUD /// @@ -17,7 +16,7 @@ namespace AsbCloudInfrastructure.Services /// public class CrudCacheServiceBase : CrudServiceBase where TDto : AsbCloudApp.Data.IId - where TEntity : class, AsbCloudDb.Model.IId + where TEntity : class, IId { protected string CacheTag = typeof(TDto).Name; protected TimeSpan CacheOlescence = TimeSpan.FromMinutes(5); @@ -53,9 +52,8 @@ namespace AsbCloudInfrastructure.Services /// public override async Task> GetAllAsync(CancellationToken token) { - var result = await GetQuery() - .FromCacheDictionaryAsync(CacheTag, CacheOlescence, KeySelector, Convert, token); - return result.Values; + var cache = await GetCacheAsync(token); + return cache.Values; } /// @@ -63,19 +61,17 @@ namespace AsbCloudInfrastructure.Services /// /// /// - public override TDto? Get(int id) + public override TDto? GetOrDefault(int id) { - var result = GetQuery() - .FromCacheDictionary(CacheTag, CacheOlescence, KeySelector, Convert); - return result.GetValueOrDefault(id); + var cache = GetCache(); + return cache.GetValueOrDefault(id); } /// - public override async Task GetAsync(int id, CancellationToken token) + public override async Task GetOrDefaultAsync(int id, CancellationToken token) { - var result = await GetQuery() - .FromCacheDictionaryAsync(CacheTag, CacheOlescence, KeySelector, Convert, token); - return result.GetValueOrDefault(id); + var cache = await GetCacheAsync(token); + return cache.GetValueOrDefault(id); } /// diff --git a/AsbCloudInfrastructure/Services/CrudServiceBase.cs b/AsbCloudInfrastructure/Repository/CrudServiceBase.cs similarity index 95% rename from AsbCloudInfrastructure/Services/CrudServiceBase.cs rename to AsbCloudInfrastructure/Repository/CrudServiceBase.cs index 552afc61..3163d86e 100644 --- a/AsbCloudInfrastructure/Services/CrudServiceBase.cs +++ b/AsbCloudInfrastructure/Repository/CrudServiceBase.cs @@ -8,7 +8,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -namespace AsbCloudInfrastructure.Services +namespace AsbCloudInfrastructure.Repository { /// @@ -18,7 +18,7 @@ namespace AsbCloudInfrastructure.Services /// public class CrudServiceBase : ICrudService where TDto : AsbCloudApp.Data.IId - where TEntity : class, AsbCloudDb.Model.IId + where TEntity : class, IId { protected readonly IAsbCloudDbContext dbContext; protected readonly DbSet dbSet; @@ -26,7 +26,7 @@ namespace AsbCloudInfrastructure.Services public CrudServiceBase(IAsbCloudDbContext context) { - this.dbContext = context; + dbContext = context; dbSet = context.Set(); GetQuery = () => dbSet; } @@ -47,7 +47,7 @@ namespace AsbCloudInfrastructure.Services public CrudServiceBase(IAsbCloudDbContext context, Func, IQueryable> makeQuery) { - this.dbContext = context; + dbContext = context; dbSet = context.Set(); GetQuery = () => makeQuery(dbSet); } @@ -65,7 +65,7 @@ namespace AsbCloudInfrastructure.Services } /// - public virtual async Task GetAsync(int id, CancellationToken token = default) + public virtual async Task GetOrDefaultAsync(int id, CancellationToken token = default) { var entity = await GetQuery() .AsNoTracking() @@ -78,7 +78,7 @@ namespace AsbCloudInfrastructure.Services } /// - public virtual TDto? Get(int id) + public virtual TDto? GetOrDefault(int id) { var entity = GetQuery() .AsNoTracking() diff --git a/AsbCloudInfrastructure/Repository/CrudWellRelatedCacheServiceBase.cs b/AsbCloudInfrastructure/Repository/CrudWellRelatedCacheServiceBase.cs new file mode 100644 index 00000000..fddcb8aa --- /dev/null +++ b/AsbCloudInfrastructure/Repository/CrudWellRelatedCacheServiceBase.cs @@ -0,0 +1,51 @@ +using AsbCloudApp.Services; +using AsbCloudDb.Model; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudInfrastructure.Repository +{ +#nullable enable + public class CrudWellRelatedCacheServiceBase : CrudCacheServiceBase, ICrudWellRelatedService + where TDto : AsbCloudApp.Data.IId, AsbCloudApp.Data.IWellRelated + where TEntity : class, IId, IWellRelated + { + public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext context) + : base(context) { } + + public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext dbContext, ISet includes) + : base(dbContext, includes) { } + + public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext context, Func, IQueryable> makeQuery) + : base(context, makeQuery) { } + + public async Task> GetByIdWellAsync(int idWell, CancellationToken token) + { + var cache = await GetCacheAsync(token); + + var dtos = cache.Values + .Where(e => e.IdWell == idWell) + .ToList(); + + return dtos; + } + + public async Task> GetByIdWellAsync(IEnumerable idsWells, CancellationToken token) + { + if (!idsWells.Any()) + return Enumerable.Empty(); + + var cache = await GetCacheAsync(token); + + var dtos = cache.Values + .Where(e => idsWells.Contains(e.IdWell)) + .ToList(); + return dtos; + } + } +#nullable disable +} \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/CrudWellRelatedServiceBase.cs b/AsbCloudInfrastructure/Repository/CrudWellRelatedServiceBase.cs similarity index 92% rename from AsbCloudInfrastructure/Services/CrudWellRelatedServiceBase.cs rename to AsbCloudInfrastructure/Repository/CrudWellRelatedServiceBase.cs index f3899c12..eb65cd9d 100644 --- a/AsbCloudInfrastructure/Services/CrudWellRelatedServiceBase.cs +++ b/AsbCloudInfrastructure/Repository/CrudWellRelatedServiceBase.cs @@ -7,12 +7,12 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -namespace AsbCloudInfrastructure.Services +namespace AsbCloudInfrastructure.Repository { #nullable enable public class CrudWellRelatedServiceBase : CrudServiceBase, ICrudWellRelatedService where TDto : AsbCloudApp.Data.IId, AsbCloudApp.Data.IWellRelated - where TEntity : class, AsbCloudDb.Model.IId, AsbCloudDb.Model.IWellRelated + where TEntity : class, IId, IWellRelated { public CrudWellRelatedServiceBase(IAsbCloudDbContext context) : base(context) { } diff --git a/AsbCloudInfrastructure/Repository/SetpointsRequestRepository.cs b/AsbCloudInfrastructure/Repository/SetpointsRequestRepository.cs new file mode 100644 index 00000000..3519a8e0 --- /dev/null +++ b/AsbCloudInfrastructure/Repository/SetpointsRequestRepository.cs @@ -0,0 +1,36 @@ +using AsbCloudApp.Data.SAUB; +using AsbCloudApp.Services; +using AsbCloudDb.Model; +using Microsoft.EntityFrameworkCore; +using System; + +namespace AsbCloudInfrastructure.Repository +{ + public class SetpointsRequestRepository : CrudWellRelatedCacheServiceBase + { + private readonly IWellService wellService; + + public SetpointsRequestRepository(IAsbCloudDbContext dbContext, IWellService wellService) + : base(dbContext, q => q.Include(s => s.Author) + .Include(s => s.Well)) + { + this.wellService = wellService; + } + + protected override SetpointsRequestDto Convert(SetpointsRequest src) + { + var result = base.Convert(src); + var timezoneOffsetHours = wellService.GetTimezone(src.IdWell).Hours; + result.UploadDate = src.UploadDate.ToRemoteDateTime(timezoneOffsetHours); + return result; + } + + protected override SetpointsRequest Convert(SetpointsRequestDto src) + { + var result = base.Convert(src); + var timezoneOffsetHours = wellService.GetTimezone(src.IdWell).Hours; + result.UploadDate = src.UploadDate.ToUtcDateTimeOffset(timezoneOffsetHours); + return result; + } + } +} diff --git a/AsbCloudInfrastructure/Repository/readme.md b/AsbCloudInfrastructure/Repository/readme.md new file mode 100644 index 00000000..aea2634d --- /dev/null +++ b/AsbCloudInfrastructure/Repository/readme.md @@ -0,0 +1,9 @@ +# Repository +`Repository` - CRUD сервис для сущности в проекте. Не содержит бизнес логику. + +Вся логика такого сервиса - преобразование данных полученых из БД в Data Transfer Object (DTO) и обратно. +Преобразования осуществляются методами `Convert` с базовым маппингом: + + protected virtual TDto Convert(TEntity src) => src.Adapt(); + protected virtual TEntity Convert(TDto src) => src.Adapt(); + diff --git a/AsbCloudInfrastructure/Services/AuthService.cs b/AsbCloudInfrastructure/Services/AuthService.cs index ea91d774..d1946d86 100644 --- a/AsbCloudInfrastructure/Services/AuthService.cs +++ b/AsbCloudInfrastructure/Services/AuthService.cs @@ -51,7 +51,7 @@ namespace AsbCloudInfrastructure.Services if (identity == default || user.IdState == 0) return null; - var userDto = await userService.GetAsync(user.Id, token) + var userDto = await userService.GetOrDefaultAsync(user.Id, token) .ConfigureAwait(false); var userTokenDto = userDto.Adapt(); diff --git a/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs b/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs index 058fb103..221e5d3c 100644 --- a/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs +++ b/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs @@ -121,7 +121,7 @@ namespace AsbCloudInfrastructure.Services.DailyReport private async Task MakeDefaultDailyReportAsync(int idWell, DateTime date, CancellationToken token) { - var well = await wellService.GetAsync(idWell, token); + var well = await wellService.GetOrDefaultAsync(idWell, token); var offsetHours = wellService.GetTimezone(idWell).Hours; var dto = new DailyReportDto() { diff --git a/AsbCloudInfrastructure/Services/DetectOperations/DetectedOperationService.cs b/AsbCloudInfrastructure/Services/DetectOperations/DetectedOperationService.cs index f84127a3..af442ac8 100644 --- a/AsbCloudInfrastructure/Services/DetectOperations/DetectedOperationService.cs +++ b/AsbCloudInfrastructure/Services/DetectOperations/DetectedOperationService.cs @@ -35,7 +35,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations public async Task GetAsync(int idWell, DetectedOperationRequest request, CancellationToken token) { - var well = await wellService.GetAsync(idWell, token); + var well = await wellService.GetOrDefaultAsync(idWell, token); if (well?.IdTelemetry is null || well.Timezone is null) return null; @@ -80,7 +80,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations public async Task DeleteAsync(int idWell, DetectedOperationRequest request, CancellationToken token) { - var well = await wellService.GetAsync(idWell, token); + var well = await wellService.GetOrDefaultAsync(idWell, token); if (well?.IdTelemetry is null || well.Timezone is null) return 0; diff --git a/AsbCloudInfrastructure/Services/DrillFlowChartService.cs b/AsbCloudInfrastructure/Services/DrillFlowChartService.cs index 8d5a2f1d..33c6ef78 100644 --- a/AsbCloudInfrastructure/Services/DrillFlowChartService.cs +++ b/AsbCloudInfrastructure/Services/DrillFlowChartService.cs @@ -1,6 +1,7 @@ using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; +using AsbCloudInfrastructure.Repository; using Mapster; using Microsoft.EntityFrameworkCore; using System; diff --git a/AsbCloudInfrastructure/Services/DrillParamsService.cs b/AsbCloudInfrastructure/Services/DrillParamsService.cs index 1b7c9f31..1af54956 100644 --- a/AsbCloudInfrastructure/Services/DrillParamsService.cs +++ b/AsbCloudInfrastructure/Services/DrillParamsService.cs @@ -1,6 +1,7 @@ using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; +using AsbCloudInfrastructure.Repository; using Mapster; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; diff --git a/AsbCloudInfrastructure/Services/DrillerService.cs b/AsbCloudInfrastructure/Services/DrillerService.cs index a268eea0..d309f4af 100644 --- a/AsbCloudInfrastructure/Services/DrillerService.cs +++ b/AsbCloudInfrastructure/Services/DrillerService.cs @@ -1,6 +1,7 @@ using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; +using AsbCloudInfrastructure.Repository; namespace AsbCloudInfrastructure.Services { diff --git a/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs b/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs index 7c8d6fe1..e3c90574 100644 --- a/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs +++ b/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs @@ -224,7 +224,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram public async Task AddUserAsync(int idWell, int idFileCategory, int idUser, int idUserRole, CancellationToken token = default) { - var user = await userService.GetAsync(idUser, token); + var user = await userService.GetOrDefaultAsync(idUser, token); if (user is null) throw new ArgumentInvalidException($"User id == {idUser} does not exist", nameof(idUser)); @@ -356,7 +356,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram private async Task NotifyPublisherOnFullAccepAsync(FileMarkDto fileMark, CancellationToken token) { var file = await fileService.GetInfoAsync(fileMark.IdFile, token); - var well = await wellService.GetAsync(file.IdWell, token); + var well = await wellService.GetOrDefaultAsync(file.IdWell, token); var user = file.Author; var factory = new MailBodyFactory(configuration); var subject = MailBodyFactory.MakeSubject(well, "Загруженный вами документ полностью согласован"); @@ -368,7 +368,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram private async Task NotifyPublisherOnRejectAsync(FileMarkDto fileMark, CancellationToken token) { var file = await fileService.GetInfoAsync(fileMark.IdFile, token); - var well = await wellService.GetAsync(file.IdWell, token); + var well = await wellService.GetOrDefaultAsync(file.IdWell, token); var user = file.Author; var factory = new MailBodyFactory(configuration); var subject = MailBodyFactory.MakeSubject(well, "Загруженный вами документ отклонен"); @@ -379,7 +379,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram private async Task NotifyApproversAsync(DrillingProgramPart part, int idFile, string fileName, CancellationToken token) { - var well = await wellService.GetAsync(part.IdWell, token); + var well = await wellService.GetOrDefaultAsync(part.IdWell, token); var factory = new MailBodyFactory(configuration); var subject = MailBodyFactory.MakeSubject(well, "Загружен новый документ для согласования."); var users = part.RelatedUsers @@ -395,7 +395,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram private async Task NotifyNewPublisherAsync(int idWell, UserDto user, string documentCategory, CancellationToken token) { - var well = await wellService.GetAsync(idWell, token); + var well = await wellService.GetOrDefaultAsync(idWell, token); var factory = new MailBodyFactory(configuration); var subject = MailBodyFactory.MakeSubject(well, $"От вас ожидается загрузка на портал документа «{documentCategory}»"); var body = factory.MakeMailBodyForNewPublisher(well, user.Name, documentCategory); @@ -469,7 +469,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram var workId = MakeWorkId(idWell); if (!backgroundWorker.Contains(workId)) { - var well = await wellService.GetAsync(idWell, token); + var well = await wellService.GetOrDefaultAsync(idWell, token); var resultFileName = $"Программа бурения {well.Cluster} {well.Caption}.xlsx"; var tempResultFilePath = Path.Combine(Path.GetTempPath(), "drillingProgram", resultFileName); var mailService = new EmailService(backgroundWorker, configuration); diff --git a/AsbCloudInfrastructure/Services/OperationValueService.cs b/AsbCloudInfrastructure/Services/OperationValueService.cs index 1ca687fc..05d36f62 100644 --- a/AsbCloudInfrastructure/Services/OperationValueService.cs +++ b/AsbCloudInfrastructure/Services/OperationValueService.cs @@ -1,6 +1,7 @@ using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; +using AsbCloudInfrastructure.Repository; namespace AsbCloudInfrastructure.Services { diff --git a/AsbCloudInfrastructure/Services/SAUB/SetpointsService.cs b/AsbCloudInfrastructure/Services/SAUB/SetpointsService.cs index 832c523b..ce297d83 100644 --- a/AsbCloudInfrastructure/Services/SAUB/SetpointsService.cs +++ b/AsbCloudInfrastructure/Services/SAUB/SetpointsService.cs @@ -1,6 +1,7 @@ using AsbCloudApp.Data.SAUB; using AsbCloudApp.Services; using AsbCloudDb.Model; +using AsbCloudInfrastructure.Repository; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; @@ -26,14 +27,12 @@ namespace AsbCloudInfrastructure.Services.SAUB //{ "", new SetpointInfoDto { Name = "", DisplayName = "Обороты ВСП, об/мин" } }, // Оно в ПЛК спинмастера, пока сделать нельзя, позднее можно. //{ "", new SetpointInfoDto { Name = "", DisplayName = "Расход промывочной жидкости, л/с" } }, // Нет в контроллере }; - private readonly IAsbCloudDbContext db; + private readonly SetpointsRequestRepository setpointsRepository; private readonly ITelemetryService telemetryService; - private readonly CrudCacheServiceBase setpointsRepository; - public SetpointsService(IAsbCloudDbContext db, ITelemetryService telemetryService) + public SetpointsService(IAsbCloudDbContext db, ITelemetryService telemetryService, IWellService wellService) { - setpointsRepository = new CrudCacheServiceBase(db, q => q.Include(s => s.Author).Include(s => s.Well)); - this.db = db; + setpointsRepository = new SetpointsRequestRepository(db, wellService); this.telemetryService = telemetryService; } @@ -62,25 +61,38 @@ namespace AsbCloudInfrastructure.Services.SAUB var filtered = all.Where(s => s.IdWell == idWell && s.IdState == 1 && - s.UploadDate.AddSeconds(s.ObsolescenceSec) > DateTime.Now); + s.UploadDate.AddSeconds(s.ObsolescenceSec) > DateTime.UtcNow) + .ToList(); if (!filtered.Any()) return null; - foreach (var entity in filtered) - entity.IdState = 2; + foreach (var item in filtered) + { + item.IdState = 2; + item.UploadDate = DateTime.SpecifyKind(item.UploadDate, DateTimeKind.Utc); + } await setpointsRepository.UpdateRangeAsync(filtered, token); return filtered; } - public async Task UpdateStateAsync(int id, SetpointsRequestDto setpointsRequestDto, CancellationToken token) + public async Task UpdateStateAsync(SetpointsRequestDto setpointsRequestDto, CancellationToken token) { if (setpointsRequestDto.IdState != 3 && setpointsRequestDto.IdState != 4) throw new ArgumentOutOfRangeException(nameof(setpointsRequestDto), $"{nameof(setpointsRequestDto.IdState)} = {setpointsRequestDto.IdState}. Mast be 3 or 4."); - var entity = await setpointsRepository.GetAsync(id, token); + if (setpointsRequestDto.Id <= 0) + throw new ArgumentOutOfRangeException(nameof(setpointsRequestDto), $"{nameof(setpointsRequestDto.Id)} = {setpointsRequestDto.Id}. Mast be > 0"); + + if (setpointsRequestDto.IdWell <= 0) + throw new ArgumentOutOfRangeException(nameof(setpointsRequestDto), $"{nameof(setpointsRequestDto.IdWell)} = {setpointsRequestDto.IdWell}. Mast be > 0"); + + var entity = await setpointsRepository.GetOrDefaultAsync(setpointsRequestDto.Id, token); + + if (entity.IdWell != setpointsRequestDto.IdWell) + return 0; if (entity is null) return 0; diff --git a/AsbCloudInfrastructure/Services/ScheduleService.cs b/AsbCloudInfrastructure/Services/ScheduleService.cs index c51a5aad..06a75a1d 100644 --- a/AsbCloudInfrastructure/Services/ScheduleService.cs +++ b/AsbCloudInfrastructure/Services/ScheduleService.cs @@ -1,6 +1,7 @@ using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; +using AsbCloudInfrastructure.Repository; using Mapster; using Microsoft.EntityFrameworkCore; using System; diff --git a/AsbCloudInfrastructure/Services/UserRoleService.cs b/AsbCloudInfrastructure/Services/UserRoleService.cs index c3c0d48e..e92e693d 100644 --- a/AsbCloudInfrastructure/Services/UserRoleService.cs +++ b/AsbCloudInfrastructure/Services/UserRoleService.cs @@ -56,7 +56,7 @@ namespace AsbCloudInfrastructure.Services var dtos = entities?.Select(Convert); return dtos; } - public UserRoleDto Get(int id) + public UserRoleDto GetOrDefault(int id) { var entity = cacheUserRoles.FirstOrDefault(r => r.Id == id); if (entity is null) @@ -65,7 +65,7 @@ namespace AsbCloudInfrastructure.Services return dto; } - public async Task GetAsync(int id, CancellationToken token = default) + public async Task GetOrDefaultAsync(int id, CancellationToken token = default) { var entity = await cacheUserRoles.FirstOrDefaultAsync(r => r.Id == id, token) .ConfigureAwait(false); diff --git a/AsbCloudInfrastructure/Services/UserService.cs b/AsbCloudInfrastructure/Services/UserService.cs index a32448da..8cfdf26c 100644 --- a/AsbCloudInfrastructure/Services/UserService.cs +++ b/AsbCloudInfrastructure/Services/UserService.cs @@ -81,7 +81,7 @@ namespace AsbCloudInfrastructure.Services return dtos; } - public UserExtendedDto Get(int id) + public UserExtendedDto GetOrDefault(int id) { var entity = cacheUsers.FirstOrDefault(u => u.Id == id); var dto = Convert(entity); @@ -89,7 +89,7 @@ namespace AsbCloudInfrastructure.Services return dto; } - public async Task GetAsync(int id, CancellationToken token = default) + public async Task GetOrDefaultAsync(int id, CancellationToken token = default) { var entity = await cacheUsers.FirstOrDefaultAsync(u => u.Id == id, token).ConfigureAwait(false); var dto = Convert(entity); diff --git a/AsbCloudInfrastructure/Services/WellOperationService/ScheduleReportService.cs b/AsbCloudInfrastructure/Services/WellOperationService/ScheduleReportService.cs index 4387124f..d53a87aa 100644 --- a/AsbCloudInfrastructure/Services/WellOperationService/ScheduleReportService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationService/ScheduleReportService.cs @@ -31,7 +31,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService if (!tvd.Any()) return null; - var well = await wellService.GetAsync(idWell, token); + var well = await wellService.GetOrDefaultAsync(idWell, token); var ecxelTemplateStream = GetExcelTemplateStream(); using var workbook = new XLWorkbook(ecxelTemplateStream, XLEventTracking.Disabled); diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 30043b10..7107bda5 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -3,6 +3,7 @@ using AsbCloudApp.Exceptions; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.EfCache; +using AsbCloudInfrastructure.Repository; using AsbCloudInfrastructure.Services.Cache; using Mapster; using Microsoft.EntityFrameworkCore; @@ -62,7 +63,7 @@ namespace AsbCloudInfrastructure.Services public DateTimeOffset GetLastTelemetryDate(int idWell) { - var well = Get(idWell); + var well = GetOrDefault(idWell); if (well?.IdTelemetry is null) return DateTimeOffset.MinValue; @@ -154,7 +155,7 @@ namespace AsbCloudInfrastructure.Services public async Task GetWellCaptionByIdAsync(int idWell, CancellationToken token) { - var entity = await GetAsync(idWell, token).ConfigureAwait(false); + var entity = await GetOrDefaultAsync(idWell, token).ConfigureAwait(false); var dto = Convert(entity); return dto.Caption; } @@ -186,7 +187,7 @@ namespace AsbCloudInfrastructure.Services public async Task> GetClusterWellsIdsAsync(int idWell, CancellationToken token) { - var well = await GetAsync(idWell, token); + var well = await GetOrDefaultAsync(idWell, token); if (well is null) return null; @@ -239,7 +240,7 @@ namespace AsbCloudInfrastructure.Services { var dto = entity.Adapt(); dto.CompanyTypeCaption = entity.CompanyType?.Caption - ?? companyTypesService.Get(entity.IdCompanyType).Caption; + ?? companyTypesService.GetOrDefault(entity.IdCompanyType).Caption; return dto; } @@ -265,7 +266,7 @@ namespace AsbCloudInfrastructure.Services public SimpleTimezoneDto GetTimezone(int idWell) { - var well = Get(idWell); + var well = GetOrDefault(idWell); if (well == null) throw new ArgumentInvalidException($"idWell: {idWell} does not exist.", nameof(idWell)); return GetTimezone(well); @@ -334,7 +335,7 @@ namespace AsbCloudInfrastructure.Services public DatesRangeDto GetDatesRange(int idWell) { - var well = Get(idWell); + var well = GetOrDefault(idWell); if (well is null) throw new Exception($"Well id: {idWell} does not exist."); diff --git a/AsbCloudWebApi.Tests/ServicesTests/CrudServiceTestAbstract.cs b/AsbCloudWebApi.Tests/ServicesTests/CrudServiceTestAbstract.cs index 73b9269d..d427e038 100644 --- a/AsbCloudWebApi.Tests/ServicesTests/CrudServiceTestAbstract.cs +++ b/AsbCloudWebApi.Tests/ServicesTests/CrudServiceTestAbstract.cs @@ -43,7 +43,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests public async Task GetById() { var id = await Insert(); - var gotItem = await service.GetAsync(id, CancellationToken.None); + var gotItem = await service.GetOrDefaultAsync(id, CancellationToken.None); Assert.True(id > 0); Assert.Equal(id, gotItem.Id); } diff --git a/AsbCloudWebApi.Tests/ServicesTests/DrillingProgramServiceTest.cs b/AsbCloudWebApi.Tests/ServicesTests/DrillingProgramServiceTest.cs index 5499b9cc..7cea8d32 100644 --- a/AsbCloudWebApi.Tests/ServicesTests/DrillingProgramServiceTest.cs +++ b/AsbCloudWebApi.Tests/ServicesTests/DrillingProgramServiceTest.cs @@ -162,7 +162,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests db.DrillingProgramParts.Add(new DrillingProgramPart { IdFileCategory = 1001, IdWell = idWell }); db.SaveChanges(); - userServiceMock.Setup((s) => s.GetAsync(It.IsAny(), It.IsAny())) + userServiceMock.Setup((s) => s.GetOrDefaultAsync(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(publisher1.Adapt())); var service = new DrillingProgramService( @@ -197,7 +197,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests IdUserRole = idUserRole }); db.SaveChanges(); - userServiceMock.Setup((s) => s.GetAsync(It.IsAny(), It.IsAny())) + userServiceMock.Setup((s) => s.GetOrDefaultAsync(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(publisher1.Adapt())); var service = new DrillingProgramService( @@ -346,7 +346,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests ); await db.SaveChangesAsync(); - wellServiceMock.Setup(s => s.GetAsync(It.IsAny(), It.IsAny())) + wellServiceMock.Setup(s => s.GetOrDefaultAsync(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(new WellDto { Caption = "test well", Cluster = "test cluster" })); var service = new DrillingProgramService( @@ -376,7 +376,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests await db.SaveChangesAsync(); - wellServiceMock.Setup(s => s.GetAsync(It.IsAny(), It.IsAny())) + wellServiceMock.Setup(s => s.GetOrDefaultAsync(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(new WellDto { Caption = "test well", Cluster = "test cluster" })); var service = new DrillingProgramService( diff --git a/AsbCloudWebApi/Controllers/AdminUserRoleController.cs b/AsbCloudWebApi/Controllers/AdminUserRoleController.cs index 11839832..39b91721 100644 --- a/AsbCloudWebApi/Controllers/AdminUserRoleController.cs +++ b/AsbCloudWebApi/Controllers/AdminUserRoleController.cs @@ -16,7 +16,7 @@ namespace AsbCloudWebApi.Controllers { UpdateForbidAsync = async (dto, token) => { - var role = await service.GetAsync(dto.Id, token); + var role = await service.GetOrDefaultAsync(dto.Id, token); return role?.IdType != 1; }; diff --git a/AsbCloudWebApi/Controllers/CrudController.cs b/AsbCloudWebApi/Controllers/CrudController.cs index 8c6df061..db66e468 100644 --- a/AsbCloudWebApi/Controllers/CrudController.cs +++ b/AsbCloudWebApi/Controllers/CrudController.cs @@ -57,7 +57,7 @@ namespace AsbCloudWebApi.Controllers [Permission] public virtual async Task> GetAsync(int id, CancellationToken token) { - var result = await service.GetAsync(id, token).ConfigureAwait(false); + var result = await service.GetOrDefaultAsync(id, token).ConfigureAwait(false); return Ok(result); } diff --git a/AsbCloudWebApi/Controllers/CrudWellRelatedController.cs b/AsbCloudWebApi/Controllers/CrudWellRelatedController.cs index dcfc5942..a8042d15 100644 --- a/AsbCloudWebApi/Controllers/CrudWellRelatedController.cs +++ b/AsbCloudWebApi/Controllers/CrudWellRelatedController.cs @@ -112,7 +112,7 @@ namespace AsbCloudWebApi.Controllers [HttpDelete("{id}")] public override async Task> DeleteAsync(int id, CancellationToken token) { - var item = await service.GetAsync(id, token); + var item = await service.GetOrDefaultAsync(id, token); if (item is null) return NoContent(); if (!await UserHasAccesToWellAsync(item.IdWell, token)) diff --git a/AsbCloudWebApi/Controllers/DailyReportController.cs b/AsbCloudWebApi/Controllers/DailyReportController.cs index 7aec2106..4381f308 100644 --- a/AsbCloudWebApi/Controllers/DailyReportController.cs +++ b/AsbCloudWebApi/Controllers/DailyReportController.cs @@ -101,7 +101,7 @@ namespace AsbCloudWebApi.Controllers [ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)] public async Task DownloadAsync(int idWell, DateTime date, CancellationToken token = default) { - var well = await wellService.GetAsync(idWell, token); + var well = await wellService.GetOrDefaultAsync(idWell, token); var stream = await dailyReportService.MakeReportAsync(idWell, date, token); if (stream != null) { diff --git a/AsbCloudWebApi/Controllers/SAUB/SetpointsController.cs b/AsbCloudWebApi/Controllers/SAUB/SetpointsController.cs index 51ce7e92..547e9c7f 100644 --- a/AsbCloudWebApi/Controllers/SAUB/SetpointsController.cs +++ b/AsbCloudWebApi/Controllers/SAUB/SetpointsController.cs @@ -115,7 +115,7 @@ namespace AsbCloudWebApi.Controllers.SAUB [AllowAnonymous] public async Task UpdateByTelemetryUidAsync([FromRoute] string uid, int id, SetpointsRequestDto setpointsRequestDto, CancellationToken token = default) { - var result = await setpointsService.UpdateStateAsync(id, setpointsRequestDto, token) + var result = await setpointsService.UpdateStateAsync(setpointsRequestDto, token) .ConfigureAwait(false); return Ok(result); diff --git a/AsbCloudWebApi/Controllers/WellController.cs b/AsbCloudWebApi/Controllers/WellController.cs index 6869964e..d7c7ef58 100644 --- a/AsbCloudWebApi/Controllers/WellController.cs +++ b/AsbCloudWebApi/Controllers/WellController.cs @@ -57,7 +57,7 @@ namespace AsbCloudWebApi.Controllers if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync(idCompany ?? default, idWell, token).ConfigureAwait(false)) return Forbid(); - var well = await wellService.GetAsync(idWell, + var well = await wellService.GetOrDefaultAsync(idWell, token).ConfigureAwait(false); return Ok(well); diff --git a/AsbCloudWebApi/wwwroot/asset-manifest.json b/AsbCloudWebApi/wwwroot/asset-manifest.json index 62c408f8..17511257 100644 --- a/AsbCloudWebApi/wwwroot/asset-manifest.json +++ b/AsbCloudWebApi/wwwroot/asset-manifest.json @@ -1,8 +1,8 @@ { "files": { - "main.css": "/static/css/main.a0664ea6.chunk.css", - "main.js": "/static/js/main.02d15bac.chunk.js", - "main.js.map": "/static/js/main.02d15bac.chunk.js.map", + "main.css": "/static/css/main.61ecfb0d.chunk.css", + "main.js": "/static/js/main.2493f2fa.chunk.js", + "main.js.map": "/static/js/main.2493f2fa.chunk.js.map", "runtime-main.js": "/static/js/runtime-main.83ebcb38.js", "runtime-main.js.map": "/static/js/runtime-main.83ebcb38.js.map", "static/js/2.ebe1f792.chunk.js": "/static/js/2.ebe1f792.chunk.js", @@ -36,7 +36,7 @@ "index.html": "/index.html", "static/css/3.f8ac3883.chunk.css.map": "/static/css/3.f8ac3883.chunk.css.map", "static/css/4.f8ac3883.chunk.css.map": "/static/css/4.f8ac3883.chunk.css.map", - "static/css/main.a0664ea6.chunk.css.map": "/static/css/main.a0664ea6.chunk.css.map", + "static/css/main.61ecfb0d.chunk.css.map": "/static/css/main.61ecfb0d.chunk.css.map", "static/js/2.ebe1f792.chunk.js.LICENSE.txt": "/static/js/2.ebe1f792.chunk.js.LICENSE.txt", "static/media/ClusterIcon.f85713df.svg": "/static/media/ClusterIcon.f85713df.svg", "static/media/DepositIcon.9688e406.svg": "/static/media/DepositIcon.9688e406.svg" @@ -44,7 +44,7 @@ "entrypoints": [ "static/js/runtime-main.83ebcb38.js", "static/js/2.ebe1f792.chunk.js", - "static/css/main.a0664ea6.chunk.css", - "static/js/main.02d15bac.chunk.js" + "static/css/main.61ecfb0d.chunk.css", + "static/js/main.2493f2fa.chunk.js" ] } \ No newline at end of file diff --git a/AsbCloudWebApi/wwwroot/index.html b/AsbCloudWebApi/wwwroot/index.html index ba0dec49..be8fe65a 100644 --- a/AsbCloudWebApi/wwwroot/index.html +++ b/AsbCloudWebApi/wwwroot/index.html @@ -1 +1 @@ -АСБ Vision
\ No newline at end of file +АСБ Vision
\ No newline at end of file