Сохранение данных с возможностью отката транзации в методе Insert в ChangeLogRepositoryAbstract

This commit is contained in:
Olga Nemt 2024-08-09 16:04:20 +03:00
parent 770a370d7f
commit d6b4f90f4c

View File

@ -117,6 +117,22 @@ public abstract class ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> : ICh
} }
public async Task<int> InsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token) public async Task<int> InsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
{
using var transaction = db.Database.BeginTransaction();
try
{
var result = await InsertRangeWithoutTransaction(idUser, dtos, token);
await transaction.CommitAsync(token);
return result;
}
catch
{
await transaction.RollbackAsync(token);
throw;
}
}
private async Task<int> InsertRangeWithoutTransaction(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
{ {
var result = 0; var result = 0;
if (dtos.Any()) if (dtos.Any())
@ -138,7 +154,8 @@ public abstract class ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> : ICh
result += await SaveChangesWithExceptionHandling(token); result += await SaveChangesWithExceptionHandling(token);
} }
return result;
return result;
} }
public async Task<int> UpdateRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token) public async Task<int> UpdateRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
@ -248,7 +265,7 @@ public abstract class ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> : ICh
try try
{ {
result += await Clear(idUser, request, token); result += await Clear(idUser, request, token);
result += await InsertRange(idUser, dtos, token); result += await InsertRangeWithoutTransaction(idUser, dtos, token);
await transaction.CommitAsync(token); await transaction.CommitAsync(token);
return result; return result;