Наработки

This commit is contained in:
Olga Nemt 2024-11-01 12:26:14 +05:00
parent f22799f1ef
commit 49c9a6107b
6 changed files with 146 additions and 22 deletions

View File

@ -1,11 +1,13 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Persistence.Models; using Persistence.Models;
using System.Linq;
namespace Persistence.Repositories; 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 TDto : class, new()
where TEntity : class, IChangeLogAbstract where TEntity : class, IChangeLogAbstract
where TChangeLogDto : ChangeLogDto<TDto>
{ {
private readonly DbContext dbContext; private readonly DbContext dbContext;
@ -13,14 +15,46 @@ public abstract class AbstractChangeLogRepository<TEntity, TRequest, TDto> : ICh
{ {
this.dbContext = dbContext; 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;
}
var result = await SaveChangesWithExceptionHandling(token);
return result;
} }
public Task<int> ClearAndInsertRange(int idUser, TRequest request, IEnumerable<TDto> dtos, CancellationToken token) 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) public Task<IEnumerable<TDto>> GetCurrent(TRequest request, CancellationToken token)
@ -38,20 +72,20 @@ public abstract class AbstractChangeLogRepository<TEntity, TRequest, TDto> : ICh
throw new NotImplementedException(); 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>(); this.dbContext.Set<TEntity>();
var db = GetDataBase(); var db = GetDataBase();
using var transaction = db.BeginTransaction(); using var transaction = db.BeginTransaction();
try try
{ {
//var result = await InsertRangeWithoutTransaction(idUser, dtos, token); var result = await InsertRangeWithoutTransaction(idUser, dtos, token);
//await transaction.CommitAsync(token); await transaction.CommitAsync(token);
//return result; return result;
} }
catch catch
{ {
//await transaction.RollbackAsync(token); await transaction.RollbackAsync(token);
throw; throw;
} }
} }
@ -73,8 +107,59 @@ public abstract class AbstractChangeLogRepository<TEntity, TRequest, TDto> : ICh
throw new NotImplementedException(); 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(); 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);
//}
} }

View File

@ -1,13 +1,29 @@
using System; using Microsoft.EntityFrameworkCore;
using Persistence.Models;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Persistence.Repositories; 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(); throw new NotImplementedException();
} }

View File

@ -6,7 +6,7 @@ namespace Persistence.Repositories;
/// ///
/// </summary> /// </summary>
/// <typeparam name="TDto"></typeparam> /// <typeparam name="TDto"></typeparam>
public interface IChangeLogRepository<TDto, TRequest> : ISyncRepository<TDto> public interface IChangeLogRepository<TDto, TChangeLogDto> : ISyncRepository<TDto>
where TDto : class where TDto : class
{ {
/// <summary> /// <summary>
@ -79,7 +79,7 @@ public interface IChangeLogRepository<TDto, TRequest> : ISyncRepository<TDto>
/// <param name="date">Фильтр по дате. Если null - вернет все записи, без привязки к дате</param> /// <param name="date">Фильтр по дате. Если null - вернет все записи, без привязки к дате</param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
Task<IEnumerable<ChangeLogDto<TDto>>> GetChangeLogForDate(TRequest request, DateOnly? date, CancellationToken token); Task<IEnumerable<TChangeLogDto>> GetChangeLogForDate(TRequest request, DateOnly? date, CancellationToken token);
/// <summary> /// <summary>
/// Получение текущих сейчас записей по параметрам /// Получение текущих сейчас записей по параметрам

View File

@ -4,7 +4,13 @@
/// ///
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <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);
} }

View File

@ -1,4 +0,0 @@
namespace Persistence.Services;
public abstract class ArchiveService : IArchiveService
{
}

View File

@ -1,4 +1,25 @@
namespace Persistence.Services; namespace Persistence.Services;
/// <summary>
///
/// </summary>
internal interface IArchiveService 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);
} }