diff --git a/Persistence.Database/Entity/ChangeLog.cs b/Persistence.Database/Entity/ChangeLog.cs index 2585ace..9a8001b 100644 --- a/Persistence.Database/Entity/ChangeLog.cs +++ b/Persistence.Database/Entity/ChangeLog.cs @@ -2,13 +2,14 @@ using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations; using Microsoft.EntityFrameworkCore; +using Persistence.Models; namespace Persistence.Database.Model; /// /// Часть записи, описывающая изменение /// -public class ChangeLog : IChangeLog +public class ChangeLog : IChangeLog, IWithSectionPart { [Key, Comment("Ключ записи")] public Guid Id { get; set; } diff --git a/Persistence.Database/Entity/IChangeLog.cs b/Persistence.Database/Entity/IChangeLog.cs index 04e08a4..c4dc962 100644 --- a/Persistence.Database/Entity/IChangeLog.cs +++ b/Persistence.Database/Entity/IChangeLog.cs @@ -1,6 +1,4 @@  -using System.ComponentModel.DataAnnotations.Schema; - namespace Persistence.Database.Model; /// diff --git a/Persistence.Database/Persistence.Database.csproj b/Persistence.Database/Persistence.Database.csproj index 8154daf..3019e82 100644 --- a/Persistence.Database/Persistence.Database.csproj +++ b/Persistence.Database/Persistence.Database.csproj @@ -14,4 +14,8 @@ + + + + diff --git a/Persistence.Repository/QueryBuilders.cs b/Persistence.Repository/QueryBuilders.cs new file mode 100644 index 0000000..23f5462 --- /dev/null +++ b/Persistence.Repository/QueryBuilders.cs @@ -0,0 +1,76 @@ +using Microsoft.EntityFrameworkCore; +using Persistence.Database.Model; +using Persistence.Models; +using Persistence.Models.Requests; + +namespace Persistence.Repository; + +/// +/// класс с набором методов, необходимых для фильтрации записей +/// +public static class QueryBuilders +{ + public static void Apply(this IQueryable query, SectionPartRequest request) + where TEntity : class, IWithSectionPart + { + if (request.IdSection.HasValue) + { + query = query.Where(e => e.IdSection == request.IdSection); + } + if (request.DepthStart.HasValue) + { + query = query.Where(e => e.DepthStart >= request.DepthStart); + } + if (request.DepthEnd.HasValue) + { + query = query.Where(e => e.DepthEnd <= request.DepthEnd); + } + } + + public static void Apply(this IQueryable query,DateTimeOffset momentUtc) + where TEntity : class, IChangeLog + { + momentUtc = momentUtc.ToUniversalTime(); + + query = query + .Where(e => e.Creation <= momentUtc) + .Where(e => e.Obsolete == null || e.Obsolete >= momentUtc); + } + + + public static async Task> ApplyPagination( + this IQueryable query, + PaginationRequest request, + Func Convert, + CancellationToken token) + where TEntity : class, IWithSectionPart + where TDto : class + { + if (String.IsNullOrEmpty(request.SortSettings)) + { + query = query + .OrderBy(e => e.IdSection) + .ThenBy(e => e.DepthStart) + .ThenBy(e => e.DepthEnd); + } + else + { + query = query.SortBy(request.SortSettings); + } + + var entities = await query + .Skip(request.Skip) + .Take(request.Take) + .ToArrayAsync(token); + + var result = new PaginationContainer + { + Skip = request.Skip, + Take = request.Take, + Items = entities.Select(Convert), + Count = await query.CountAsync(token) + }; + + return result; + } +} diff --git a/Persistence.Repository/Repositories/ChangeLogRepository.cs b/Persistence.Repository/Repositories/ChangeLogRepository.cs index 16790a7..7f6d573 100644 --- a/Persistence.Repository/Repositories/ChangeLogRepository.cs +++ b/Persistence.Repository/Repositories/ChangeLogRepository.cs @@ -37,7 +37,7 @@ public class ChangeLogRepository : IChangeLogRepository .Where(s => ids.Contains(s.Id)) .Where(s => s.Obsolete != null); - if(query.Count() != ids.Count()) + if (query.Count() != ids.Count()) { throw new ArgumentException("Count of active items not equal count of ids", nameof(ids)); } @@ -80,13 +80,13 @@ public class ChangeLogRepository : IChangeLogRepository var result = 0; using var transaction = await db.Database.BeginTransactionAsync(token); - + result += await MarkAsDeleted(idAuthor, idDiscriminator, token); result += await AddRange(idAuthor, idDiscriminator, dtos, token); await transaction.CommitAsync(token); - + return result; } @@ -106,7 +106,7 @@ public class ChangeLogRepository : IChangeLogRepository foreach (var dto in dtos) { var updatedEntity = updatedEntities.GetValueOrDefault(dto.Id); - if(updatedEntity is null) + if (updatedEntity is null) { throw new ArgumentException($"Entity with id = {dto.Id} doesn't exist in Db", nameof(dto)); } @@ -138,33 +138,18 @@ public class ChangeLogRepository : IChangeLogRepository PaginationRequest paginationRequest, CancellationToken token) { - var query = BuildQuery(idDiscriminator, momentUtc, filterRequest); - //ApplyFilter(query, , filterRequest); - var result = await BuildPaginationContainer(query, paginationRequest, token); + var query = CreateQuery(idDiscriminator); + query.Apply(momentUtc); + query.Apply(filterRequest); + + var result = await query.ApplyPagination(paginationRequest, Convert, token); return result; } - private IQueryable BuildQuery(Guid idDiscriminator, DateTimeOffset momentUtc, SectionPartRequest request) + private IQueryable CreateQuery(Guid idDiscriminator) { - momentUtc = momentUtc.ToUniversalTime(); - var query = db.Set() - .Where(e => e.IdDiscriminator == idDiscriminator) - .Where(e => e.Creation <= momentUtc) - .Where(e => e.Obsolete == null || e.Obsolete >= momentUtc); - - if (request.IdSection.HasValue) - { - query = query.Where(e => e.IdSection == request.IdSection); - } - if (request.DepthStart.HasValue) - { - query = query.Where(e => e.DepthStart >= request.DepthStart); - } - if (request.DepthEnd.HasValue) - { - query = query.Where(e => e.DepthEnd <= request.DepthEnd); - } + var query = db.Set().Where(e => e.IdDiscriminator == idDiscriminator); return query; } @@ -173,8 +158,8 @@ public class ChangeLogRepository : IChangeLogRepository { var query = db.Set().Where(s => s.IdDiscriminator == idDiscriminator); - var min = new DateTimeOffset(dateBegin.Year, dateBegin.Month, dateBegin.Day, 0, 0, 0, TimeSpan.Zero); - var max = new DateTimeOffset(dateEnd.Year, dateEnd.Month, dateEnd.Day, 0, 0, 0, TimeSpan.Zero); + var min = new DateTimeOffset(dateBegin.ToUniversalTime().Date, TimeSpan.Zero); + var max = new DateTimeOffset(dateEnd.ToUniversalTime().Date, TimeSpan.Zero); var createdQuery = query.Where(e => e.Creation >= min && e.Creation <= max); var editedQuery = query.Where(e => e.Obsolete != null && e.Obsolete >= min && e.Obsolete <= max); @@ -189,39 +174,6 @@ public class ChangeLogRepository : IChangeLogRepository - private async Task> BuildPaginationContainer(IQueryable query, PaginationRequest request, CancellationToken token) - { - var result = new PaginationContainer - { - Skip = request.Skip, - Take = request.Take, - Items = Enumerable.Empty(), - Count = await query.CountAsync(token) - }; - - if (!String.IsNullOrEmpty(request.SortSettings)) - { - query = query.SortBy(request.SortSettings); - } - else - { - query = query - .OrderBy(e => e.IdSection) - .ThenBy(e => e.DepthStart) - .ThenBy(e => e.DepthEnd); - } - - var entities = await query - .Skip(result.Skip) - .Take(result.Take) - .ToArrayAsync(token); - - var dtos = entities.Select(e => e.Adapt()); - result.Items = dtos; - - return result; - } - public async Task> GetDatesChange(Guid idDiscriminator, CancellationToken token) { var query = db.Set().Where(e => e.IdDiscriminator == idDiscriminator); @@ -276,7 +228,7 @@ public class ChangeLogRepository : IChangeLogRepository var entities = await query.ToArrayAsync(token); - var dtos = entities.Select(e => e.Adapt()); + var dtos = entities.Select(Convert); return dtos; } @@ -289,12 +241,12 @@ public class ChangeLogRepository : IChangeLogRepository .Select(group => new { Min = group.Min(e => e.Creation), - Max = group.Max(e => e.Creation), + Max = group.Max(e => (e.Creation > e.Obsolete ? e.Creation : e.Obsolete!.Value)), }); var values = await query.FirstOrDefaultAsync(token); - if(values is null) + if (values is null) { return null; } @@ -305,4 +257,6 @@ public class ChangeLogRepository : IChangeLogRepository To = values.Max, }; } + + private DataWithWellDepthAndSectionDto Convert(ChangeLog entity) => entity.Adapt(); } diff --git a/Persistence/Models/IWithSectionPart.cs b/Persistence/Models/IWithSectionPart.cs new file mode 100644 index 0000000..adb5fdd --- /dev/null +++ b/Persistence/Models/IWithSectionPart.cs @@ -0,0 +1,9 @@ +namespace Persistence.Models; +public interface IWithSectionPart +{ + public double DepthStart { get; set; } + + public double DepthEnd { get; set; } + + public Guid IdSection { get; set; } +}