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

@@ -8,6 +8,7 @@
<ItemGroup>
<Folder Include="Abstractions\" />
<Folder Include="Common\" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,13 @@
using Printbase.Domain.Entities.Products;
namespace Printbase.Domain.Repositories;
public interface IProductGroupRepository
{
Task<ProductGroup?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
Task<IEnumerable<ProductGroup>> GetAllAsync(CancellationToken cancellationToken = default);
Task<bool> ExistsAsync(Guid id, CancellationToken cancellationToken = default);
Task AddAsync(ProductGroup productGroup, CancellationToken cancellationToken = default);
Task UpdateAsync(ProductGroup productGroup, CancellationToken cancellationToken = default);
Task DeleteAsync(Guid id, CancellationToken cancellationToken = default);
}

View File

@@ -0,0 +1,14 @@
using Printbase.Domain.Entities.Products;
namespace Printbase.Domain.Repositories;
public interface IProductRepository
{
Task<Product?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
Task<IEnumerable<Product>> GetAllAsync(CancellationToken cancellationToken = default);
Task<IEnumerable<Product>> GetByTypeIdAsync(Guid typeId, CancellationToken cancellationToken = default);
Task<bool> ExistsAsync(Guid id, CancellationToken cancellationToken = default);
Task AddAsync(Product product, CancellationToken cancellationToken = default);
Task UpdateAsync(Product product, CancellationToken cancellationToken = default);
Task DeleteAsync(Guid id, CancellationToken cancellationToken = default);
}

View File

@@ -0,0 +1,14 @@
using Printbase.Domain.Entities.Products;
namespace Printbase.Domain.Repositories;
public interface IProductTypeRepository
{
Task<ProductType?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
Task<IEnumerable<ProductType>> GetAllAsync(CancellationToken cancellationToken = default);
Task<IEnumerable<ProductType>> GetByGroupIdAsync(Guid groupId, CancellationToken cancellationToken = default);
Task<bool> ExistsAsync(Guid id, CancellationToken cancellationToken = default);
Task AddAsync(ProductType productType, CancellationToken cancellationToken = default);
Task UpdateAsync(ProductType productType, CancellationToken cancellationToken = default);
Task DeleteAsync(Guid id, CancellationToken cancellationToken = default);
}

View File

@@ -0,0 +1,13 @@
using Printbase.Domain.Entities.Products;
namespace Printbase.Domain.Repositories;
public interface IProductVariantRepository
{
Task<ProductVariant?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
Task<IEnumerable<ProductVariant>> GetByProductIdAsync(Guid productId, CancellationToken cancellationToken = default);
Task<bool> ExistsAsync(Guid id, CancellationToken cancellationToken = default);
Task AddAsync(ProductVariant productVariant, CancellationToken cancellationToken = default);
Task UpdateAsync(ProductVariant productVariant, CancellationToken cancellationToken = default);
Task DeleteAsync(Guid id, CancellationToken cancellationToken = default);
}

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

View File

@@ -10,4 +10,8 @@
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Printbase.Infrastructure\Printbase.Infrastructure.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,41 +1,34 @@
var builder = WebApplication.CreateBuilder(args);
using Microsoft.EntityFrameworkCore;
using Printbase.Domain.Repositories;
using Printbase.Infrastructure.Database;
using Printbase.Infrastructure.Mapping;
using Printbase.Infrastructure.Repositories;
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
var configuration = builder.Configuration;
services.AddOpenApi();
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
configuration.GetConnectionString("DefaultConnection"),
b => b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName)));
services.AddAutoMapper(typeof(ProductMappingProfile).Assembly);
services.AddScoped<IProductRepository, ProductRepository>();
services.AddScoped<IProductVariantRepository, ProductVariantRepository>();
services.AddScoped<IProductTypeRepository, ProductTypeRepository>();
services.AddScoped<IProductGroupRepository, ProductGroupRepository>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
var summaries = new[]
else
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast");
app.Run();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
app.UseHttpsRedirection();
}
app.Run();