using FastEndpoints; using Microsoft.EntityFrameworkCore; namespace Faben.Api.Endpoints; public class GetCategoryProductsEndpoint (FabenDbContext context) : Endpoint { public override void Configure() { Get("categories/{name}/products"); Version(1); AllowAnonymous(); } public override async Task HandleAsync(GetCategoryProducts req, CancellationToken c) { var pageNumber = req.PageNumber is 1 or 0 ? 0 : req.PageNumber - 1; var name = Route("name"); var totalCount = await context .Products .AsNoTracking() .Where(p => p.Category.SeoFriendlyName == name) .CountAsync(); var result = await context .Products .AsNoTracking() .Where(p => p.Category.SeoFriendlyName == name) .OrderBy(p => p.Name) .Select(p => new ProductDto { Id = p.Id, CategoryId = p.CategoryId, CategoryName = p.Category.Name, CategorySeoFriendlyName = p.Category.SeoFriendlyName, SeoFriendlyName = p.SeoFriendlyName, Image = p.ProductImages.FirstOrDefault()!.Url, Name = p.Name, BrandId = p.BrandId, BrandName = p.Brand.Name, BrandLogo = p.Brand.Image, SkuNo = p.SkuNo, }) // .Skip(pageNumber * 20) // .Take(20) .ToListAsync(c); await SendOkAsync(new GetCategoryProductsResponse { Items = result, TotalCount = totalCount, PageSize = (int)Math.Ceiling((double)totalCount / 20) }, c); } }