forked from ddrilling/AsbCloudServer
Add ChangeLogServiceAbstract
This commit is contained in:
parent
148e111b6a
commit
6d17500b27
@ -1,15 +1,18 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Requests;
|
||||||
|
|
||||||
namespace AsbCloudApp.Repositories;
|
namespace AsbCloudApp.Repositories;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Репозиторий для записей с историей
|
/// Репозиторий для записей с историей
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IChangeLogRepository<T>
|
public interface IChangeLogRepository<TDto, TRequest>
|
||||||
where T : ChangeLogAbstract
|
where TDto : ChangeLogAbstract
|
||||||
|
where TRequest : ChangeLogBaseRequest
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавление записей
|
/// Добавление записей
|
||||||
@ -18,7 +21,7 @@ public interface IChangeLogRepository<T>
|
|||||||
/// <param name="dtos"></param>
|
/// <param name="dtos"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<int> InsertRange(int idUser, IEnumerable<T> dtos, CancellationToken token);
|
Task<int> InsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Редактирование записей
|
/// Редактирование записей
|
||||||
@ -27,15 +30,50 @@ public interface IChangeLogRepository<T>
|
|||||||
/// <param name="dtos"></param>
|
/// <param name="dtos"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <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);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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);
|
|
||||||
}
|
|
@ -4,12 +4,14 @@ using System.Threading;
|
|||||||
using AsbCloudApp.Data.ProcessMaps;
|
using AsbCloudApp.Data.ProcessMaps;
|
||||||
using AsbCloudApp.Requests;
|
using AsbCloudApp.Requests;
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace AsbCloudApp.Repositories;
|
namespace AsbCloudApp.Repositories;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// РТК план
|
/// РТК план
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Obsolete]
|
||||||
public interface IProcessMapPlanRepository<TDto> : IRepositoryWellRelated<TDto>
|
public interface IProcessMapPlanRepository<TDto> : IRepositoryWellRelated<TDto>
|
||||||
where TDto : ProcessMapPlanBaseDto
|
where TDto : ProcessMapPlanBaseDto
|
||||||
{
|
{
|
||||||
|
69
AsbCloudApp/Services/ChangeLogServiceAbstract.cs
Normal file
69
AsbCloudApp/Services/ChangeLogServiceAbstract.cs
Normal 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>();
|
||||||
|
}
|
||||||
|
}
|
@ -23,10 +23,10 @@ namespace AsbCloudWebApi.Controllers.ProcessMapPlan;
|
|||||||
public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase
|
public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase
|
||||||
where TDto : ProcessMapPlanBaseDto
|
where TDto : ProcessMapPlanBaseDto
|
||||||
{
|
{
|
||||||
private readonly IProcessMapPlanBaseRepository<TDto> repository;
|
private readonly ProcessMapPlanBaseService<TDto> repository;
|
||||||
private readonly IWellService wellService;
|
private readonly IWellService wellService;
|
||||||
|
|
||||||
public ProcessMapPlanBaseController(IProcessMapPlanBaseRepository<TDto> repository, IWellService wellService)
|
public ProcessMapPlanBaseController(ProcessMapPlanBaseService<TDto> repository, IWellService wellService)
|
||||||
{
|
{
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
this.wellService = wellService;
|
this.wellService = wellService;
|
||||||
|
Loading…
Reference in New Issue
Block a user