Repositories + some models

This commit is contained in:
lumijiez
2025-05-27 12:28:12 +03:00
parent 6fedaf9737
commit 73269bd42a
26 changed files with 416 additions and 3133 deletions

View File

@@ -0,0 +1,111 @@
using Microsoft.EntityFrameworkCore;
using Printbase.Domain.Entities.Product;
using Printbase.Domain.Repositories;
using Printbase.Infrastructure.Database;
namespace Printbase.Infrastructure.Repositories;
public class CategoryRepository(ApplicationDbContext context) : ICategoryRepository
{
public async Task<Category?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
{
return await context.Categories
.FirstOrDefaultAsync(c => c.Id == id, cancellationToken);
}
public async Task<Category?> GetByIdWithSubCategoriesAsync(Guid id, CancellationToken cancellationToken = default)
{
return await context.Categories
.Include(c => c.SubCategories.Where(sc => sc.IsActive))
.FirstOrDefaultAsync(c => c.Id == id, cancellationToken);
}
public async Task<Category?> GetByIdWithProductsAsync(Guid id, CancellationToken cancellationToken = default)
{
return await context.Categories
.Include(c => c.Products.Where(p => p.IsActive))
.FirstOrDefaultAsync(c => c.Id == id, cancellationToken);
}
public async Task<IEnumerable<Category>> GetAllAsync(CancellationToken cancellationToken = default)
{
return await context.Categories
.OrderBy(c => c.SortOrder)
.ThenBy(c => c.Name)
.ToListAsync(cancellationToken);
}
public async Task<IEnumerable<Category>> GetActiveAsync(CancellationToken cancellationToken = default)
{
return await context.Categories
.Where(c => c.IsActive)
.OrderBy(c => c.SortOrder)
.ThenBy(c => c.Name)
.ToListAsync(cancellationToken);
}
public async Task<IEnumerable<Category>> GetRootCategoriesAsync(CancellationToken cancellationToken = default)
{
return await context.Categories
.Where(c => c.ParentCategoryId == null && c.IsActive)
.OrderBy(c => c.SortOrder)
.ThenBy(c => c.Name)
.ToListAsync(cancellationToken);
}
public async Task<IEnumerable<Category>> GetSubCategoriesAsync(Guid parentCategoryId, CancellationToken cancellationToken = default)
{
return await context.Categories
.Where(c => c.ParentCategoryId == parentCategoryId && c.IsActive)
.OrderBy(c => c.SortOrder)
.ThenBy(c => c.Name)
.ToListAsync(cancellationToken);
}
public async Task<Category> AddAsync(Category category, CancellationToken cancellationToken = default)
{
category.Id = Guid.NewGuid();
category.CreatedAt = DateTime.UtcNow;
category.ModifiedAt = DateTime.UtcNow;
context.Categories.Add(category);
await context.SaveChangesAsync(cancellationToken);
return category;
}
public async Task<Category> UpdateAsync(Category category, CancellationToken cancellationToken = default)
{
category.ModifiedAt = DateTime.UtcNow;
context.Categories.Update(category);
await context.SaveChangesAsync(cancellationToken);
return category;
}
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
{
var category = await GetByIdAsync(id, cancellationToken);
if (category != null)
{
context.Categories.Remove(category);
await context.SaveChangesAsync(cancellationToken);
}
}
public async Task<bool> ExistsAsync(Guid id, CancellationToken cancellationToken = default)
{
return await context.Categories
.AnyAsync(c => c.Id == id, cancellationToken);
}
public async Task<bool> HasSubCategoriesAsync(Guid categoryId, CancellationToken cancellationToken = default)
{
return await context.Categories
.AnyAsync(c => c.ParentCategoryId == categoryId, cancellationToken);
}
public async Task<bool> HasProductsAsync(Guid categoryId, CancellationToken cancellationToken = default)
{
return await context.Products
.AnyAsync(p => p.CategoryId == categoryId, cancellationToken);
}
}

View File

