Add ChangeLogServiceAbstract

This commit is contained in:
ngfrolov 2024-01-26 17:23:21 +05:00
parent 148e111b6a
commit 6d17500b27
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
5 changed files with 124 additions and 67 deletions

View File

@ -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;
/// <summary>
/// Репозиторий для записей с историей
/// </summary>
public interface IChangeLogRepository<T>
where T : ChangeLogAbstract
public interface IChangeLogRepository<TDto, TRequest>
where TDto : ChangeLogAbstract
where TRequest : ChangeLogBaseRequest
{
/// <summary>
/// Добавление записей
@ -18,7 +21,7 @@ public interface IChangeLogRepository<T>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> InsertRange(int idUser, IEnumerable<T> dtos, CancellationToken token);
Task<int> InsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token);
/// <summary>
/// Редактирование записей
@ -27,15 +30,50 @@ public interface IChangeLogRepository<T>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> UpdateRange(int idUser, IEnumerable<T> dtos, CancellationToken token);
Task<int> UpdateRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token);
/// <summary>
/// Добавление записей с удалением старых (для импорта)
/// </summary>
/// <param name="idUser"></param>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> Clear(int idUser, TRequest request, CancellationToken token);
/// <summary>
/// Удаление записей
/// </summary>
/// <param name="idUser"></param>
/// <param name="ids"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> DeleteRange(int idUser, IEnumerable<int> ids, CancellationToken token);
/// <summary>
/// Получение дат изменений записей
/// </summary>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<DateOnly>> GetDatesChange(TRequest request, CancellationToken token);
/// <summary>
/// Получение журнала изменений
/// </summary>
/// <param name="request"></param>
/// <param name="date"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<TDto>> GetChangeLog(TRequest request, DateOnly? date, CancellationToken token);
/// <summary>
/// Получение записей по параметрам
/// </summary>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<TDto>> Get(TRequest request, CancellationToken token);
/// <summary>
/// Удаление записей
/// </summary>
/// <param name="idUser"></param>
/// <param name="ids"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> DeleteRange(int idUser, IEnumerable<int> ids, CancellationToken token);
}

View File

@ -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;
/// <summary>
/// Общий интерфейс для РТК план с учетом истории изменений
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IProcessMapPlanBaseRepository<T>: IChangeLogRepository<T>
where T: ProcessMapPlanBaseDto
{
/// <summary>
/// Добавление записей с удалением старых (для импорта)
/// </summary>
/// <param name="idUser"></param>
/// <param name="idWell"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> ClearAndInsertRange(int idUser, int idWell, IEnumerable<T> dtos, CancellationToken token);
/// <summary>
/// Получение дат изменений записей
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<DateOnly>> GetDatesChange(int idWell, CancellationToken token);
/// <summary>
/// Получение журнала изменений
/// </summary>
/// <param name="idWell"></param>
/// <param name="date"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<T>> GetChangeLog(int idWell, DateOnly? date, CancellationToken token);
/// <summary>
/// Получение записей по параметрам
/// </summary>
/// <param name="idWell"></param>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<T>> Get(int idWell, ProcessMapPlanBaseRequest request, CancellationToken token);
}

View File

@ -4,12 +4,14 @@ using System.Threading;
using AsbCloudApp.Data.ProcessMaps;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using System;
namespace AsbCloudApp.Repositories;
/// <summary>
/// РТК план
/// </summary>
[Obsolete]
public interface IProcessMapPlanRepository<TDto> : IRepositoryWellRelated<TDto>
where TDto : ProcessMapPlanBaseDto
{

View File

@ -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
{
/// <summary>
///
/// </summary>
/// <typeparam name="TDto"></typeparam>
/// <typeparam name="TRequest"></typeparam>
public abstract class ChangeLogServiceAbstract<TDto, TRequest>
where TDto : ChangeLogAbstract
where TRequest : ChangeLogBaseRequest
{
/// <summary>
/// Репозиторий
/// </summary>
public IChangeLogRepository<TDto, TRequest> repository { get; }
/// <summary>
/// ctor
/// </summary>
/// <param name="repository"></param>
public ChangeLogServiceAbstract(IChangeLogRepository<TDto, TRequest> repository)
{
this.repository = repository;
}
/// <summary>
/// Добавляет Dto у которых id == 0, изменяет dto у которых id != 0
/// </summary>
/// <param name="idUser"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
public virtual async Task<int> UpdateOrInsertRange(int idUser, IEnumerable<TDto> 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;
}
/// <summary>
/// Валидация входных данных
/// </summary>
/// <param name="dtos"></param>
/// <returns></returns>
protected virtual IEnumerable<ValidationResult> Validate(IEnumerable<TDto> dtos)
=> Enumerable.Empty<ValidationResult>();
}
}

View File

@ -23,10 +23,10 @@ namespace AsbCloudWebApi.Controllers.ProcessMapPlan;
public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase
where TDto : ProcessMapPlanBaseDto
{
private readonly IProcessMapPlanBaseRepository<TDto> repository;
private readonly ProcessMapPlanBaseService<TDto> repository;
private readonly IWellService wellService;
public ProcessMapPlanBaseController(IProcessMapPlanBaseRepository<TDto> repository, IWellService wellService)
public ProcessMapPlanBaseController(ProcessMapPlanBaseService<TDto> repository, IWellService wellService)
{
this.repository = repository;
this.wellService = wellService;