From 49c9a6107badb480d121909e04b43df0f3cccfd8 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Fri, 1 Nov 2024 12:26:14 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractChangeLogRepository.cs | 107 ++++++++++++++++-- .../AbstractTimeSeriesDataRepository.cs | 22 +++- .../Repositories/IChangeLogRepository.cs | 4 +- .../Repositories/ITimeSeriesDataRepository.cs | 10 +- Persistence/Services/ArchiveService.cs | 4 - Persistence/Services/IArchiveService.cs | 21 ++++ 6 files changed, 146 insertions(+), 22 deletions(-) delete mode 100644 Persistence/Services/ArchiveService.cs diff --git a/Persistence/Repositories/AbstractChangeLogRepository.cs b/Persistence/Repositories/AbstractChangeLogRepository.cs index 687ccfc..92966fa 100644 --- a/Persistence/Repositories/AbstractChangeLogRepository.cs +++ b/Persistence/Repositories/AbstractChangeLogRepository.cs @@ -1,11 +1,13 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Persistence.Models; +using System.Linq; namespace Persistence.Repositories; -public abstract class AbstractChangeLogRepository : IChangeLogRepository +public abstract class AbstractChangeLogRepository : IChangeLogRepository where TDto : class, new() where TEntity : class, IChangeLogAbstract + where TChangeLogDto : ChangeLogDto { private readonly DbContext dbContext; @@ -13,14 +15,46 @@ public abstract class AbstractChangeLogRepository : ICh { this.dbContext = dbContext; } - public Task Clear(int idUser, TRequest request, CancellationToken token) + + public abstract TEntity Convert(TDto entity); + public async Task Clear(int idUser, TRequest request, CancellationToken token) { - throw new NotImplementedException(); + var updateTime = DateTimeOffset.UtcNow; + + //todo + var query = BuildQuery(request); + query = query.Where(e => e.Obsolete == null); + + var entitiesToDelete = await query.ToArrayAsync(token); + + foreach (var entity in entitiesToDelete) + { + entity.IdState = IChangeLogAbstract.IdCleared; + entity.Obsolete = updateTime; + entity.IdEditor = idUser; + } + + var result = await SaveChangesWithExceptionHandling(token); + return result; } - public Task ClearAndInsertRange(int idUser, TRequest request, IEnumerable dtos, CancellationToken token) + public async Task ClearAndInsertRange(int idUser, TRequest request, IEnumerable dtos, CancellationToken token) { - throw new NotImplementedException(); + var result = 0; + using var transaction = await dbContext.Database.BeginTransactionAsync(token); + try + { + result += await Clear(idUser, request, token); + result += await InsertRangeWithoutTransaction(idUser, dtos, token); + + await transaction.CommitAsync(token); + return result; + } + catch + { + await transaction.RollbackAsync(token); + throw; + } } public Task> GetCurrent(TRequest request, CancellationToken token) @@ -38,20 +72,20 @@ public abstract class AbstractChangeLogRepository : ICh throw new NotImplementedException(); } - public Task InsertRange(int idUser, IEnumerable dtos, CancellationToken token) + public async Task InsertRange(int idUser, IEnumerable dtos, CancellationToken token) { this.dbContext.Set(); var db = GetDataBase(); using var transaction = db.BeginTransaction(); try { - //var result = await InsertRangeWithoutTransaction(idUser, dtos, token); - //await transaction.CommitAsync(token); - //return result; + var result = await InsertRangeWithoutTransaction(idUser, dtos, token); + await transaction.CommitAsync(token); + return result; } catch { - //await transaction.RollbackAsync(token); + await transaction.RollbackAsync(token); throw; } } @@ -73,8 +107,59 @@ public abstract class AbstractChangeLogRepository : ICh throw new NotImplementedException(); } - public Task>> GetChangeLogForDate(TRequest request, DateOnly? date, CancellationToken token) + public Task> GetChangeLogForDate(TRequest request, DateOnly? date, CancellationToken token) { throw new NotImplementedException(); } + + private async Task InsertRangeWithoutTransaction(int idUser, IEnumerable dtos, CancellationToken token) + { + var result = 0; + if (dtos.Any()) + { + var entities = dtos.Select(Convert); + var creation = DateTimeOffset.UtcNow; + var dbSet = dbContext.Set(); + foreach (var entity in entities) + { + entity.Id = default; + entity.IdAuthor = idUser; + entity.Creation = creation; + entity.IdState = IChangeLogAbstract.IdStateActual; + entity.IdEditor = null; + entity.IdPrevious = null; + entity.Obsolete = null; + dbSet.Add(entity); + } + + result += await SaveChangesWithExceptionHandling(token); + } + + return result; + } + + private async Task SaveChangesWithExceptionHandling(CancellationToken token) + { + var result = await dbContext.SaveChangesAsync(token); + return result; + //try + //{ + // var result = await dbContext.SaveChangesAsync(token); + // return result; + //} + //catch (DbUpdateException ex) + //{ + // if (ex.InnerException is PostgresException pgException) + // TryConvertPostgresExceptionToValidateException(pgException); + // throw; + //} + } + + + + //private static void TryConvertPostgresExceptionToValidateException(PostgresException pgException) + //{ + // if (pgException.SqlState == PostgresErrorCodes.ForeignKeyViolation) + // throw new ArgumentInvalidException("dtos", pgException.Message + "\r\n" + pgException.Detail); + //} } diff --git a/Persistence/Repositories/AbstractTimeSeriesDataRepository.cs b/Persistence/Repositories/AbstractTimeSeriesDataRepository.cs index 92a1ccf..d42719f 100644 --- a/Persistence/Repositories/AbstractTimeSeriesDataRepository.cs +++ b/Persistence/Repositories/AbstractTimeSeriesDataRepository.cs @@ -1,13 +1,29 @@ -using System; +using Microsoft.EntityFrameworkCore; +using Persistence.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Persistence.Repositories; -public abstract class AbstractTimeSeriesDataRepository : ITimeSeriesDataRepository +public abstract class AbstractTimeSeriesDataRepository : ITimeSeriesDataRepository + where TDto : class, new() + where TEntity : class, IChangeLogAbstract { - public Task> GetGtDate(DateTimeOffset date, CancellationToken token) + private readonly DbContext dbContext; + + protected AbstractTimeSeriesDataRepository(DbContext dbContext) + { + this.dbContext = dbContext; + } + + public Task> GetGtDate(DateTimeOffset date, CancellationToken token) + { + throw new NotImplementedException(); + } + + public Task InsertRange(IEnumerable dtos, CancellationToken token) { throw new NotImplementedException(); } diff --git a/Persistence/Repositories/IChangeLogRepository.cs b/Persistence/Repositories/IChangeLogRepository.cs index b75bde8..628f59f 100644 --- a/Persistence/Repositories/IChangeLogRepository.cs +++ b/Persistence/Repositories/IChangeLogRepository.cs @@ -6,7 +6,7 @@ namespace Persistence.Repositories; /// /// /// -public interface IChangeLogRepository : ISyncRepository +public interface IChangeLogRepository : ISyncRepository where TDto : class { /// @@ -79,7 +79,7 @@ public interface IChangeLogRepository : ISyncRepository /// Фильтр по дате. Если null - вернет все записи, без привязки к дате /// /// - Task>> GetChangeLogForDate(TRequest request, DateOnly? date, CancellationToken token); + Task> GetChangeLogForDate(TRequest request, DateOnly? date, CancellationToken token); /// /// Получение текущих сейчас записей по параметрам diff --git a/Persistence/Repositories/ITimeSeriesDataRepository.cs b/Persistence/Repositories/ITimeSeriesDataRepository.cs index c246366..9c28e37 100644 --- a/Persistence/Repositories/ITimeSeriesDataRepository.cs +++ b/Persistence/Repositories/ITimeSeriesDataRepository.cs @@ -4,7 +4,13 @@ /// /// /// -public interface ITimeSeriesDataRepository : ISyncRepository +public interface ITimeSeriesDataRepository : ISyncRepository { - + /// + /// Добавление записей + /// + /// + /// + /// + Task InsertRange(IEnumerable dtos, CancellationToken token); } diff --git a/Persistence/Services/ArchiveService.cs b/Persistence/Services/ArchiveService.cs deleted file mode 100644 index 34c48c2..0000000 --- a/Persistence/Services/ArchiveService.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace Persistence.Services; -public abstract class ArchiveService : IArchiveService -{ -} diff --git a/Persistence/Services/IArchiveService.cs b/Persistence/Services/IArchiveService.cs index 83c0cd9..285f76a 100644 --- a/Persistence/Services/IArchiveService.cs +++ b/Persistence/Services/IArchiveService.cs @@ -1,4 +1,25 @@ namespace Persistence.Services; + +/// +/// +/// internal interface IArchiveService { + /// + /// + /// + /// + /// + /// + /// + Task RenameDatabase(string connectionString, string databaseName, CancellationToken token); + + /// + /// + /// + /// + /// + /// + /// + Task CreateDatabase(string connectionString, string databaseName, CancellationToken token); }