diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index 902fe3a..ab1e564 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -11,7 +11,7 @@ use crate::data::group_member::GroupMembershipLevel; use crate::data::post::{Post, PostAccessLevel, PostFile, PostKind, PostPageKind, PostVisibilityLevel, PostWebLink}; use crate::data::post::PostKind::{POST_KIND_COUNTDOWN, POST_KIND_IMAGE, POST_KIND_MOVIE, POST_KIND_PDF, POST_KIND_SURVEY, POST_KIND_WEBLINK, POST_KIND_YOUTUBE}; use crate::data::user::UserID; -use crate::helpers::{comments_helper, database, friends_helper, groups_helper, likes_helper, user_helper}; +use crate::helpers::{comments_helper, database, friends_helper, groups_helper, likes_helper, survey_helper, user_helper}; use crate::helpers::likes_helper::LikeType; use crate::utils::date_utils::{mysql_date, time}; use crate::utils::user_data_utils::user_data_path; @@ -422,6 +422,13 @@ pub fn delete(p: &Post) -> ResultBoxError { } } + // Survey + PostKind::POST_KIND_SURVEY => { + if survey_helper::exists(p.id)? { + survey_helper::delete(p.id)?; + } + } + _ => { /* Nothing to be done */ } } diff --git a/src/helpers/survey_helper.rs b/src/helpers/survey_helper.rs index f4675fe..ca17ca1 100644 --- a/src/helpers/survey_helper.rs +++ b/src/helpers/survey_helper.rs @@ -28,6 +28,23 @@ pub fn create(survey: &NewSurvey) -> ResultBoxError { Ok(()) } +/// Delete the survey associated to a post +pub fn delete(post_id: u64) -> ResultBoxError { + let survey_id = get_id(post_id)?; + + database::DeleteQuery::new(SURVEY_RESPONSE_TABLE) + .cond_u64("ID_sondage", survey_id) + .exec()?; + + database::DeleteQuery::new(SURVEY_CHOICES_TABLE) + .cond_u64("ID_sondage", survey_id) + .exec()?; + + database::DeleteQuery::new(SURVEY_INFO_TABLE) + .cond_u64("ID", survey_id) + .exec() +} + /// Insert a new choice for a survey pub fn create_choice(survey_id: u64, choice: &str) -> ResultBoxError { database::InsertQuery::new(SURVEY_CHOICES_TABLE) @@ -37,6 +54,14 @@ pub fn create_choice(survey_id: u64, choice: &str) -> ResultBoxError { .insert_drop_result() } +/// Get the ID of a survey associated to a post +pub fn get_id(post_id: u64) -> ResultBoxError { + database::QueryInfo::new(SURVEY_INFO_TABLE) + .cond_u64("ID_texte", post_id) + .add_field("ID") + .query_row(|r| r.get_u64("ID")) +} + /// Get information about a survey pub fn get_info(post_id: u64) -> ResultBoxError { database::QueryInfo::new(SURVEY_INFO_TABLE) @@ -67,6 +92,14 @@ pub fn get_user_choice(survey_id: u64, user_id: &UserID) -> ResultBoxError .unwrap_or(0)) } +/// Check out whether a survey exists for a post or not +pub fn exists(post_id: u64) -> ResultBoxError { + database::QueryInfo::new(SURVEY_INFO_TABLE) + .cond_u64("ID_texte", post_id) + .exec_count() + .map(|r| r > 0) +} + /// Turn a database entry into a row object fn db_to_survey(row: &database::RowResult) -> ResultBoxError { let survey_id = row.get_u64("ID")?;