lmrc-cli 0.3.16

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

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

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

use super::{Deployment, DeploymentListItem};

/// List deployments for a project
pub async fn list_deployments(
    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 = "deployments")]
    pub struct Model {
        #[sea_orm(primary_key)]
        pub id: i64,
        pub project_id: i64,
        pub pipeline_id: Option<i64>,
        pub environment: String,
        pub version: String,
        pub status: String,
        pub deployed_at: Option<ChronoDateTimeUtc>,
        pub created_at: ChronoDateTimeUtc,
    }

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

    impl ActiveModelBehavior for ActiveModel {}

    let deployments = Entity::find()
        .filter(Column::ProjectId.eq(project_id))
        .all(&state.db)
        .await?
        .into_iter()
        .map(|d| DeploymentListItem {
            id: d.id,
            project_id: d.project_id,
            environment: d.environment,
            version: d.version,
            status: d.status,
            deployed_at: d.deployed_at,
        })
        .collect::<Vec<_>>();

    Ok(Json(deployments))
}

/// Get deployment by ID
pub async fn get_deployment(
    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 = "deployments")]
    pub struct Model {
        #[sea_orm(primary_key)]
        pub id: i64,
        pub project_id: i64,
        pub pipeline_id: Option<i64>,
        pub environment: String,
        pub version: String,
        pub status: String,
        pub deployed_at: Option<ChronoDateTimeUtc>,
        pub created_at: ChronoDateTimeUtc,
    }

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

    impl ActiveModelBehavior for ActiveModel {}

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

    Ok(Json(Deployment {
        id: deployment.id,
        project_id: deployment.project_id,
        pipeline_id: deployment.pipeline_id,
        environment: deployment.environment,
        version: deployment.version,
        status: deployment.status,
        deployed_at: deployment.deployed_at,
        created_at: deployment.created_at,
    }))
}