Good structure so far
This commit is contained in:
@@ -12,61 +12,91 @@ public class ProductVariantRepository(ApplicationDbContext dbContext, IMapper ma
|
||||
private readonly ApplicationDbContext _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
||||
private readonly IMapper _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
|
||||
|
||||
public async Task<ProductVariant?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
public async Task<ProductVariant?> GetByIdAsync(Guid id, bool includeRelations = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntity = await _dbContext.Set<ProductVariantDbEntity>()
|
||||
.FirstOrDefaultAsync(v => v.Id == id, cancellationToken);
|
||||
IQueryable<ProductVariantDbEntity> query = _dbContext.ProductVariants;
|
||||
|
||||
return dbEntity != null ? _mapper.Map<ProductVariant>(dbEntity) : null;
|
||||
if (includeRelations)
|
||||
query = query.Include(v => v.Product)
|
||||
.ThenInclude(p => p.Type);
|
||||
|
||||
var dbEntity = await query.FirstOrDefaultAsync(v => v.Id == id, cancellationToken);
|
||||
|
||||
return dbEntity == null ? null : _mapper.Map<ProductVariant>(dbEntity);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ProductVariant>> GetByProductIdAsync(Guid productId, CancellationToken cancellationToken = default)
|
||||
public async Task<IEnumerable<ProductVariant>> GetAllAsync(bool includeRelations = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntities = await _dbContext.Set<ProductVariantDbEntity>()
|
||||
.Where(v => v.ProductId == productId)
|
||||
.ToListAsync(cancellationToken);
|
||||
IQueryable<ProductVariantDbEntity> query = _dbContext.ProductVariants;
|
||||
|
||||
if (includeRelations)
|
||||
query = query.Include(v => v.Product)
|
||||
.ThenInclude(p => p.Type);
|
||||
|
||||
var dbEntities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
return _mapper.Map<IEnumerable<ProductVariant>>(dbEntities);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ProductVariant>> GetByProductIdAsync(Guid productId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntities = await _dbContext.ProductVariants
|
||||
.Where(v => v.ProductId == productId)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return _mapper.Map<IEnumerable<ProductVariant>>(dbEntities);
|
||||
}
|
||||
|
||||
public async Task<ProductVariant> AddAsync(ProductVariant variant, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntity = _mapper.Map<ProductVariantDbEntity>(variant);
|
||||
|
||||
dbEntity.Product = await _dbContext.Products.FindAsync([variant.ProductId], cancellationToken)
|
||||
?? throw new InvalidOperationException($"Product with ID {variant.ProductId} not found");
|
||||
|
||||
_dbContext.ProductVariants.Add(dbEntity);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return _mapper.Map<ProductVariant>(dbEntity);
|
||||
}
|
||||
|
||||
public async Task<ProductVariant> UpdateAsync(ProductVariant variant, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var existingEntity = await _dbContext.ProductVariants
|
||||
.FindAsync([variant.Id], cancellationToken);
|
||||
|
||||
if (existingEntity == null) throw new KeyNotFoundException($"ProductVariant with ID {variant.Id} not found");
|
||||
|
||||
_mapper.Map(variant, existingEntity);
|
||||
|
||||
if (existingEntity.ProductId != variant.ProductId)
|
||||
{
|
||||
existingEntity.Product = await _dbContext.Products.FindAsync([variant.ProductId], cancellationToken)
|
||||
?? throw new InvalidOperationException($"Product with ID {variant.ProductId} not found");
|
||||
existingEntity.ProductId = variant.ProductId;
|
||||
}
|
||||
|
||||
existingEntity.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return _mapper.Map<ProductVariant>(existingEntity);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = await _dbContext.ProductVariants.FindAsync([id], cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_dbContext.ProductVariants.Remove(entity);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> ExistsAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _dbContext.Set<ProductVariantDbEntity>()
|
||||
.AnyAsync(v => v.Id == id, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task AddAsync(ProductVariant productVariant, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(productVariant);
|
||||
|
||||
var dbEntity = _mapper.Map<ProductVariantDbEntity>(productVariant);
|
||||
await _dbContext.Set<ProductVariantDbEntity>().AddAsync(dbEntity, cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(ProductVariant productVariant, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(productVariant);
|
||||
|
||||
var dbEntity = await _dbContext.Set<ProductVariantDbEntity>()
|
||||
.FirstOrDefaultAsync(v => v.Id == productVariant.Id, cancellationToken);
|
||||
|
||||
if (dbEntity == null)
|
||||
throw new KeyNotFoundException($"ProductVariant with ID {productVariant.Id} not found");
|
||||
|
||||
_mapper.Map(productVariant, dbEntity);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntity = await _dbContext.Set<ProductVariantDbEntity>()
|
||||
.FindAsync([id], cancellationToken);
|
||||
|
||||
if (dbEntity == null)
|
||||
return;
|
||||
|
||||
_dbContext.Set<ProductVariantDbEntity>().Remove(dbEntity);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
return await _dbContext.ProductVariants.AnyAsync(v => v.Id == id, cancellationToken);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user