1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Can delete associated surveys

This commit is contained in:
Pierre HUBERT 2020-07-09 11:12:35 +02:00
parent 409f5a8feb
commit 82adbbcd6d
2 changed files with 41 additions and 1 deletions

View File

@ -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::{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::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::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::helpers::likes_helper::LikeType;
use crate::utils::date_utils::{mysql_date, time}; use crate::utils::date_utils::{mysql_date, time};
use crate::utils::user_data_utils::user_data_path; 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 */ } _ => { /* Nothing to be done */ }
} }

View File

@ -28,6 +28,23 @@ pub fn create(survey: &NewSurvey) -> ResultBoxError {
Ok(()) 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 /// Insert a new choice for a survey
pub fn create_choice(survey_id: u64, choice: &str) -> ResultBoxError { pub fn create_choice(survey_id: u64, choice: &str) -> ResultBoxError {
database::InsertQuery::new(SURVEY_CHOICES_TABLE) database::InsertQuery::new(SURVEY_CHOICES_TABLE)
@ -37,6 +54,14 @@ pub fn create_choice(survey_id: u64, choice: &str) -> ResultBoxError {
.insert_drop_result() .insert_drop_result()
} }
/// Get the ID of a survey associated to a post
pub fn get_id(post_id: u64) -> ResultBoxError<u64> {
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 /// Get information about a survey
pub fn get_info(post_id: u64) -> ResultBoxError<Survey> { pub fn get_info(post_id: u64) -> ResultBoxError<Survey> {
database::QueryInfo::new(SURVEY_INFO_TABLE) database::QueryInfo::new(SURVEY_INFO_TABLE)
@ -67,6 +92,14 @@ pub fn get_user_choice(survey_id: u64, user_id: &UserID) -> ResultBoxError<u64>
.unwrap_or(0)) .unwrap_or(0))
} }
/// Check out whether a survey exists for a post or not
pub fn exists(post_id: u64) -> ResultBoxError<bool> {
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 /// Turn a database entry into a row object
fn db_to_survey(row: &database::RowResult) -> ResultBoxError<Survey> { fn db_to_survey(row: &database::RowResult) -> ResultBoxError<Survey> {
let survey_id = row.get_u64("ID")?; let survey_id = row.get_u64("ID")?;