Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,69 +37,81 @@ public async Task<bool> InsertSingle(TEntity entity)

public async Task<bool> InsertMany(IEnumerable<TEntity> entities)
{
using var transaction = await _context.Database.BeginTransactionAsync();
var strategy = _context.Database.CreateExecutionStrategy();
var result = await strategy.ExecuteAsync( async () =>
{
using var transaction = await _context.Database.BeginTransactionAsync();

await _context.AddRangeAsync(entities);
var result = await _context.SaveChangesAsync();
await _context.AddRangeAsync(entities);
var result = await _context.SaveChangesAsync();

await transaction.CommitAsync();
return result > 0;
await transaction.CommitAsync();
return result > 0;
});
return result;
}

public async Task<bool> Remove(Expression<Func<TEntity, bool>> predicate)
{

using var transaction = await _context.Database.BeginTransactionAsync();
var result = await _context.Set<TEntity>().AsNoTracking().SingleOrDefaultAsync(predicate);

if (result == null)
{
return false;
}
_context.Set<TEntity>().Remove(result);
var rowsEffected = await _context.SaveChangesAsync();
if(rowsEffected > 1)
var strategy = _context.Database.CreateExecutionStrategy();
var result = await strategy.ExecuteAsync( async () =>
{
await _context.Database.RollbackTransactionAsync();
using var transaction = await _context.Database.BeginTransactionAsync();
var result = await _context.Set<TEntity>().AsNoTracking().SingleOrDefaultAsync(predicate);

if (result == null)
{
return false;
}
_context.Set<TEntity>().Remove(result);
var rowsEffected = await _context.SaveChangesAsync();
if(rowsEffected > 1)
{
await _context.Database.RollbackTransactionAsync();

_logger.LogError("There was an error while trying to deleted despite a record being found");
throw new MultipleRecordsFoundException("Multiple Records were updated by PUT request, Changes have been Rolled-back");
}

_logger.LogError("There was an error while trying to deleted despite a record being found");
throw new MultipleRecordsFoundException("Multiple Records were updated by PUT request, Changes have been Rolled-back");
}

await _context.Database.CommitTransactionAsync();
return true;
await _context.Database.CommitTransactionAsync();
return true;
});
return result;

}

public async Task<TEntity> Update(TEntity entity, Expression<Func<TEntity, bool>> predicate)
{


using var transaction = await _context.Database.BeginTransactionAsync();

var existingEntity = await _context.Set<TEntity>().AsNoTracking().SingleOrDefaultAsync(predicate);

if (existingEntity == null)
var strategy = _context.Database.CreateExecutionStrategy();
var result = await strategy.ExecuteAsync( async () =>
{
return null;
}
_context.Update(entity);
var rowsEffected = await _context.SaveChangesAsync();


if (rowsEffected == 1)
{
await _context.Database.CommitTransactionAsync();
return entity;
}
else if (rowsEffected > 1)
{
await transaction.RollbackAsync();
_logger.LogError("Multiple Records were updated by PUT request, Changes have been Rolled-back");
using var transaction = await _context.Database.BeginTransactionAsync();

var existingEntity = await _context.Set<TEntity>().AsNoTracking().SingleOrDefaultAsync(predicate);

if (existingEntity == null)
{
return null;
}
_context.Update(entity);
var rowsEffected = await _context.SaveChangesAsync();


if (rowsEffected == 1)
{
await _context.Database.CommitTransactionAsync();
return entity;
}
else if (rowsEffected > 1)
{
await transaction.RollbackAsync();
_logger.LogError("Multiple Records were updated by PUT request, Changes have been Rolled-back");
throw new MultipleRecordsFoundException("Multiple Records were updated by PUT request, Changes have been Rolled-back");
}
_logger.LogError("No records were updated despite a record being found");
throw new MultipleRecordsFoundException("Multiple Records were updated by PUT request, Changes have been Rolled-back");
}
_logger.LogError("No records were updated despite a record being found");
throw new MultipleRecordsFoundException("Multiple Records were updated by PUT request, Changes have been Rolled-back");
});
return result;
}

}
Expand Down