From 72a1dbc38ed67ee0e08ac35dee1c3112982e8065 Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Sun, 15 Jan 2023 15:05:31 +0100 Subject: [PATCH] feat: candidate delete --- api/src/routes/admin.rs | 13 +++++++++---- core/src/database/mutation/application.rs | 13 ++++++++++++- core/src/services/application_service.rs | 5 +++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/api/src/routes/admin.rs b/api/src/routes/admin.rs index 4cbe7aa..4ffc1d1 100644 --- a/api/src/routes/admin.rs +++ b/api/src/routes/admin.rs @@ -192,11 +192,16 @@ pub async fn delete_candidate( .ok_or(to_custom_error(ServiceError::CandidateNotFound))?; let candidate = ApplicationService::find_related_candidate(db, &application).await.map_err(to_custom_error)?; - // TODO + ApplicationService::delete(db, application).await.map_err(to_custom_error)?; + + let remaining_applications = Query::find_applications_by_candidate_id(db, candidate.id).await + .map_err(|e| to_custom_error(ServiceError::DbError(e)))?; + + if remaining_applications.is_empty() { + CandidateService::delete_candidate(db, candidate).await.map_err(to_custom_error)?; + } - CandidateService::delete_candidate(db, candidate) - .await - .map_err(to_custom_error) + Ok(()) } #[post("/candidate//reset_password")] diff --git a/core/src/database/mutation/application.rs b/core/src/database/mutation/application.rs index 0187360..23c0da6 100644 --- a/core/src/database/mutation/application.rs +++ b/core/src/database/mutation/application.rs @@ -1,6 +1,6 @@ use ::entity::application; use log::{info, warn}; -use sea_orm::{DbConn, DbErr, Set, ActiveModelTrait, IntoActiveModel}; +use sea_orm::{DbConn, DbErr, Set, ActiveModelTrait, IntoActiveModel, DeleteResult, ModelTrait}; use crate::Mutation; @@ -32,6 +32,17 @@ impl Mutation { Ok(insert) } + pub async fn delete_application( + db: &DbConn, + application: application::Model, + ) -> Result { + let application_id = application.id; + let delete = application.delete(db).await?; + + warn!("APPLICATION {} DELETED", application_id); + Ok(delete) + } + pub async fn update_candidate_fk( db: &DbConn, application: application::Model, diff --git a/core/src/services/application_service.rs b/core/src/services/application_service.rs index 1229b62..378f68c 100644 --- a/core/src/services/application_service.rs +++ b/core/src/services/application_service.rs @@ -142,6 +142,11 @@ impl ApplicationService { } } + pub async fn delete(db: &DbConn, application: application::Model) -> Result<(), ServiceError> { + Mutation::delete_application(db, application).await?; + Ok(()) + } + fn is_application_id_valid(application_id: i32) -> bool { let s = &application_id.to_string(); if s.len() <= 3 {