@@ -0,0 +1,155 @@
using Microsoft.EntityFrameworkCore;
using Printbase.Domain.Common.Models;
using Printbase.Domain.Entities.Product;
using Printbase.Domain.Repositories;
using Printbase.Infrastructure.Database;
namespace Printbase.Infrastructure.Repositories;
public class ProductRepository(ApplicationDbContext context) : IProductRepository
{
public async Task<Product?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
{
return await context.Products
.FirstOrDefaultAsync(p => p.Id == id, cancellationToken);
}
public async Task<Product?> GetByIdWithVariantsAsync(Guid id, CancellationToken cancellationToken = default)
{
return await context.Products
.Include(p => p.ProductVariants.Where(pv => pv.IsActive))
.FirstOrDefaultAsync(p => p.Id == id, cancellationToken);
}
public async Task<Product?> GetByIdWithCategoryAsync(Guid id, CancellationToken cancellationToken = default)
{
return await context.Products
.Include(p => p.Category)
.FirstOrDefaultAsync(p => p.Id == id, cancellationToken);
}
public async Task<Product?> GetByIdFullAsync(Guid id, CancellationToken cancellationToken = default)
{
return await context.Products
.Include(p => p.Category)
.Include(p => p.ProductVariants.Where(pv => pv.IsActive))
.FirstOrDefaultAsync(p => p.Id == id, cancellationToken);
}
public async Task<PagedResult<Product>> GetPagedAsync(ProductFilterParameters filterParameters, CancellationToken cancellationToken = default)
{
var query = context.Products
.Include(p => p.Category)
.AsQueryable();
if (filterParameters.IsActive.HasValue)
{
query = query.Where(p => p.IsActive == filterParameters.IsActive.Value);
}
if (!string.IsNullOrEmpty(filterParameters.SearchTerm))
{
query = query.Where(p => p.Name.Contains(filterParameters.SearchTerm) ||
(p.Description != null && p.Description.Contains(filterParameters.SearchTerm)));
}
if (filterParameters.CategoryId.HasValue)
{
query = query.Where(p => p.CategoryId == filterParameters.CategoryId.Value);
}
if (filterParameters.MinPrice.HasValue)
{
query = query.Where(p => p.BasePrice >= filterParameters.MinPrice.Value);
}
if (filterParameters.MaxPrice.HasValue)
{
query = query.Where(p => p.BasePrice <= filterParameters.MaxPrice.Value);
}
if (filterParameters.IsCustomizable.HasValue)
{
query = query.Where(p => p.IsCustomizable == filterParameters.IsCustomizable.Value);
}
query = filterParameters.SortBy.ToLower() switch
{
"price" => filterParameters.SortDirection.ToUpper() == "DESC"
? query.OrderByDescending(p => p.BasePrice)
: query.OrderBy(p => p.BasePrice),
"name" => filterParameters.SortDirection.ToUpper() == "DESC"
? query.OrderByDescending(p => p.Name)
: query.OrderBy(p => p.Name),
_ => query.OrderBy(p => p.Name)
};
var totalCount = await query.CountAsync(cancellationToken);
var items = await query
.Skip((filterParameters.PageNumber - 1) * filterParameters.PageSize)
.Take(filterParameters.PageSize)
.ToListAsync(cancellationToken);
return new PagedResult<Product>
{
Items = items,
TotalCount = totalCount,
PageNumber = filterParameters.PageNumber,
PageSize = filterParameters.PageSize
};
}
public async Task<IEnumerable<Product>> GetByCategoryAsync(Guid categoryId, CancellationToken cancellationToken = default)
{
return await context.Products
.Include(p => p.Category)
.Where(p => p.CategoryId == categoryId && p.IsActive)
.OrderBy(p => p.Name)
.ToListAsync(cancellationToken);
}
public async Task<IEnumerable<Product>> GetCustomizableAsync(CancellationToken cancellationToken = default)
{
return await context.Products
.Include(p => p.Category)
.Where(p => p.IsCustomizable && p.IsActive)
.OrderBy(p => p.Name)
.ToListAsync(cancellationToken);
}
public async Task<Product> AddAsync(Product product, CancellationToken cancellationToken = default)
{
product.Id = Guid.NewGuid();
product.CreatedAt = DateTime.UtcNow;
product.ModifiedAt = DateTime.UtcNow;
context.Products.Add(product);
await context.SaveChangesAsync(cancellationToken);
return product;
}
public async Task<Product> UpdateAsync(Product product, CancellationToken cancellationToken = default)
{
product.ModifiedAt = DateTime.UtcNow;
context.Products.Update(product);
await context.SaveChangesAsync(cancellationToken);
return product;
}
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
{
var product = await GetByIdAsync(id, cancellationToken);
if (product != null)
{
context.Products.Remove(product);
await context.SaveChangesAsync(cancellationToken);
}
}
public async Task<bool> ExistsAsync(Guid id, CancellationToken cancellationToken = default)
{
return await context.Products
.AnyAsync(p => p.Id == id, cancellationToken);
}
}