Add repositories
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Printbase.Domain.Entities.Products;
|
||||
using Printbase.Domain.Repositories;
|
||||
using Printbase.Infrastructure.Database;
|
||||
using Printbase.Infrastructure.DbEntities.Products;
|
||||
|
||||
namespace Printbase.Infrastructure.Repositories;
|
||||
|
||||
public class ProductGroupRepository(ApplicationDbContext dbContext, IMapper mapper) : IProductGroupRepository
|
||||
{
|
||||
private readonly ApplicationDbContext _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
||||
private readonly IMapper _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
|
||||
|
||||
public async Task<ProductGroup?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntity = await _dbContext.Set<ProductGroupDbEntity>()
|
||||
.Include(g => g.Types)
|
||||
.FirstOrDefaultAsync(g => g.Id == id, cancellationToken);
|
||||
|
||||
return dbEntity != null ? _mapper.Map<ProductGroup>(dbEntity) : null;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ProductGroup>> GetAllAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntities = await _dbContext.Set<ProductGroupDbEntity>()
|
||||
.Include(g => g.Types)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return _mapper.Map<IEnumerable<ProductGroup>>(dbEntities);
|
||||
}
|
||||
|
||||
public async Task<bool> ExistsAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _dbContext.Set<ProductGroupDbEntity>()
|
||||
.AnyAsync(g => g.Id == id, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task AddAsync(ProductGroup productGroup, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(productGroup);
|
||||
|
||||
var dbEntity = _mapper.Map<ProductGroupDbEntity>(productGroup);
|
||||
await _dbContext.Set<ProductGroupDbEntity>().AddAsync(dbEntity, cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(ProductGroup productGroup, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(productGroup);
|
||||
|
||||
var dbEntity = await _dbContext.Set<ProductGroupDbEntity>()
|
||||
.Include(g => g.Types)
|
||||
.FirstOrDefaultAsync(g => g.Id == productGroup.Id, cancellationToken);
|
||||
|
||||
if (dbEntity == null)
|
||||
throw new KeyNotFoundException($"ProductGroup with ID {productGroup.Id} not found");
|
||||
|
||||
_mapper.Map(productGroup, dbEntity);
|
||||
|
||||
var existingTypeIds = dbEntity.Types.Select(t => t.Id).ToList();
|
||||
var updatedTypeIds = productGroup.Types.Select(t => t.Id).ToList();
|
||||
|
||||
var typesToRemove = dbEntity.Types.Where(t => !updatedTypeIds.Contains(t.Id)).ToList();
|
||||
foreach (var type in typesToRemove)
|
||||
{
|
||||
dbEntity.Types.Remove(type);
|
||||
}
|
||||
|
||||
foreach (var type in productGroup.Types)
|
||||
{
|
||||
if (existingTypeIds.Contains(type.Id)) continue;
|
||||
var newType = _mapper.Map<ProductTypeDbEntity>(type);
|
||||
dbEntity.Types.Add(newType);
|
||||
}
|
||||
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntity = await _dbContext.Set<ProductGroupDbEntity>()
|
||||
.FindAsync([id], cancellationToken);
|
||||
|
||||
if (dbEntity == null)
|
||||
return;
|
||||
|
||||
_dbContext.Set<ProductGroupDbEntity>().Remove(dbEntity);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
100
src/Printbase.Infrastructure/Repositories/ProductRepository.cs
Normal file
100
src/Printbase.Infrastructure/Repositories/ProductRepository.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Printbase.Domain.Entities.Products;
|
||||
using Printbase.Domain.Repositories;
|
||||
using Printbase.Infrastructure.Database;
|
||||
using Printbase.Infrastructure.DbEntities.Products;
|
||||
|
||||
namespace Printbase.Infrastructure.Repositories;
|
||||
|
||||
public class ProductRepository(ApplicationDbContext dbContext, IMapper mapper) : IProductRepository
|
||||
{
|
||||
private readonly ApplicationDbContext _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
||||
private readonly IMapper _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
|
||||
|
||||
public async Task<Product?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntity = await _dbContext.Set<ProductDbEntity>()
|
||||
.Include(p => p.Variants)
|
||||
.FirstOrDefaultAsync(p => p.Id == id, cancellationToken);
|
||||
|
||||
return dbEntity != null ? _mapper.Map<Product>(dbEntity) : null;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Product>> GetAllAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntities = await _dbContext.Set<ProductDbEntity>()
|
||||
.Include(p => p.Variants)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return _mapper.Map<IEnumerable<Product>>(dbEntities);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Product>> GetByTypeIdAsync(Guid typeId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntities = await _dbContext.Set<ProductDbEntity>()
|
||||
.Include(p => p.Variants)
|
||||
.Where(p => p.TypeId == typeId)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return _mapper.Map<IEnumerable<Product>>(dbEntities);
|
||||
}
|
||||
|
||||
public async Task<bool> ExistsAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _dbContext.Set<ProductDbEntity>()
|
||||
.AnyAsync(p => p.Id == id, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task AddAsync(Product product, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(product);
|
||||
|
||||
var dbEntity = _mapper.Map<ProductDbEntity>(product);
|
||||
await _dbContext.Set<ProductDbEntity>().AddAsync(dbEntity, cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(Product product, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(product);
|
||||
|
||||
var dbEntity = await _dbContext.Set<ProductDbEntity>()
|
||||
.Include(p => p.Variants)
|
||||
.FirstOrDefaultAsync(p => p.Id == product.Id, cancellationToken);
|
||||
|
||||
if (dbEntity == null)
|
||||
throw new KeyNotFoundException($"Product with ID {product.Id} not found");
|
||||
|
||||
_mapper.Map(product, dbEntity);
|
||||
|
||||
var existingVariantIds = dbEntity.Variants.Select(v => v.Id).ToList();
|
||||
var updatedVariantIds = product.Variants.Select(v => v.Id).ToList();
|
||||
|
||||
var variantsToRemove = dbEntity.Variants.Where(v => !updatedVariantIds.Contains(v.Id)).ToList();
|
||||
foreach (var variant in variantsToRemove)
|
||||
{
|
||||
dbEntity.Variants.Remove(variant);
|
||||
}
|
||||
|
||||
foreach (var variant in product.Variants)
|
||||
{
|
||||
if (existingVariantIds.Contains(variant.Id)) continue;
|
||||
var newVariant = _mapper.Map<ProductVariantDbEntity>(variant);
|
||||
dbEntity.Variants.Add(newVariant);
|
||||
}
|
||||
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntity = await _dbContext.Set<ProductDbEntity>()
|
||||
.FindAsync([id], cancellationToken);
|
||||
|
||||
if (dbEntity == null) return;
|
||||
|
||||
_dbContext.Set<ProductDbEntity>().Remove(dbEntity);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Printbase.Domain.Entities.Products;
|
||||
using Printbase.Domain.Repositories;
|
||||
using Printbase.Infrastructure.Database;
|
||||
using Printbase.Infrastructure.DbEntities.Products;
|
||||
|
||||
namespace Printbase.Infrastructure.Repositories;
|
||||
|
||||
public class ProductTypeRepository(ApplicationDbContext dbContext, IMapper mapper) : IProductTypeRepository
|
||||
{
|
||||
private readonly ApplicationDbContext _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
||||
private readonly IMapper _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
|
||||
|
||||
public async Task<ProductType?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntity = await _dbContext.Set<ProductTypeDbEntity>()
|
||||
.FirstOrDefaultAsync(t => t.Id == id, cancellationToken);
|
||||
|
||||
return dbEntity != null ? _mapper.Map<ProductType>(dbEntity) : null;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ProductType>> GetAllAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntities = await _dbContext.Set<ProductTypeDbEntity>()
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return _mapper.Map<IEnumerable<ProductType>>(dbEntities);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ProductType>> GetByGroupIdAsync(Guid groupId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntities = await _dbContext.Set<ProductTypeDbEntity>()
|
||||
.Where(t => t.GroupId == groupId)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return _mapper.Map<IEnumerable<ProductType>>(dbEntities);
|
||||
}
|
||||
|
||||
public async Task<bool> ExistsAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _dbContext.Set<ProductTypeDbEntity>()
|
||||
.AnyAsync(t => t.Id == id, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task AddAsync(ProductType productType, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(productType);
|
||||
|
||||
var dbEntity = _mapper.Map<ProductTypeDbEntity>(productType);
|
||||
await _dbContext.Set<ProductTypeDbEntity>().AddAsync(dbEntity, cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(ProductType productType, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(productType);
|
||||
|
||||
var dbEntity = await _dbContext.Set<ProductTypeDbEntity>()
|
||||
.FirstOrDefaultAsync(t => t.Id == productType.Id, cancellationToken);
|
||||
|
||||
if (dbEntity == null)
|
||||
throw new KeyNotFoundException($"ProductType with ID {productType.Id} not found");
|
||||
|
||||
_mapper.Map(productType, dbEntity);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntity = await _dbContext.Set<ProductTypeDbEntity>()
|
||||
.FindAsync([id], cancellationToken);
|
||||
|
||||
if (dbEntity == null)
|
||||
return;
|
||||
|
||||
_dbContext.Set<ProductTypeDbEntity>().Remove(dbEntity);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Printbase.Domain.Entities.Products;
|
||||
using Printbase.Domain.Repositories;
|
||||
using Printbase.Infrastructure.Database;
|
||||
using Printbase.Infrastructure.DbEntities.Products;
|
||||
|
||||
namespace Printbase.Infrastructure.Repositories;
|
||||
|
||||
public class ProductVariantRepository(ApplicationDbContext dbContext, IMapper mapper) : IProductVariantRepository
|
||||
{
|
||||
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)
|
||||
{
|
||||
var dbEntity = await _dbContext.Set<ProductVariantDbEntity>()
|
||||
.FirstOrDefaultAsync(v => v.Id == id, cancellationToken);
|
||||
|
||||
return dbEntity != null ? _mapper.Map<ProductVariant>(dbEntity) : null;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ProductVariant>> GetByProductIdAsync(Guid productId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dbEntities = await _dbContext.Set<ProductVariantDbEntity>()
|
||||
.Where(v => v.ProductId == productId)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return _mapper.Map<IEnumerable<ProductVariant>>(dbEntities);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user