Path: redb/redb.Templates/templates/redb-app/RedbApp.Api/Services/ProductService.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Templates/templates/redb-app/RedbApp.Api/Services/ProductService.cs ---- using System.ComponentModel.DataAnnotations; using System.Globalization; using redb.Core; using redb.Core.Models.Entities; using redb.Core.Query; using redb.Route.Abstractions; using redb.Route.Http; using RedbApp.Api.Props; using RedbApp.Models; namespace RedbApp.Api.Services; /// /// The product operations. Each method is one ProcessWithRedb step: it gets the request from the /// exchange (path parameters as headers, query parameters as query.* headers, the JSON body already /// bound to its type) and leaves the answer as the body. /// public static class ProductService { public const int PageSize = 10; /// GET /api/products?search=&inStock=&sort=&page=. Search, sorting and paging run in the database. public static async Task ListAsync(IRedbService redb, IExchange e, CancellationToken ct) { var search = e.In.GetHeader("query.search")?.Trim(); var inStockOnly = e.In.GetHeader("query.inStock") == "true"; var sort = e.In.GetHeader("query.sort") ?? "name"; var page = int.TryParse(e.In.GetHeader("query.page"), CultureInfo.InvariantCulture, out var p) ? p : 1; // The filter is built from scratch for the count and for the page, so the two queries share no state. IRedbQueryable Filtered() { var query = redb.Query(); // Name is the object's own field, so it is filtered with WhereRedb; Props fields use Where. if (!string.IsNullOrEmpty(search)) query = query.WhereRedb(x => x.Name.Contains(search)); if (inStockOnly) query = query.Where(x => x.InStock); return query; } var total = await Filtered().CountAsync(ct); var pageCount = Math.Max(1, (total + PageSize - 1) / PageSize); page = Math.Clamp(page, 1, pageCount); IRedbQueryable sorted = sort switch { "price" => Filtered().OrderBy(x => x.Price), "-price" => Filtered().OrderByDescending(x => x.Price), "category" => Filtered().OrderBy(x => x.Category), _ => Filtered().OrderByRedb(x => x.Name), }; var items = await sorted.Skip((page - 1) * PageSize).Take(PageSize).ToListAsync(ct); e.In.Body = new ProductPage { Items = items.Select(ToDto).ToList(), Total = total, Page = page, PageSize = PageSize }; } /// GET /api/products/{id}. public static async Task GetAsync(IRedbService redb, IExchange e, CancellationToken ct) { var product = await redb.LoadAsync(IdOf(e), cancellationToken: ct); e.In.Body = product is null ? NotFound(e) : ToDto(product); } /// POST /api/products: creates the product and answers 201 with it. public static async Task CreateAsync(IRedbService redb, IExchange e, CancellationToken ct) { var dto = (ProductDto)e.In.Body!; if (Invalid(e, dto)) return; var product = new RedbObject { Props = new Product() }; Apply(product, dto); await redb.SaveAsync(product, ct); e.In.Headers[HttpHeaders.ResponseCode] = 201; e.In.Body = ToDto(product); } /// PUT /api/products/{id}. With change tracking on, the save writes only the fields that changed. public static async Task UpdateAsync(IRedbService redb, IExchange e, CancellationToken ct) { var dto = (ProductDto)e.In.Body!; if (Invalid(e, dto)) return; var product = await redb.LoadAsync(IdOf(e), cancellationToken: ct); if (product is null) { e.In.Body = NotFound(e); return; } Apply(product, dto); await redb.SaveAsync(product, ct); e.In.Body = ToDto(product); } /// DELETE /api/products/{id}: 204, or 404 when there was nothing to delete. public static async Task DeleteAsync(IRedbService redb, IExchange e, CancellationToken ct) { e.In.Body = await redb.DeleteAsync(IdOf(e), ct) ? null : NotFound(e); } private static long IdOf(IExchange e) => long.TryParse(e.In.GetHeader("id"), CultureInfo.InvariantCulture, out var id) ? id : 0; private static ApiError NotFound(IExchange e) { e.In.Headers[HttpHeaders.ResponseCode] = 404; return new ApiError { Message = $"Product {e.In.GetHeader("id")} was not found." }; } // The browser validates the form, but the API cannot trust that: the same attributes are checked here. private static bool Invalid(IExchange e, ProductDto dto) { var errors = new List(); if (Validator.TryValidateObject(dto, new ValidationContext(dto), errors, validateAllProperties: true)) return false; e.In.Headers[HttpHeaders.ResponseCode] = 400; e.In.Body = new ApiError { Message = string.Join(" ", errors.Select(r => r.ErrorMessage)) }; return true; } private static void Apply(RedbObject product, ProductDto dto) { product.Name = dto.Name; product.Props!.Category = dto.Category; product.Props.Price = dto.Price; product.Props.InStock = dto.InStock; product.Props.Description = dto.Description; } private static ProductDto ToDto(RedbObject o) => new() { Id = o.Id, Name = o.Name ?? "", Category = o.Props!.Category, Price = o.Props.Price, InStock = o.Props.InStock, Description = o.Props.Description, }; }