using FastEndpoints; using Microsoft.EntityFrameworkCore; namespace Faben.Api.Endpoints; public class GetCategoriesEndpoint (FabenDbContext context) : EndpointWithMapping, List> { public override void Configure() { Get("categories"); //ResponseCache(60 * 60); //cache for 60 seconds Version(1); AllowAnonymous(); } public override async Task HandleAsync(EmptyRequest req, CancellationToken c) { var categories = await context .Products .GroupBy(p => p.CategoryId) .Select(p => new CategoryDto { Id = p.Key, Name = p.FirstOrDefault().Category.Name, SeoFriendlyName = p.FirstOrDefault().Category.SeoFriendlyName, Image = p.FirstOrDefault().Category.Image, ProductCount = p.Count() }) .AsNoTracking() .OrderBy(p => p.Name) .ToListAsync(cancellationToken: c); await SendOkAsync(categories, c); } public override List MapFromEntity(List e) { return e.Select(c => new CategoryDto { Id = c.Id, SeoFriendlyName = c.SeoFriendlyName, Name = c.Name, }).ToList(); } } public class CategoryDto { public Guid Id { get; set; } public string Name { get; set; } = null!; public string SeoFriendlyName { get; set; } = null!; public int ProductCount { get; set; } public string Image { get; set; } = null!; } public class ProductDto { public Guid Id { get; set; } public string Name { get; set; } = null!; public string SeoFriendlyName { get; set; } = null!; public string CategoryName { get; set; } public Guid CategoryId { get; set; } public string Image { get; set; } public string BrandLogo { get; set; } public string BrandName { get; set; } public Guid BrandId { get; set; } public string CategorySeoFriendlyName { get; set; } public string? SkuNo { get; set; } public string? PdfDrawingLink { get; set; } public string? ThreeDModelLink { get; set; } } public class GetCategoryProducts { public int PageNumber { get; set; } } public class GetCategoryProductsResponse { public List Items { get; set; } public int TotalCount { get; set; } public int PageSize { get; set; } }