Rebadge the rest of the files
This commit is contained in:
111
src/Imprink.Infrastructure/Repositories/CategoryRepository.cs
Normal file
111
src/Imprink.Infrastructure/Repositories/CategoryRepository.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using Imprink.Domain.Entities.Product;
|
||||
using Imprink.Domain.Repositories;
|
||||
using Imprink.Infrastructure.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Imprink.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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user