Path: redb/redb.Templates/templates/redb-bff/RedbBff.Backend/Controllers/CategoriesController.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-bff/RedbBff.Backend/Controllers/CategoriesController.cs ---- using redb.Core.Models.Contracts; using redb.Core.Models.Entities; using redb.Route.Controllers.Attributes; using RedbBff.Backend.Props; using RedbBff.Models; namespace RedbBff.Backend.Controllers; /// The category tree: every root with its subtree, add a node, delete a subtree. [Route("/api/categories")] public sealed class CategoriesController : ApiController { /// The top-level nodes from a tree query, each loaded with its subtree. [HttpGet] public async Task> Tree() { var redb = Redb; var roots = await redb.TreeQuery().WhereRoots().ToListAsync(); var result = new List(); foreach (var root in roots) result.Add(ToNode(await redb.LoadTreeAsync(root.Id))); return result; } /// A top-level node, or a child of ParentId. [HttpPost] public async Task Create([FromBody] NewCategory request) { if (Invalid(request) is { } error) return error; var redb = Redb; var node = new TreeRedbObject { Name = request.Name.Trim(), Props = new Category() }; if (request.ParentId is { } parentId) { var parent = await redb.LoadAsync(parentId); if (parent is null) return NotFound($"Category {parentId} was not found."); await redb.CreateChildAsync(node, parent); } else { await redb.SaveAsync(node); } return Status(201, new CategoryNode { Id = node.Id, Name = node.Name ?? "" }); } /// Deleting a node deletes its whole subtree. [HttpDelete("{id}")] public async Task Delete([FromRoute("id")] long id) => await Redb.DeleteAsync(id) ? null : NotFound($"Category {id} was not found."); private static CategoryNode ToNode(ITreeRedbObject node) => new() { Id = node.Id, Name = node.Name ?? "", Children = node.Children.Select(ToNode).ToList(), }; }