Skip to main content

backbone_activity/
lib.rs

1//! Activity Module
2//!
3//! Generated by metaphor-schema. Enhanced with runtime implementations.
4//!
5//! This module provides:
6//! - Domain entities and repositories
7//! - Application services
8//! - HTTP and gRPC handlers
9//! - Route configuration
10//! - State machine enforcement
11//! - Validation rules runtime
12//! - RBAC middleware
13//! - Trigger execution system
14//! - Computed fields
15//! - Workflow orchestrator
16
17#![recursion_limit = "1024"]
18#![allow(unused_imports)]
19
20// Generated modules
21pub mod domain;
22pub mod infrastructure;
23pub mod application;
24pub mod presentation;
25pub mod seeders;
26pub mod exports;
27
28// Re-exports for convenience - Domain entities
29pub use domain::entity::*;
30
31// Re-exports - Infrastructure
32pub use infrastructure::persistence::*;
33
34// Re-exports - Application services
35pub use application::service::ActivityService;
36
37use std::sync::Arc;
38use axum::Router;
39use sqlx::PgPool;
40
41/// Activity module configuration
42///
43/// Use the builder pattern to configure and register this module:
44///
45/// ```text
46/// let activity = ActivityModule::builder()
47///     .with_database(pool.clone())
48///     .build()?;
49///
50/// // Unguarded full CRUD (trusted/admin); compose a guarded router for production.
51/// let router = activity.all_crud_routes();
52/// ```
53pub struct ActivityModule {
54    pub(crate) activity_service: Arc<ActivityService>,
55}
56
57impl ActivityModule {
58    /// Create a new module builder
59    pub fn builder() -> ActivityModuleBuilder {
60        ActivityModuleBuilder::new()
61    }
62
63    /// Mount ALL generated CRUD endpoints (12 per entity) with NO domain
64    /// validation — the fully **unguarded** surface. A well-formed request can
65    /// create invalid rows or soft-delete a referenced master out from under its
66    /// dependents. Prefer a guarded composition (read + validated writes) for any
67    /// real deployment; use this only in trusted/admin/seeding contexts.
68    pub fn all_crud_routes(&self) -> Router {
69        use presentation::http::{
70            create_activity_routes,
71        };
72
73        Router::new()
74            .merge(create_activity_routes(self.activity_service.clone()))
75    }
76
77    /// Deprecated alias for [`Self::all_crud_routes`]. `routes()` reads like
78    /// "the routes" but mounts UNVALIDATED generic CRUD on every entity — a naive
79    /// mount exposes unguarded writes. Compose a guarded router (read + validated
80    /// writes) for production, or call `all_crud_routes()` to opt into the full
81    /// unguarded surface explicitly.
82    #[deprecated(note = "mounts unvalidated generic CRUD; prefer readonly_routes() + validated writes, or all_crud_routes() for the full/unguarded surface")]
83    pub fn routes(&self) -> Router {
84        self.all_crud_routes()
85    }
86
87    /// Read-only routes for every entity (GET endpoints only) — the safe base.
88    ///
89    /// Generic mutation can't reach here, so this surface cannot bypass a
90    /// validated write service's invariants. Use this as the production base and
91    /// merge validated write routes (or a write service's HTTP layer) onto it.
92    pub fn readonly_routes(&self) -> Router {
93        use presentation::http::{
94            create_activity_read_routes,
95        };
96
97        Router::new()
98            .merge(create_activity_read_routes(self.activity_service.clone()))
99    }
100
101}
102
103/// Builder for ActivityModule
104pub struct ActivityModuleBuilder {
105    db_pool: Option<PgPool>,
106}
107
108impl ActivityModuleBuilder {
109    /// Create a new builder
110    pub fn new() -> Self {
111        Self {
112            db_pool: None,
113        }
114    }
115
116    /// Set the database connection pool
117    pub fn with_database(mut self, pool: PgPool) -> Self {
118        self.db_pool = Some(pool);
119        self
120    }
121
122
123    /// Build the module with configured dependencies
124    pub fn build(self) -> anyhow::Result<ActivityModule> {
125        let db_pool = self.db_pool
126            .ok_or_else(|| anyhow::anyhow!("Database pool not configured"))?;
127
128        // Activity service
129        let activity_repository = Arc::new(ActivityRepository::new(db_pool.clone()));
130        let activity_service = Arc::new(ActivityService::with_repository(activity_repository.clone()).with_guard(Arc::new(crate::application::service::ActivityRowRules)));
131
132
133        Ok(ActivityModule {
134            activity_service,
135        })
136    }
137}
138
139impl Default for ActivityModuleBuilder {
140    fn default() -> Self {
141        Self::new()
142    }
143}