Add repositories

This commit is contained in:
lumijiez
2025-05-04 01:09:10 +03:00
parent 91b3115073
commit 65f25318a5
13 changed files with 475 additions and 31 deletions

View File

@@ -0,0 +1,47 @@
using Microsoft.EntityFrameworkCore;
using Printbase.Infrastructure.DbEntities.Products;
namespace Printbase.Infrastructure.Database;
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : DbContext(options)
{
public DbSet<ProductDbEntity> Products { get; set; } = null!;
public DbSet<ProductVariantDbEntity> ProductVariants { get; set; } = null!;
public DbSet<ProductTypeDbEntity> ProductTypes { get; set; } = null!;
public DbSet<ProductGroupDbEntity> ProductGroups { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ProductDbEntity>()
.HasMany(p => p.Variants)
.WithOne(v => v.Product)
.HasForeignKey(v => v.ProductId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<ProductDbEntity>()
.HasOne(p => p.Type)
.WithMany(t => t.Products)
.HasForeignKey(p => p.TypeId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<ProductTypeDbEntity>()
.HasOne(t => t.Group)
.WithMany(g => g.Types)
.HasForeignKey(t => t.GroupId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<ProductDbEntity>()
.Property(p => p.Discount)
.HasPrecision(18, 2);
modelBuilder.Entity<ProductVariantDbEntity>()
.Property(v => v.Price)
.HasPrecision(18, 2);
modelBuilder.Entity<ProductVariantDbEntity>()
.Property(v => v.Discount)
.HasPrecision(18, 2);
}
}

View File

@@ -8,6 +8,8 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="14.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.4" />
</ItemGroup>
<ItemGroup>

View File

@@ -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);
}
}

View 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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}