Add IChangeLogQueryBuilderWithKnownTimezone

This commit is contained in:
ngfrolov 2024-06-05 15:52:40 +05:00
parent 9322d2874b
commit 96e9cd3f16
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7
5 changed files with 117 additions and 60 deletions

View File

@ -0,0 +1,62 @@
using AsbCloudApp.Data;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudApp.Repositories;
/// <summary>
/// Интерфейс для работы с объектами, содержащими историю изменений
/// </summary>
/// <typeparam name="TDto"></typeparam>
/// <typeparam name="TRequest"></typeparam>
public interface IChangeLogQueryBuilder<TDto, TRequest>
where TDto : IId
{
/// <summary>
/// Применение запроса
/// </summary>
/// <param name="request">Запрос</param>
/// <returns></returns>
IChangeLogQueryBuilderWithKnownTimezone<TDto, TRequest> ApplyRequest(TRequest request);
/// <summary>
/// Материализация записей
/// </summary>
/// <param name="offset"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<TDto>> GetData(TimeSpan offset, CancellationToken token);
/// <summary>
/// Материализация записей с историей
/// </summary>
/// <param name="offset"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<ChangeLogDto<TDto>>> GetChangeLogData(TimeSpan offset, CancellationToken token);
}
/// <summary>
/// Интерфейс для работы с объектами, содержащими историю изменений. С известной временной зоной
/// </summary>
/// <typeparam name="TDto"></typeparam>
/// <typeparam name="TRequest"></typeparam>
public interface IChangeLogQueryBuilderWithKnownTimezone<TDto, TRequest>: IChangeLogQueryBuilder<TDto, TRequest>
where TDto : IId
{
/// <summary>
/// Материализация записей. Временная зона определяется по запросу из последнего ApplyRequest
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<TDto>> GetData(CancellationToken token);
/// <summary>
/// Материализация записей с историей. Временная зона определяется по запросу из последнего ApplyRequest
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<ChangeLogDto<TDto>>> GetChangeLogData(CancellationToken token);
}

View File

@ -93,11 +93,10 @@ public interface IChangeLogRepository<TDto, TRequest>
/// <returns></returns>
Task<IEnumerable<TDto>> GetCurrent(TRequest request, CancellationToken token);
/// <summary>
/// Получение объекта, реализующего интерфейс IChangeLogRepositoryBuilder
/// для последующих вызовов методов фильтрации по запросам
/// </summary>
/// <returns></returns>
IChangeLogRepositoryBuilder<TDto, TRequest> GetRequestBuilder(ChangeLogRequest request);
IChangeLogQueryBuilder<TDto, TRequest> GetQueryBuilder(ChangeLogRequest request);
}

View File

@ -1,40 +0,0 @@
using AsbCloudApp.Data;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudApp.Repositories;
/// <summary>
/// Интерфейс для работы с объектами, содержащими историю изменений
/// </summary>
/// <typeparam name="TDto"></typeparam>
/// <typeparam name="TRequest"></typeparam>
public interface IChangeLogRepositoryBuilder<TDto, TRequest>
where TDto : IId
{
/// <summary>
/// Применение запроса
/// </summary>
/// <param name="request">Запрос</param>
/// <param name="token"></param>
/// <returns></returns>
IChangeLogRepositoryBuilder<TDto, TRequest> ApplyRequest(TRequest request, CancellationToken token);
/// <summary>
/// Материализация записей
/// </summary>
/// <param name="offset"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<TDto>> GetData(TimeSpan offset, CancellationToken token);
/// <summary>
/// Материализация записей с историей
/// </summary>
/// <param name="offset"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<ChangeLogDto<TDto>>> GetChangeLogData(TimeSpan offset, CancellationToken token);
}

View File

