persistence/Persistence/Repositories/AbstractChangeLogRepository.cs

166 lines
5.1 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Persistence.Models;
2024-11-01 12:26:14 +05:00
using System.Linq;
2024-10-31 15:01:12 +05:00
namespace Persistence.Repositories;
2024-11-01 12:26:14 +05:00
public abstract class AbstractChangeLogRepository<TEntity, TChangeLogDto, TDto> : IChangeLogRepository<TDto, TChangeLogDto>
where TDto : class, new()
where TEntity : class, IChangeLogAbstract
2024-11-01 12:26:14 +05:00
where TChangeLogDto : ChangeLogDto<TDto>
2024-10-31 15:01:12 +05:00
{
private readonly DbContext dbContext;
protected AbstractChangeLogRepository(DbContext dbContext)
{
this.dbContext = dbContext;
}
2024-11-01 12:26:14 +05:00
public abstract TEntity Convert(TDto entity);
2024-11-02 12:05:12 +05:00
public async Task<int> Clear(int idUser,CancellationToken token)
2024-10-31 15:01:12 +05:00
{
2024-11-02 12:05:12 +05:00
throw new NotImplementedException();
2024-11-01 12:26:14 +05:00
2024-11-02 12:05:12 +05:00
//var updateTime = DateTimeOffset.UtcNow;
2024-11-01 12:26:14 +05:00
2024-11-02 12:05:12 +05:00
////todo
//var query = BuildQuery(request);
//query = query.Where(e => e.Obsolete == null);
2024-11-01 12:26:14 +05:00
2024-11-02 12:05:12 +05:00
//var entitiesToDelete = await query.ToArrayAsync(token);
//foreach (var entity in entitiesToDelete)
//{
// entity.IdState = IChangeLogAbstract.IdCleared;
// entity.Obsolete = updateTime;
// entity.IdEditor = idUser;
//}
2024-11-01 12:26:14 +05:00
2024-11-02 12:05:12 +05:00
//var result = await SaveChangesWithExceptionHandling(token);
//return result;
2024-10-31 15:01:12 +05:00
}
2024-11-02 12:05:12 +05:00
public async Task<int> ClearAndInsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
2024-10-31 15:01:12 +05:00
{
2024-11-01 12:26:14 +05:00
var result = 0;
using var transaction = await dbContext.Database.BeginTransactionAsync(token);
try
{
2024-11-02 12:05:12 +05:00
result += await Clear(idUser, token);
2024-11-01 12:26:14 +05:00
result += await InsertRangeWithoutTransaction(idUser, dtos, token);
await transaction.CommitAsync(token);
return result;
}
catch
{
await transaction.RollbackAsync(token);
throw;
}
2024-10-31 15:01:12 +05:00
}
2024-11-02 12:05:12 +05:00
public Task<IEnumerable<TDto>> GetCurrent(DateTimeOffset moment, CancellationToken token)
2024-10-31 15:01:12 +05:00
{
throw new NotImplementedException();
}
2024-11-02 12:05:12 +05:00
public Task<IEnumerable<DateOnly>> GetDatesChange(CancellationToken token)
2024-10-31 15:01:12 +05:00
{
throw new NotImplementedException();
}
public Task<IEnumerable<TDto>> GetGtDate(DateTimeOffset date, CancellationToken token)
2024-10-31 15:01:12 +05:00
{
throw new NotImplementedException();
}
2024-11-01 12:26:14 +05:00
public async Task<int> InsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
2024-10-31 15:01:12 +05:00
{
2024-11-02 12:05:12 +05:00
using var transaction = dbContext.Database.BeginTransaction();
try
{
2024-11-01 12:26:14 +05:00
var result = await InsertRangeWithoutTransaction(idUser, dtos, token);
await transaction.CommitAsync(token);
return result;
}
catch
{
2024-11-01 12:26:14 +05:00
await transaction.RollbackAsync(token);
throw;
}
2024-10-31 15:01:12 +05:00
}
protected abstract DatabaseFacade GetDataBase();
2024-10-31 15:01:12 +05:00
public Task<int> MarkAsDeleted(int idUser, IEnumerable<int> ids, CancellationToken token)
{
throw new NotImplementedException();
}
public Task<int> UpdateOrInsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
{
throw new NotImplementedException();
}
public Task<int> UpdateRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
2024-10-31 15:01:12 +05:00
{
throw new NotImplementedException();
}
2024-11-02 12:05:12 +05:00
public Task<IEnumerable<TChangeLogDto>> GetChangeLogForDate(DateTimeOffset? updateFrom, CancellationToken token)
2024-10-31 15:01:12 +05:00
{
throw new NotImplementedException();
}
2024-11-01 12:26:14 +05:00
private async Task<int> InsertRangeWithoutTransaction(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
{
var result = 0;
if (dtos.Any())
{
var entities = dtos.Select(Convert);
var creation = DateTimeOffset.UtcNow;
var dbSet = dbContext.Set<TEntity>();
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<int> 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);
//}
2024-10-31 15:01:12 +05:00
}