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
|
||||
Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancellationToken = default);
|
||||
|
||||
Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancellationToken = default);
|
||||
|
||||
// READ
|
||||
Task<TEntity?> GetByIdAsync(int id, 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<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
|
||||
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);
|
||||
|
||||
@@ -25,6 +25,14 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
||||
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 ---
|
||||
|
||||
public async Task<TEntity?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
||||
@@ -44,13 +52,13 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var query = _dbSet.Where(predicate);
|
||||
|
||||
|
||||
if (skip.HasValue)
|
||||
query = query.Skip(skip.Value);
|
||||
|
||||
|
||||
if (take.HasValue)
|
||||
query = query.Take(take.Value);
|
||||
|
||||
|
||||
return await query.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
@@ -84,6 +92,59 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
||||
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 ---
|
||||
|
||||
/// <summary>
|
||||
@@ -96,7 +157,7 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
||||
TDto dto,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = await _dbSet.SingleOrDefaultAsync(predicate, cancellationToken)
|
||||
var entity = await _dbSet.SingleOrDefaultAsync(predicate, cancellationToken)
|
||||
?? throw new NotFoundException($"No {typeof(TEntity).Name} found matching the predicate.");
|
||||
Mapper.Map(dto, entity);
|
||||
await Context.SaveChangesAsync(cancellationToken);
|
||||
@@ -113,12 +174,7 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entities = await _dbSet.Where(predicate).ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
Mapper.Map(dto, entity);
|
||||
}
|
||||
|
||||
entities.ForEach(entity => Mapper.Map(dto, entity));
|
||||
await Context.SaveChangesAsync(cancellationToken);
|
||||
return entities.Count;
|
||||
}
|
||||
@@ -134,7 +190,7 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
||||
Expression<Func<TEntity, bool>> predicate,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = await _dbSet.SingleOrDefaultAsync(predicate, cancellationToken)
|
||||
var entity = await _dbSet.SingleOrDefaultAsync(predicate, cancellationToken)
|
||||
?? throw new NotFoundException($"No {typeof(TEntity).Name} found matching the predicate.");
|
||||
_dbSet.Remove(entity);
|
||||
await Context.SaveChangesAsync(cancellationToken);
|
||||
@@ -150,7 +206,6 @@ public class Repository<TEntity>(MessagingServiceDbContext Context, IMapper Mapp
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entities = await _dbSet.Where(predicate).ToListAsync(cancellationToken);
|
||||
|
||||
_dbSet.RemoveRange(entities);
|
||||
await Context.SaveChangesAsync(cancellationToken);
|
||||
return entities.Count;
|
||||
|
||||
Reference in New Issue
Block a user