Path: redb/redb.Templates/templates/redb-app/RedbApp.Api/Routes/ApiRouteBuilder.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/Routes/ApiRouteBuilder.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Http.Rest;
using redb.Route.RedbCore.Extensions;
using RedbApp.Api.Services;
using RedbApp.Models;
namespace RedbApp.Api.Routes;
///
/// The product and category API. Every verb is a route of its own: the REST declaration binds the request
/// and forwards to a direct: endpoint, where one ProcessWithRedb step does the work. Code in the
/// same process can send to the same direct: endpoints with a ProducerTemplate.
/// Every call needs the token from POST /api/auth/login in Authorization: Bearer ....
/// An OpenAPI document of each declaration is served at /api/products/openapi.json and
/// /api/categories/openapi.json.
///
public sealed class ApiRouteBuilder(ModuleSettings settings) : RouteBuilder
{
protected override void Configure()
{
this.Rest("/api/products", o => ApiOptions.Apply(o, settings, requireToken: true))
.Get().Produces("application/json").OutType().To("direct:products-list")
.Get("/{id}").Produces("application/json").OutType().To("direct:products-get")
.Post().Consumes("application/json").Type().OutType().To("direct:products-create")
.Put("/{id}").Consumes("application/json").Type().OutType().To("direct:products-update")
.Delete("/{id}").To("direct:products-delete");
From("direct:products-list").RouteId("products-list").ProcessWithRedb(ProductService.ListAsync);
From("direct:products-get").RouteId("products-get").ProcessWithRedb(ProductService.GetAsync);
From("direct:products-create").RouteId("products-create").ProcessWithRedb(ProductService.CreateAsync);
From("direct:products-update").RouteId("products-update").ProcessWithRedb(ProductService.UpdateAsync);
From("direct:products-delete").RouteId("products-delete").ProcessWithRedb(ProductService.DeleteAsync);
this.Rest("/api/categories", o => ApiOptions.Apply(o, settings, requireToken: true))
.Get().Produces("application/json").OutType>().To("direct:categories-tree")
.Post().Consumes("application/json").Type().OutType().To("direct:categories-create")
.Delete("/{id}").To("direct:categories-delete");
From("direct:categories-tree").RouteId("categories-tree").ProcessWithRedb(CategoryService.TreeAsync);
From("direct:categories-create").RouteId("categories-create").ProcessWithRedb(CategoryService.CreateAsync);
// Deleting a subtree removes several objects; the transaction makes it all or nothing.
From("direct:categories-delete").RouteId("categories-delete")
.Transacted()
.ProcessWithRedb(CategoryService.DeleteAsync)
.EndTransaction();
}
}