Skip to main content

molten_api/handlers/
workflow.rs

1//! This module provides the API handlers for Workflow entity operations.
2//!
3//! It includes functions for creating new workflows and retrieving existing ones,
4//! serving as the entry point for interactions with the workflow service layer.
5use crate::{error::ApiError, state::AppState};
6use axum::{
7    Json,
8    extract::{Path, State},
9};
10use molten_core::{WorkflowBuilder, WorkflowDefinition};
11use molten_service::ServiceError;
12
13/// Create a new workflow definition.
14///
15/// Accepts a [`WorkflowBuilder`] and validates it into a finalized
16/// [`WorkflowDefinition`]. If validation succeeds, the workflow is
17/// persisted and the stored definition is returned.
18///
19/// # Route
20/// `POST /workflows`
21///
22/// # Errors
23/// - Returns an error if the workflow definition fails validation.
24/// - Returns an error if persistence fails.
25///
26/// # Notes
27/// This endpoint is intended only for creating new workflows.
28/// Updates to existing workflows should be handled via a separate endpoint
29/// to allow different validation and lifecycle rules.
30pub async fn create_workflow(
31    State(state): State<AppState>,
32    Json(builder): Json<WorkflowBuilder>,
33) -> Result<Json<WorkflowDefinition>, ApiError> {
34    let workflow_def: WorkflowDefinition = builder
35        .build()
36        .map_err(ServiceError::WorkflowValidationErrors)?;
37
38    let workflow = state.workflow_service.save_workflow(workflow_def).await?;
39
40    Ok(Json(workflow))
41}
42
43/// Retrieve a workflow definition by id.
44///
45/// # Route
46/// `GET /workflows/{id}`
47///
48/// # Errors
49/// - Returns an error if the workflow does not exist.
50/// - Returns an error if the underlying storage operation fails.
51pub async fn get_workflow(
52    State(state): State<AppState>,
53    Path(id): Path<String>,
54) -> Result<Json<WorkflowDefinition>, ApiError> {
55    let workflow = state.workflow_service.get_workflow(&id).await?;
56    Ok(Json(workflow))
57}
58
59// TODO: POST /workflows/{id} for Updates