From 6d17500b2746b33e7ff1545193f76dbd53eb8bf0 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Fri, 26 Jan 2024 17:23:21 +0500 Subject: [PATCH] Add ChangeLogServiceAbstract --- .../Repositories/IChangeLogRepository.cs | 64 +++++++++++++---- .../IProcessMapPlanBaseRepository.cs | 52 -------------- .../Repositories/IProcessMapPlanRepository.cs | 2 + .../Services/ChangeLogServiceAbstract.cs | 69 +++++++++++++++++++ .../ProcessMapPlanBaseController.cs | 4 +- 5 files changed, 124 insertions(+), 67 deletions(-) delete mode 100644 AsbCloudApp/Repositories/IProcessMapPlanBaseRepository.cs create mode 100644 AsbCloudApp/Services/ChangeLogServiceAbstract.cs diff --git a/AsbCloudApp/Repositories/IChangeLogRepository.cs b/AsbCloudApp/Repositories/IChangeLogRepository.cs index 9a5ab8c9..97db1dd9 100644 --- a/AsbCloudApp/Repositories/IChangeLogRepository.cs +++ b/AsbCloudApp/Repositories/IChangeLogRepository.cs @@ -1,15 +1,18 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; +using AsbCloudApp.Requests; namespace AsbCloudApp.Repositories; /// /// Репозиторий для записей с историей /// -public interface IChangeLogRepository - where T : ChangeLogAbstract +public interface IChangeLogRepository + where TDto : ChangeLogAbstract + where TRequest : ChangeLogBaseRequest { /// /// Добавление записей @@ -18,7 +21,7 @@ public interface IChangeLogRepository /// /// /// - Task InsertRange(int idUser, IEnumerable dtos, CancellationToken token); + Task InsertRange(int idUser, IEnumerable dtos, CancellationToken token); /// /// Редактирование записей @@ -27,15 +30,50 @@ public interface IChangeLogRepository /// /// /// - Task UpdateRange(int idUser, IEnumerable dtos, CancellationToken token); + Task UpdateRange(int idUser, IEnumerable dtos, CancellationToken token); + + /// + /// Добавление записей с удалением старых (для импорта) + /// + /// + /// + /// + /// + Task Clear(int idUser, TRequest request, CancellationToken token); + + /// + /// Удаление записей + /// + /// + /// + /// + /// + Task DeleteRange(int idUser, IEnumerable ids, CancellationToken token); + + /// + /// Получение дат изменений записей + /// + /// + /// + /// + Task> GetDatesChange(TRequest request, CancellationToken token); + + /// + /// Получение журнала изменений + /// + /// + /// + /// + /// + Task> GetChangeLog(TRequest request, DateOnly? date, CancellationToken token); + + /// + /// Получение записей по параметрам + /// + /// + /// + /// + Task> Get(TRequest request, CancellationToken token); - /// - /// Удаление записей - /// - /// - /// - /// - /// - Task DeleteRange(int idUser, IEnumerable ids, CancellationToken token); } diff --git a/AsbCloudApp/Repositories/IProcessMapPlanBaseRepository.cs b/AsbCloudApp/Repositories/IProcessMapPlanBaseRepository.cs deleted file mode 100644 index 7524002a..00000000 --- a/AsbCloudApp/Repositories/IProcessMapPlanBaseRepository.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using AsbCloudApp.Data.ProcessMapPlan; -using AsbCloudApp.Requests; - -namespace AsbCloudApp.Repositories; - -/// -/// Общий интерфейс для РТК план с учетом истории изменений -/// -/// -public interface IProcessMapPlanBaseRepository: IChangeLogRepository - where T: ProcessMapPlanBaseDto -{ - /// - /// Добавление записей с удалением старых (для импорта) - /// - /// - /// - /// - /// - /// - Task ClearAndInsertRange(int idUser, int idWell, IEnumerable dtos, CancellationToken token); - - /// - /// Получение дат изменений записей - /// - /// - /// - /// - Task> GetDatesChange(int idWell, CancellationToken token); - - /// - /// Получение журнала изменений - /// - /// - /// - /// - /// - Task> GetChangeLog(int idWell, DateOnly? date, CancellationToken token); - - /// - /// Получение записей по параметрам - /// - /// - /// - /// - /// - Task> Get(int idWell, ProcessMapPlanBaseRequest request, CancellationToken token); -} \ No newline at end of file diff --git a/AsbCloudApp/Repositories/IProcessMapPlanRepository.cs b/AsbCloudApp/Repositories/IProcessMapPlanRepository.cs index 1828b3ed..7e41cb58 100644 --- a/AsbCloudApp/Repositories/IProcessMapPlanRepository.cs +++ b/AsbCloudApp/Repositories/IProcessMapPlanRepository.cs @@ -4,12 +4,14 @@ using System.Threading; using AsbCloudApp.Data.ProcessMaps; using AsbCloudApp.Requests; using AsbCloudApp.Services; +using System; namespace AsbCloudApp.Repositories; /// /// РТК план /// +[Obsolete] public interface IProcessMapPlanRepository : IRepositoryWellRelated where TDto : ProcessMapPlanBaseDto { diff --git a/AsbCloudApp/Services/ChangeLogServiceAbstract.cs b/AsbCloudApp/Services/ChangeLogServiceAbstract.cs new file mode 100644 index 00000000..31202939 --- /dev/null +++ b/AsbCloudApp/Services/ChangeLogServiceAbstract.cs @@ -0,0 +1,69 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Repositories; +using AsbCloudApp.Requests; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Threading; +using System.Linq; +using System.ComponentModel.DataAnnotations; +using AsbCloudApp.Exceptions; + +namespace AsbCloudApp.Services +{ + /// + /// + /// + /// + /// + public abstract class ChangeLogServiceAbstract + where TDto : ChangeLogAbstract + where TRequest : ChangeLogBaseRequest + { + /// + /// Репозиторий + /// + public IChangeLogRepository repository { get; } + + /// + /// ctor + /// + /// + public ChangeLogServiceAbstract(IChangeLogRepository repository) + { + this.repository = repository; + } + + /// + /// Добавляет Dto у которых id == 0, изменяет dto у которых id != 0 + /// + /// + /// + /// + /// + public virtual async Task UpdateOrInsertRange(int idUser, IEnumerable dtos, CancellationToken token) + { + var validationResults = Validate(dtos); + if (validationResults.Any()) + { + var errors = validationResults.SelectMany(r => r.MemberNames.Select(member => $"{member}: {r.ErrorMessage}")); + throw new ArgumentInvalidException(nameof(dtos), $"not valid: {string.Join("\n", errors)}"); + } + + var itemsToInsert = dtos.Where(e => e.Id == 0); + var itemsToUpdate = dtos.Where(e => e.Id != 0); + + var result = await repository.InsertRange(idUser, itemsToInsert, token); + result += await repository.UpdateRange(idUser, itemsToInsert, token); + + return result; + } + + /// + /// Валидация входных данных + /// + /// + /// + protected virtual IEnumerable Validate(IEnumerable dtos) + => Enumerable.Empty(); + } +} diff --git a/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanBaseController.cs b/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanBaseController.cs index 1237e628..8d954a60 100644 --- a/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanBaseController.cs +++ b/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanBaseController.cs @@ -23,10 +23,10 @@ namespace AsbCloudWebApi.Controllers.ProcessMapPlan; public abstract class ProcessMapPlanBaseController : ControllerBase where TDto : ProcessMapPlanBaseDto { - private readonly IProcessMapPlanBaseRepository repository; + private readonly ProcessMapPlanBaseService repository; private readonly IWellService wellService; - public ProcessMapPlanBaseController(IProcessMapPlanBaseRepository repository, IWellService wellService) + public ProcessMapPlanBaseController(ProcessMapPlanBaseService repository, IWellService wellService) { this.repository = repository; this.wellService = wellService;