Наработки
This commit is contained in:
parent
f22799f1ef
commit
49c9a6107b
@ -1,11 +1,13 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Persistence.Models;
|
||||
using System.Linq;
|
||||
|
||||
namespace Persistence.Repositories;
|
||||
public abstract class AbstractChangeLogRepository<TEntity, TRequest, TDto> : IChangeLogRepository<TDto, TRequest>
|
||||
public abstract class AbstractChangeLogRepository<TEntity, TChangeLogDto, TDto> : IChangeLogRepository<TDto, TChangeLogDto>
|
||||
where TDto : class, new()
|
||||
where TEntity : class, IChangeLogAbstract
|
||||
where TChangeLogDto : ChangeLogDto<TDto>
|
||||
{
|
||||
private readonly DbContext dbContext;
|
||||
|
||||
@ -13,14 +15,46 @@ public abstract class AbstractChangeLogRepository<TEntity, TRequest, TDto> : ICh
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
}
|
||||
public Task<int> Clear(int idUser, TRequest request, CancellationToken token)
|
||||
|
||||
public abstract TEntity Convert(TDto entity);
|
||||
public async Task<int> 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;
|
||||
}
|
||||
|
||||
public Task<int> ClearAndInsertRange(int idUser, TRequest request, IEnumerable<TDto> dtos, CancellationToken token)
|
||||
var result = await SaveChangesWithExceptionHandling(token);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<int> ClearAndInsertRange(int idUser, TRequest request, IEnumerable<TDto> 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<IEnumerable<TDto>> GetCurrent(TRequest request, CancellationToken token)
|
||||
@ -38,20 +72,20 @@ public abstract class AbstractChangeLogRepository<TEntity, TRequest, TDto> : ICh
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<int> InsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
|
||||
public async Task<int> InsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
|
||||
{
|
||||
this.dbContext.Set<TEntity>();
|
||||
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<TEntity, TRequest, TDto> : ICh
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<IEnumerable<ChangeLogDto<TDto>>> GetChangeLogForDate(TRequest request, DateOnly? date, CancellationToken token)
|
||||
public Task<IEnumerable<TChangeLogDto>> GetChangeLogForDate(TRequest request, DateOnly? date, CancellationToken token)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
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);
|
||||
//}
|
||||
}
|
||||
|
@ -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<T> : ITimeSeriesDataRepository<T>
|
||||
public abstract class AbstractTimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository<TDto>
|
||||
where TDto : class, new()
|
||||
where TEntity : class, IChangeLogAbstract
|
||||
{
|
||||
public Task<IEnumerable<T>> GetGtDate(DateTimeOffset date, CancellationToken token)
|
||||
private readonly DbContext dbContext;
|
||||
|
||||
protected AbstractTimeSeriesDataRepository(DbContext dbContext)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
}
|
||||
|
||||
public Task<IEnumerable<TDto>> GetGtDate(DateTimeOffset date, CancellationToken token)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<int> InsertRange(IEnumerable<TDto> dtos, CancellationToken token)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ namespace Persistence.Repositories;
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="TDto"></typeparam>
|
||||
public interface IChangeLogRepository<TDto, TRequest> : ISyncRepository<TDto>
|
||||
public interface IChangeLogRepository<TDto, TChangeLogDto> : ISyncRepository<TDto>
|
||||
where TDto : class
|
||||
{
|
||||
/// <summary>
|
||||
@ -79,7 +79,7 @@ public interface IChangeLogRepository<TDto, TRequest> : ISyncRepository<TDto>
|
||||
/// <param name="date">Фильтр по дате. Если null - вернет все записи, без привязки к дате</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<ChangeLogDto<TDto>>> GetChangeLogForDate(TRequest request, DateOnly? date, CancellationToken token);
|
||||
Task<IEnumerable<TChangeLogDto>> GetChangeLogForDate(TRequest request, DateOnly? date, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Получение текущих сейчас записей по параметрам
|
||||
|
@ -4,7 +4,13 @@
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public interface ITimeSeriesDataRepository<T> : ISyncRepository<T>
|
||||
public interface ITimeSeriesDataRepository<TDto> : ISyncRepository<TDto>
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Добавление записей
|
||||
/// </summary>
|
||||
/// <param name="dtos"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<int> InsertRange(IEnumerable<TDto> dtos, CancellationToken token);
|
||||
}
|
||||
|
@ -1,4 +0,0 @@
|
||||
namespace Persistence.Services;
|
||||
public abstract class ArchiveService : IArchiveService
|
||||
{
|
||||
}
|
@ -1,4 +1,25 @@
|
||||
namespace Persistence.Services;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
internal interface IArchiveService
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="connectionString"></param>
|
||||
/// <param name="databaseName"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task RenameDatabase(string connectionString, string databaseName, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="connectionString"></param>
|
||||
/// <param name="databaseName"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task CreateDatabase(string connectionString, string databaseName, CancellationToken token);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user