@ -25,12 +25,12 @@ public abstract class ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> : ICh
this.db = db;
}
private class ChangeLogRepositoryBuilder<TEntityChangeLog, TDtoChangeLog, TRequestChangeLog> : IChangeLogRepositoryBuilder<TDto, TRequest>
private class ChangeLogQueryBuilder: IChangeLogQueryBuilder<TDto, TRequest>
{
private readonly ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> Repository;
private IQueryable<TEntity> Query;
protected readonly ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> Repository;
protected IQueryable<TEntity> Query;
public ChangeLogRepositoryBuilder(
public ChangeLogQueryBuilder(
ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> repository,
ChangeLogRequest request)
{
@ -49,15 +49,20 @@ public abstract class ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> : ICh
}
}
public IChangeLogRepositoryBuilder<TDto, TRequest> ApplyRequest(TRequest request, CancellationToken token)
public ChangeLogQueryBuilder(ChangeLogQueryBuilder builder)
{
Repository = builder.Repository;
Query = builder.Query;
}
public virtual IChangeLogQueryBuilderWithKnownTimezone<TDto, TRequest> ApplyRequest(TRequest request)
{
this.Query = Repository.BuildQuery(request, Query);
return this;
return new ChangeLogQueryBuilderWithKnownTimezone(this, request);
}
public async Task<IEnumerable<TDto>> GetData(TimeSpan offset, CancellationToken token)
{
var dtos = await this.Query.Select(e => Repository.Convert(e, offset))
.ToArrayAsync(token);
return dtos;
@ -71,10 +76,43 @@ public abstract class ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> : ICh
}
}
public IChangeLogRepositoryBuilder<TDto, TRequest> GetRequestBuilder(ChangeLogRequest request)
private class ChangeLogQueryBuilderWithKnownTimezone: ChangeLogQueryBuilder, IChangeLogQueryBuilderWithKnownTimezone<TDto, TRequest>
{
var builder = new ChangeLogRepositoryBuilder<TEntity, TDto, TRequest>(this, request);
TRequest request;
public ChangeLogQueryBuilderWithKnownTimezone(
ChangeLogQueryBuilder parentBuilder,
TRequest request)
:base(parentBuilder)
{
this.request = request;
}
public override IChangeLogQueryBuilderWithKnownTimezone<TDto, TRequest> ApplyRequest(TRequest request)
{
Query = Repository.BuildQuery(request, Query);
this.request = request;
return this;
}
public async Task<IEnumerable<TDto>> GetData(CancellationToken token)
{
TimeSpan timezoneOffset = Repository.GetTimezoneOffset(request);
var dtos = await this.GetData(timezoneOffset, token);
return dtos;
}
public async Task<IEnumerable<ChangeLogDto<TDto>>> GetChangeLogData(CancellationToken token)
{
TimeSpan timezoneOffset = Repository.GetTimezoneOffset(request);
var dtos = await this.GetChangeLogData(timezoneOffset, token);
return dtos;
}
}
public IChangeLogQueryBuilder<TDto, TRequest> GetQueryBuilder(ChangeLogRequest request)
{
var builder = new ChangeLogQueryBuilder(this, request);
return builder;
}
@ -311,11 +349,9 @@ public abstract class ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> : ICh
Moment = new DateTimeOffset(3000, 1, 1, 0, 0, 0, TimeSpan.Zero)
};
var builder = new ChangeLogRepositoryBuilder<TEntity, TDto, TRequest>(this, changeLogRequest);
builder.ApplyRequest(request, token);
var offset = GetTimezoneOffset(request);
var dtos = await builder.GetData(offset, token);
var builder = GetQueryBuilder(changeLogRequest)
.ApplyRequest(request);
var dtos = await builder.GetData(token);
return dtos;
}

View File

@ -172,11 +172,11 @@ public abstract class ProcessMapPlanBaseController<TEntity, TDto> : ControllerBa
Moment = moment
};
var builder = repository.GetRequestBuilder(changeLogRequest);
builder.ApplyRequest(dataRequest, token);
var builder = repository
.GetQueryBuilder(changeLogRequest)
.ApplyRequest(dataRequest);
var offset = wellService.GetTimezone(idWell).Offset;
var dtos = await builder.GetChangeLogData(offset, token);
var dtos = await builder.GetChangeLogData(token);
return Ok(dtos);
}