rustploy 0.1.7

Rustploy PaaS daemon — lightweight self-hosted deployment platform
//! `Command::ProjectDelete`: apaga um projeto; recusa se ainda tiver serviços
//! ou jobs.

use crate::bootstrap::state::AppState;
use shared::Response as RpResponse;

pub async fn handle(state: AppState, id: String) -> RpResponse {
    match crate::store::services::count_by_project(&state.db, &id).await {
        Ok(count) if count > 0 => {
            return RpResponse::err(
                "HasServices",
                "Remova todos os serviços antes de deletar o projeto",
            );
        }
        Err(e) => return RpResponse::err("DatabaseError", e.to_string()),
        _ => {}
    }
    match crate::store::job::count_by_project(&state.db, &id).await {
        Ok(count) if count > 0 => {
            return RpResponse::err(
                "HasJobs",
                "Remova todos os jobs (Schedules) antes de deletar o projeto",
            );
        }
        Err(e) => return RpResponse::err("DatabaseError", e.to_string()),
        _ => {}
    }
    match crate::store::projects::delete(&state.db, &id).await {
        Ok(true) => RpResponse::Ok,
        Ok(false) => RpResponse::err("NotFound", "project not found"),
        Err(e) => RpResponse::err("DatabaseError", e.to_string()),
    }
}