Path: redb/redb.Templates/templates/redb-app/RedbApp.Api/Services/CategoryService.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/CategoryService.cs
----
using System.Globalization;
using redb.Core;
using redb.Core.Models.Contracts;
using redb.Core.Models.Entities;
using redb.Route.Abstractions;
using redb.Route.Http;
using RedbApp.Api.Props;
using RedbApp.Models;
namespace RedbApp.Api.Services;
/// The category tree: every root with its subtree, add a node, delete a subtree.
public static class CategoryService
{
/// GET /api/categories: the top-level nodes from a tree query, each loaded with its subtree.
public static async Task TreeAsync(IRedbService redb, IExchange e, CancellationToken ct)
{
var roots = await redb.TreeQuery().WhereRoots().ToListAsync(ct);
var result = new List();
foreach (var root in roots)
result.Add(ToNode(await redb.LoadTreeAsync(root.Id)));
e.In.Body = result;
}
/// POST /api/categories: a top-level node, or a child of ParentId.
public static async Task CreateAsync(IRedbService redb, IExchange e, CancellationToken ct)
{
var request = (NewCategory)e.In.Body!;
if (string.IsNullOrWhiteSpace(request.Name))
{
e.In.Headers[HttpHeaders.ResponseCode] = 400;
e.In.Body = new ApiError { Message = "The name is required." };
return;
}
var node = new TreeRedbObject { Name = request.Name.Trim(), Props = new Category() };
if (request.ParentId is { } parentId)
{
var parent = await redb.LoadAsync(parentId, cancellationToken: ct);
if (parent is null)
{
e.In.Headers[HttpHeaders.ResponseCode] = 404;
e.In.Body = new ApiError { Message = $"Category {parentId} was not found." };
return;
}
await redb.CreateChildAsync(node, parent);
}
else
{
await redb.SaveAsync(node, ct);
}
e.In.Headers[HttpHeaders.ResponseCode] = 201;
e.In.Body = new CategoryNode { Id = node.Id, Name = node.Name ?? "" };
}
/// DELETE /api/categories/{id}: deleting a node deletes its whole subtree.
public static async Task DeleteAsync(IRedbService redb, IExchange e, CancellationToken ct)
{
var id = long.TryParse(e.In.GetHeader("id"), CultureInfo.InvariantCulture, out var v) ? v : 0;
if (await redb.DeleteAsync(id, ct))
{
e.In.Body = null;
return;
}
e.In.Headers[HttpHeaders.ResponseCode] = 404;
e.In.Body = new ApiError { Message = $"Category {id} was not found." };
}
private static CategoryNode ToNode(ITreeRedbObject node) => new()
{
Id = node.Id,
Name = node.Name ?? "",
Children = node.Children.Select(ToNode).ToList(),
};
}