using Imprink.Domain.Entities; using Imprink.Domain.Repositories; using Imprink.Infrastructure.Database; using Microsoft.EntityFrameworkCore; namespace Imprink.Infrastructure.Repositories; public class CategoryRepository(ApplicationDbContext context) : ICategoryRepository { public async Task GetByIdAsync(Guid id, CancellationToken cancellationToken = default) { return await context.Categories .FirstOrDefaultAsync(c => c.Id == id, cancellationToken); } public async Task 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 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> GetAllAsync(CancellationToken cancellationToken = default) { return await context.Categories .OrderBy(c => c.SortOrder) .ThenBy(c => c.Name) .ToListAsync(cancellationToken); } public async Task> 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> 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> 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 Task AddAsync(Category category, CancellationToken cancellationToken = default) { category.Id = Guid.NewGuid(); category.CreatedAt = DateTime.UtcNow; category.ModifiedAt = DateTime.UtcNow; context.Categories.Add(category); return Task.FromResult(category); } public Task UpdateAsync(Category category, CancellationToken cancellationToken = default) { category.ModifiedAt = DateTime.UtcNow; context.Categories.Update(category); return Task.FromResult(category); } public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default) { var category = await GetByIdAsync(id, cancellationToken); if (category != null) { context.Categories.Remove(category); } } public async Task ExistsAsync(Guid id, CancellationToken cancellationToken = default) { return await context.Categories .AnyAsync(c => c.Id == id, cancellationToken); } public async Task HasSubCategoriesAsync(Guid categoryId, CancellationToken cancellationToken = default) { return await context.Categories .AnyAsync(c => c.ParentCategoryId == categoryId, cancellationToken); } public async Task HasProductsAsync(Guid categoryId, CancellationToken cancellationToken = default) { return await context.Products .AnyAsync(p => p.CategoryId == categoryId, cancellationToken); } }