backbone-integrations 0.6.0

Integration registry: connectors, integration accounts and an idempotent inbound event lane, with one OAuth flow (HMAC-bound state, PKCE)
Documentation
//! Example REST handlers
//!
//! Generated by backbone-schema. Do not edit manually.
//!
//! Uses Axum and backbone-core's BackboneCrudHandler for all CRUD endpoints.

use std::sync::Arc;

use axum::Router;

use backbone_core::http::BackboneCrudHandler;

use crate::application::dto::{CreateExampleDto, ExampleResponseDto, UpdateExampleDto};
use crate::application::service::{ExampleService, ServiceError};
use crate::domain::entity::Example;

/// Application error type for the Example handler.
#[derive(Debug, thiserror::Error)]
pub enum ExampleError {
    #[error("Not found: {0}")]
    NotFound(String),
    #[error("Validation error: {0}")]
    Validation(String),
    #[error("Database error: {0}")]
    Database(String),
    #[error("Internal error: {0}")]
    Internal(String),
}

impl From<ServiceError> for ExampleError {
    fn from(err: ServiceError) -> Self {
        match err {
            ServiceError::NotFound => Self::NotFound(err.to_string()),
            ServiceError::Validation(ref msg) => Self::Validation(msg.clone()),
            ServiceError::AlreadyExists(ref msg) => Self::Validation(msg.clone()),
            ServiceError::Repository(ref e) => Self::Database(e.to_string()),
            ServiceError::Internal(ref msg) => Self::Internal(msg.clone()),
        }
    }
}

impl axum::response::IntoResponse for ExampleError {
    fn into_response(self) -> axum::response::Response {
        use axum::http::StatusCode;
        use axum::Json;

        let (status, code) = match &self {
            Self::NotFound(_) => (StatusCode::NOT_FOUND, "EXAMPLE_NOT_FOUND"),
            Self::Validation(_) => (StatusCode::BAD_REQUEST, "EXAMPLE_VALIDATION_ERROR"),
            Self::Database(_) => (StatusCode::INTERNAL_SERVER_ERROR, "EXAMPLE_DATABASE_ERROR"),
            Self::Internal(_) => (StatusCode::INTERNAL_SERVER_ERROR, "EXAMPLE_INTERNAL_ERROR"),
        };

        let body = serde_json::json!({
            "success": false,
            "error": code,
            "message": self.to_string(),
        });

        (status, Json(body)).into_response()
    }
}

/// Create an Axum router with all standard Backbone endpoints for Example.
pub fn create_example_routes(service: Arc<ExampleService>) -> Router {
    BackboneCrudHandler::<
        ExampleService,
        Example,
        CreateExampleDto,
        UpdateExampleDto,
        ExampleResponseDto,
    >::routes(service, "/examples")
}