Add bulk create and upsert methods to repository
Added `CreateAsync` for bulk entity creation and `UpsertAsync` and `UpsertSingleAsync` for upsert operations in `IRepository` and `Repository`. Improved error handling in `UpdateSingleAsync` and `DeleteSingleAsync` by throwing `NotFoundException` when no matching entity is found. Refactored `FindAsync` and `UpdateAsync` for better readability and performance. Cleaned up formatting in `DeleteAsync`.
This commit is contained in:
@@ -11,6 +11,8 @@ public interface IRepository<TEntity> where TEntity : class
|
|||||||
// CREATE
|
// CREATE
|
||||||
Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancellationToken = default);
|
Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
// READ
|
// READ
|
||||||
Task<TEntity?> GetByIdAsync(int id, CancellationToken cancellationToken = default);
|
Task<TEntity?> GetByIdAsync(int id, CancellationToken cancellationToken = default);
|
||||||
Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancellationToken = default);
|
Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancellationToken = default);
|
||||||
@@ -20,6 +22,11 @@ public interface IRepository<TEntity> where TEntity : class
|
|||||||
Task<int> CountAsync(Expression<Func<TEntity, bool>>? predicate = null, CancellationToken cancellationToken = default);
|
Task<int> CountAsync(Expression<Func<TEntity, bool>>? predicate = null, CancellationToken cancellationToken = default);
|
||||||
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);
|
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
// UPSERT
|
||||||
|
Task<(TEntity Entity, bool Created)> UpsertAsync<TDto>(Expression<Func<TEntity, bool>> predicate, TDto dto, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
Task<(TEntity Entity, bool Created)> UpsertSingleAsync<TDto>(Expression<Func<TEntity, bool>> predicate, TDto dto, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
// UPDATE
|
// UPDATE
|
||||||
Task UpdateSingleAsync<TDto>(Expression<Func<TEntity, bool>> predicate, TDto dto, CancellationToken cancellationToken = default);
|
Task UpdateSingleAsync<TDto>(Expression<Func<TEntity, bool>> predicate, TDto dto, CancellationToken cancellationToken = default);
|
||||||
Task<int> UpdateAsync<TDto>(Expression<Func<TEntity, bool>> predicate, TDto dto, CancellationToken cancellationToken = default);
|
Task<int> UpdateAsync<TDto>(Expression<Func<TEntity, bool>> predicate, TDto dto, CancellationToken cancellationToken = default);
|
||||||
|
|||||||
@@ -25,6 +25,14 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var entities = Mapper.Map<IEnumerable<TEntity>>(dtos);
|
||||||
|
await _dbSet.AddRangeAsync(entities, cancellationToken);
|
||||||
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
|
return entities;
|
||||||
|
}
|
||||||
|
|
||||||
// --- READ ---
|
// --- READ ---
|
||||||
|
|
||||||
public async Task<TEntity?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
public async Task<TEntity?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
||||||
@@ -84,6 +92,59 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
return await _dbSet.AnyAsync(predicate, cancellationToken);
|
return await _dbSet.AnyAsync(predicate, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- UPSERT ---
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Upsert: if no record matches the predicate, creates a new entity;
|
||||||
|
/// if one or more match, updates the FIRST match.
|
||||||
|
/// Returns the entity and a flag indicating whether it was created (true) or updated (false).
|
||||||
|
/// Auto-saves changes.
|
||||||
|
/// </summary>
|
||||||
|
public async Task<(TEntity Entity, bool Created)> UpsertAsync<TDto>(
|
||||||
|
Expression<Func<TEntity, bool>> predicate,
|
||||||
|
TDto dto,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var entity = await _dbSet.FirstOrDefaultAsync(predicate, cancellationToken);
|
||||||
|
|
||||||
|
if (entity is null)
|
||||||
|
{
|
||||||
|
entity = Mapper.Map<TEntity>(dto);
|
||||||
|
await _dbSet.AddAsync(entity, cancellationToken);
|
||||||
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
|
return (entity, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Mapper.Map(dto, entity);
|
||||||
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
|
return (entity, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Upsert (single-safe): if no record matches the predicate, creates a new entity;
|
||||||
|
/// if exactly one matches, updates it. Throws InvalidOperationException if 2+ match.
|
||||||
|
/// Auto-saves changes.
|
||||||
|
/// </summary>
|
||||||
|
public async Task<(TEntity Entity, bool Created)> UpsertSingleAsync<TDto>(
|
||||||
|
Expression<Func<TEntity, bool>> predicate,
|
||||||
|
TDto dto,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var entity = await _dbSet.SingleOrDefaultAsync(predicate, cancellationToken);
|
||||||
|
|
||||||
|
if (entity is null)
|
||||||
|
{
|
||||||
|
entity = Mapper.Map<TEntity>(dto);
|
||||||
|
await _dbSet.AddAsync(entity, cancellationToken);
|
||||||
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
|
return (entity, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Mapper.Map(dto, entity);
|
||||||
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
|
return (entity, false);
|
||||||
|
}
|
||||||
|
|
||||||
// --- UPDATE ---
|
// --- UPDATE ---
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -113,12 +174,7 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var entities = await _dbSet.Where(predicate).ToListAsync(cancellationToken);
|
var entities = await _dbSet.Where(predicate).ToListAsync(cancellationToken);
|
||||||
|
entities.ForEach(entity => Mapper.Map(dto, entity));
|
||||||
foreach (var entity in entities)
|
|
||||||
{
|
|
||||||
Mapper.Map(dto, entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
await Context.SaveChangesAsync(cancellationToken);
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
return entities.Count;
|
return entities.Count;
|
||||||
}
|
}
|
||||||
@@ -150,7 +206,6 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
|||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var entities = await _dbSet.Where(predicate).ToListAsync(cancellationToken);
|
var entities = await _dbSet.Where(predicate).ToListAsync(cancellationToken);
|
||||||
|
|
||||||
_dbSet.RemoveRange(entities);
|
_dbSet.RemoveRange(entities);
|
||||||
await Context.SaveChangesAsync(cancellationToken);
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
return entities.Count;
|
return entities.Count;
|
||||||
|
|||||||
Reference in New Issue
Block a user