public class Product { public int ProductId { get; set; } public string ProductName { get; set; } = null!; public int ProductTypeId { get; set; } public string SKU { get; set; } = null!; public ProductType ProductType { get; set; } = null!; } public class ProductType { public int ProductTypeId { get; set; } public string TypeName { get; set; } = null!; public ICollection Products { get; set; } = new List(); } public class ProductListRequest { public string ProductSearchTerm { get; set; } = ""; public int PageNumber { get; set; } = 1; public int PageSize { get; set; } = 10; } public class ProductListResponse { public List InventoryItems { get; set; } = new(); public int TotalCount { get; set; } public int PageNumber { get; set; } public int PageSize { get; set; } } public class ProductListItemResponse { public int ProductId { get; set; } public string ProductName { get; set; } = null!; public int ProductTypeId { get; set; } public string SKU { get; set; } = null!; public string TypeName { get; set; } = null!; } public interface IExampleService { Task GetAllProductsAsync(ProductListRequest request, CancellationToken cancellationToken); } public class ExampleService : IExampleService { private readonly ExampleDbContext _context; public ExampleService(ExampleDbContext context) { _context = context; } public async Task GetAllProductsAsync(ProductListRequest request, CancellationToken cancellationToken) { try { // Get search from request var productSearchTerm = request.ProductSearchTerm; // Setup query var productList = _context.Products .Include(i => i.ProductType) .AsQueryable(); // Apply filters if (!string.IsNullOrEmpty(productSearchTerm)) { productList = productList.Where(i => i.ProductName.Contains(productSearchTerm)); } // Get total count var totalCount = await productList.CountAsync(cancellationToken); // Apply pagination var results = await productList .OrderBy(i => i.ProductId) .Skip((request.PageNumber - 1) * request.PageSize) .Take(request.PageSize) .Select(i => new ProductListItemResponse { ProductId = i.ProductId, ProductName = i.ProductName, ProductTypeId = i.ProductTypeId, SKU = i.SKU, TypeName = i.ProductType.TypeName }) .ToListAsync(cancellationToken); return new ProductListResponse { InventoryItems = results, TotalCount = totalCount, PageNumber = request.PageNumber, PageSize = request.PageSize }; } catch (Exception ex) { Console.WriteLine(ex.Message); return null; } } }