From 44730bb66e5c5f5258f5c54edc95ab53ca080752 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 26 Nov 2024 11:47:42 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BD=D1=8B=20=D0=B4?= =?UTF-8?q?=D0=B6=D0=B5=D0=BD=D0=B5=D1=80=D0=B8=D0=BA=D0=B8=20=D1=83=20?= =?UTF-8?q?=D0=B2=D1=81=D0=B5=D0=B3=D0=BE,=20=D1=87=D1=82=D0=BE=20=D0=BA?= =?UTF-8?q?=D0=B0=D1=81=D0=B0=D0=B5=D1=82=D1=81=D1=8F=20changeLog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ChangeLogController.cs | 31 ++++++++----- Persistence.Database/Entity/ChangeLog.cs | 4 +- Persistence.Database/Entity/IChangeLog.cs | 11 ++++- Persistence.Repository/DependencyInjection.cs | 2 +- .../Repositories/ChangeLogRepository.cs | 45 ++++++++++--------- Persistence/API/IChangeLogApi.cs | 16 +++---- Persistence/Models/ChangeLogDto.cs | 26 ++++++----- .../Repositories/IChangeLogRepository.cs | 17 ++++--- 8 files changed, 86 insertions(+), 66 deletions(-) diff --git a/Persistence.API/Controllers/ChangeLogController.cs b/Persistence.API/Controllers/ChangeLogController.cs index 6060faa..de0ef2f 100644 --- a/Persistence.API/Controllers/ChangeLogController.cs +++ b/Persistence.API/Controllers/ChangeLogController.cs @@ -9,27 +9,29 @@ namespace Persistence.API.Controllers; [ApiController] [Authorize] [Route("api/[controller]")] -public class ChangeLogController : ControllerBase, IChangeLogApi> - where TDto : class, IChangeLogDto, new() +public class ChangeLogController : ControllerBase, IChangeLogApi { - private IChangeLogRepository> repository; + private IChangeLogRepository repository; - public ChangeLogController(IChangeLogRepository> repository) + public ChangeLogController(IChangeLogRepository repository) { this.repository = repository; } [HttpPost] - public async Task> Add(TDto dto, CancellationToken token) + public async Task> Add( + [FromRoute] Guid idDiscriminator, + [FromBody]IDictionary dtos, + CancellationToken token) { var userId = User.GetUserId(); - var result = await repository.InsertRange(userId, [dto], token); + var result = await repository.InsertRange(userId, idDiscriminator, [dtos], token); return Ok(result); } [HttpPost("range")] - public Task> AddRange(IEnumerable dtos, CancellationToken token) + public Task> AddRange(IEnumerable> dtos, CancellationToken token) { throw new NotImplementedException(); } @@ -47,25 +49,30 @@ public class ChangeLogController : ControllerBase, IChangeLogApi>> GetChangeLogCurrent(CancellationToken token) + public Task>>> GetChangeLogCurrent(CancellationToken token) { throw new NotImplementedException(); } - [HttpGet("history")] - public Task>>> GetChangeLogForDate(DateTimeOffset historyMoment, CancellationToken token) + public Task>>>> GetChangeLogForDate(DateTimeOffset historyMoment, CancellationToken token) { throw new NotImplementedException(); } + //[HttpGet("history")] + //public Task>>>> GetChangeLogForDate(DateTimeOffset historyMoment, CancellationToken token) + //{ + // throw new NotImplementedException(); + //} + [HttpPut] - public Task> Update(TDto dto, CancellationToken token) + public Task> Update(IDictionary dto, CancellationToken token) { throw new NotImplementedException(); } [HttpPut("range")] - public Task> UpdateRange(IEnumerable dtos, CancellationToken token) + public Task> UpdateRange(IEnumerable> dtos, CancellationToken token) { throw new NotImplementedException(); } diff --git a/Persistence.Database/Entity/ChangeLog.cs b/Persistence.Database/Entity/ChangeLog.cs index f25f61b..59802ae 100644 --- a/Persistence.Database/Entity/ChangeLog.cs +++ b/Persistence.Database/Entity/ChangeLog.cs @@ -10,7 +10,7 @@ namespace Persistence.Database.Model; public class ChangeLog : IChangeLog { /// - /// Ид записи + /// Ключ записи /// [Key, Column("Id")] public Guid Id { get; set; } @@ -55,5 +55,5 @@ public class ChangeLog : IChangeLog /// Значение /// [Column("Value", TypeName = "jsonb")] - public required object Value { get; set; } + public required IDictionary Value { get; set; } } diff --git a/Persistence.Database/Entity/IChangeLog.cs b/Persistence.Database/Entity/IChangeLog.cs index f8a5b15..449c691 100644 --- a/Persistence.Database/Entity/IChangeLog.cs +++ b/Persistence.Database/Entity/IChangeLog.cs @@ -1,4 +1,6 @@  +using System.ComponentModel.DataAnnotations.Schema; + namespace Persistence.Database.Model; /// @@ -7,7 +9,7 @@ namespace Persistence.Database.Model; public interface IChangeLog { /// - /// Ид записи + /// Ключ записи /// public Guid Id { get; set; } @@ -36,8 +38,13 @@ public interface IChangeLog /// public Guid? IdNext { get; set; } + /// + /// Дискриминатор таблицы + /// + public Guid IdDiscriminator { get; set; } + /// /// Значение /// - public object Value { get; set; } + public IDictionary Value { get; set; } } diff --git a/Persistence.Repository/DependencyInjection.cs b/Persistence.Repository/DependencyInjection.cs index d732434..34c47f4 100644 --- a/Persistence.Repository/DependencyInjection.cs +++ b/Persistence.Repository/DependencyInjection.cs @@ -17,7 +17,7 @@ public static class DependencyInjection MapsterSetup(); services.AddTransient, TimeSeriesDataCachedRepository>(); - services.AddTransient(typeof(IChangeLogRepository<,>), typeof(ChangeLogRepository<,>)); + services.AddTransient(); return services; } diff --git a/Persistence.Repository/Repositories/ChangeLogRepository.cs b/Persistence.Repository/Repositories/ChangeLogRepository.cs index 9195788..f0d8141 100644 --- a/Persistence.Repository/Repositories/ChangeLogRepository.cs +++ b/Persistence.Repository/Repositories/ChangeLogRepository.cs @@ -5,9 +5,7 @@ using Persistence.Models; using Persistence.Repositories; namespace Persistence.Repository.Repositories; -public class ChangeLogRepository : IChangeLogRepository - where TDto : class, IChangeLogDto, new() - where TChangeLogDto : ChangeLogDto +public class ChangeLogRepository : IChangeLogRepository { private DbContext db; @@ -21,17 +19,17 @@ public class ChangeLogRepository : IChangeLogRepository ClearAndInsertRange(int idUser, IEnumerable dtos, CancellationToken token) + public Task ClearAndInsertRange(int idUser, IEnumerable> dtos, CancellationToken token) { throw new NotImplementedException(); } - public Task> GetChangeLogForDate(DateTimeOffset? updateFrom, CancellationToken token) + public Task>>> GetChangeLogForDate(DateTimeOffset? updateFrom, CancellationToken token) { throw new NotImplementedException(); } - public Task> GetCurrent(DateTimeOffset moment, CancellationToken token) + public Task>> GetCurrent(DateTimeOffset moment, CancellationToken token) { throw new NotImplementedException(); } @@ -41,25 +39,32 @@ public class ChangeLogRepository : IChangeLogRepository> GetGtDate(DateTimeOffset dateBegin, CancellationToken token) + public Task>> GetGtDate(DateTimeOffset dateBegin, CancellationToken token) { throw new NotImplementedException(); } - public Task InsertRange(Guid idUser, IEnumerable dtos, CancellationToken token) + public Task InsertRange(Guid idUser, Guid idDiscriminator, IEnumerable> dtos, CancellationToken token) { - var entity = new ChangeLog() { - Value = dtos, - Creation = DateTimeOffset.UtcNow, - Id = idUser, - IdAuthor = idUser, - IdDiscriminator = new Guid(), - IdEditor = idUser - }; - entity.Id = idUser; + var entities = new List(); + foreach (var dto in dtos) + { + var entity = new ChangeLog() + { + IdAuthor = idUser, + IdDiscriminator = idDiscriminator, + IdEditor = idUser, + Value = dto, + Creation = DateTimeOffset.UtcNow + }; + entity.Id = idUser; - db.Set().Add(entity); + entities.Add(entity); + } + + db.Set().AddRange(entities); var result = db.SaveChangesAsync(token); + return result; } @@ -68,12 +73,12 @@ public class ChangeLogRepository : IChangeLogRepository UpdateOrInsertRange(int idUser, IEnumerable dtos, CancellationToken token) + public Task UpdateOrInsertRange(int idUser, IEnumerable> dtos, CancellationToken token) { throw new NotImplementedException(); } - public Task UpdateRange(int idUser, IEnumerable dtos, CancellationToken token) + public Task UpdateRange(int idUser, IEnumerable> dtos, CancellationToken token) { throw new NotImplementedException(); } diff --git a/Persistence/API/IChangeLogApi.cs b/Persistence/API/IChangeLogApi.cs index 2a734ca..6cb0896 100644 --- a/Persistence/API/IChangeLogApi.cs +++ b/Persistence/API/IChangeLogApi.cs @@ -6,16 +6,14 @@ namespace Persistence.API; /// /// Интерфейс для работы с API журнала изменений /// -public interface IChangeLogApi - where TDto : class, IChangeLogDto, new() - where TChangeLogDto : ChangeLogDto +public interface IChangeLogApi { /// /// Получение исторических данных на текущую дату /// /// /// - Task>> GetChangeLogCurrent(CancellationToken token); + Task>>> GetChangeLogCurrent(CancellationToken token); /// /// Получение исторических данных на определенную дату @@ -23,7 +21,7 @@ public interface IChangeLogApi /// /// /// - Task>> GetChangeLogForDate(DateTimeOffset historyMoment, CancellationToken token); + Task>>>> GetChangeLogForDate(DateTimeOffset historyMoment, CancellationToken token); /// /// Добавить одну запись @@ -31,7 +29,7 @@ public interface IChangeLogApi /// /// /// - Task> Add(TDto dto, CancellationToken token); + Task> Add(Guid idDiscriminator, IDictionary dto, CancellationToken token); /// /// Добавить несколько записей @@ -39,7 +37,7 @@ public interface IChangeLogApi /// /// /// - Task> AddRange(IEnumerable dtos, CancellationToken token); + Task> AddRange(IEnumerable> dtos, CancellationToken token); /// /// Обновить одну запись @@ -47,7 +45,7 @@ public interface IChangeLogApi /// /// /// - Task> Update(TDto dto, CancellationToken token); + Task> Update(IDictionary dto, CancellationToken token); /// /// Обновить несколько записей @@ -55,7 +53,7 @@ public interface IChangeLogApi /// /// /// - Task> UpdateRange(IEnumerable dtos, CancellationToken token); + Task> UpdateRange(IEnumerable> dtos, CancellationToken token); /// /// Удалить одну запись diff --git a/Persistence/Models/ChangeLogDto.cs b/Persistence/Models/ChangeLogDto.cs index deb0c2b..8f716f4 100644 --- a/Persistence/Models/ChangeLogDto.cs +++ b/Persistence/Models/ChangeLogDto.cs @@ -5,8 +5,12 @@ namespace Persistence.Models; /// Часть записи описывающая изменение /// public class ChangeLogDto : IChangeLogDto - where T: class, IChangeLogDto, new() + where T: IDictionary { + public ChangeLogDto() + { + + } /// /// Запись /// @@ -15,35 +19,35 @@ public class ChangeLogDto : IChangeLogDto /// /// /// - public int Id { get; set; } + public Guid Id { get; set; } /// - /// + /// Создатель записи /// - public int IdAuthor { get; set; } + public Guid IdAuthor { get; set; } /// - /// + /// Пользователь, изменивший запись /// - public int? IdEditor { get; set; } + public Guid? IdEditor { get; set; } /// - /// + /// Дата создания /// public DateTimeOffset Creation { get; set; } /// - /// + /// Дата устаревания /// public DateTimeOffset? Obsolete { get; set; } /// - /// + /// Ключ следующей записи /// - public int? IdNext { get; set; } + public Guid? IdNext { get; set; } /// - /// + /// Объект записи /// public required object Value { get; set; } } diff --git a/Persistence/Repositories/IChangeLogRepository.cs b/Persistence/Repositories/IChangeLogRepository.cs index 5e05652..06fbeb2 100644 --- a/Persistence/Repositories/IChangeLogRepository.cs +++ b/Persistence/Repositories/IChangeLogRepository.cs @@ -6,18 +6,17 @@ namespace Persistence.Repositories; /// Интерфейс для работы с историческими данными /// /// -public interface IChangeLogRepository : ISyncRepository - where TDto : class, IChangeLogDto, new() - where TChangeLogDto : ChangeLogDto +public interface IChangeLogRepository //: ISyncRepository { /// /// Добавление записей /// /// пользователь, который добавляет + /// ключ справочника /// /// /// - Task InsertRange(Guid idUser, IEnumerable dtos, CancellationToken token); + Task InsertRange(Guid idUser, Guid idDiscriminator, IEnumerable> dtos, CancellationToken token); /// /// Редактирование записей @@ -26,7 +25,7 @@ public interface IChangeLogRepository : ISyncRepository /// /// - Task UpdateRange(int idUser, IEnumerable dtos, CancellationToken token); + Task UpdateRange(int idUser, IEnumerable> dtos, CancellationToken token); /// /// Добавляет Dto у которых id == 0, изменяет dto у которых id != 0 @@ -35,7 +34,7 @@ public interface IChangeLogRepository : ISyncRepository /// /// - Task UpdateOrInsertRange(int idUser, IEnumerable dtos, CancellationToken token); + Task UpdateOrInsertRange(int idUser, IEnumerable> dtos, CancellationToken token); /// /// Помечает записи как удаленные @@ -52,7 +51,7 @@ public interface IChangeLogRepository : ISyncRepository /// /// - Task ClearAndInsertRange(int idUser, IEnumerable dtos, CancellationToken token); + Task ClearAndInsertRange(int idUser, IEnumerable> dtos, CancellationToken token); /// /// Пометить записи как удаленные @@ -77,7 +76,7 @@ public interface IChangeLogRepository : ISyncRepository /// /// - Task> GetChangeLogForDate(DateTimeOffset? updateFrom, CancellationToken token); + Task>>> GetChangeLogForDate(DateTimeOffset? updateFrom, CancellationToken token); /// /// Получение текущих сейчас записей по параметрам @@ -85,5 +84,5 @@ public interface IChangeLogRepository : ISyncRepository /// /// - Task> GetCurrent(DateTimeOffset moment, CancellationToken token); + Task>> GetCurrent(DateTimeOffset moment, CancellationToken token); }