lmrc-cli 0.3.16

CLI tool for scaffolding LMRC Stack infrastructure projects
Documentation
//! Pipeline handlers

use axum::{
    extract::{Path, State},
    response::IntoResponse,
    Json,
};
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};

use crate::{error::ApiResult, state::AppState};

use super::{Pipeline, PipelineListItem};

/// List pipelines for a project
pub async fn list_pipelines(
    State(state): State<AppState>,
    Path(project_id): Path<i64>,
) -> ApiResult<impl IntoResponse> {
    use sea_orm::entity::prelude::*;

    #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
    #[sea_orm(table_name = "pipelines")]
    pub struct Model {
        #[sea_orm(primary_key)]
        pub id: i64,
        pub project_id: i64,
        pub name: String,
        pub status: String,
        pub started_at: Option<ChronoDateTimeUtc>,
        pub finished_at: Option<ChronoDateTimeUtc>,
        pub created_at: ChronoDateTimeUtc,
    }

    #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
    pub enum Relation {}

    impl ActiveModelBehavior for ActiveModel {}

    let pipelines = Entity::find()
        .filter(Column::ProjectId.eq(project_id))
        .all(&state.db)
        .await?
        .into_iter()
        .map(|p| PipelineListItem {
            id: p.id,
            project_id: p.project_id,
            name: p.name,
            status: p.status,
            started_at: p.started_at,
            finished_at: p.finished_at,
        })
        .collect::<Vec<_>>();

    Ok(Json(pipelines))
}

/// Get pipeline by ID
pub async fn get_pipeline(
    State(state): State<AppState>,
    Path(id): Path<i64>,
) -> ApiResult<impl IntoResponse> {
    use sea_orm::entity::prelude::*;

    #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
    #[sea_orm(table_name = "pipelines")]
    pub struct Model {
        #[sea_orm(primary_key)]
        pub id: i64,
        pub project_id: i64,
        pub name: String,
        pub status: String,
        pub started_at: Option<ChronoDateTimeUtc>,
        pub finished_at: Option<ChronoDateTimeUtc>,
        pub created_at: ChronoDateTimeUtc,
    }

    #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
    pub enum Relation {}

    impl ActiveModelBehavior for ActiveModel {}

    let pipeline = Entity::find_by_id(id)
        .one(&state.db)
        .await?
        .ok_or_else(|| crate::error::AppError::NotFound(format!("Pipeline {} not found", id)))?;

    Ok(Json(Pipeline {
        id: pipeline.id,
        project_id: pipeline.project_id,
        name: pipeline.name,
        status: pipeline.status,
        started_at: pipeline.started_at,
        finished_at: pipeline.finished_at,
        created_at: pipeline.created_at,
    }))
}