From af307671bbbf920a191ba10aa1e9f0a40c041b97 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 9 Jul 2020 10:37:06 +0200 Subject: [PATCH] Start to delete posts --- src/controllers/posts_controller.rs | 6 +++++- src/helpers/likes_helper.rs | 16 ++++++++++++---- src/helpers/posts_helper.rs | 13 ++++++++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index cdf4c63..6d34043 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -263,5 +263,9 @@ pub fn update_content(r: &mut HttpRequestHandler) -> RequestResult { /// Delete a post pub fn delete(r: &mut HttpRequestHandler) -> RequestResult { - r.success("Implement me") + let post = r.post_post_with_access("postID", PostAccessLevel::FULL_ACCESS)?; + + posts_helper::delete(&post)?; + + r.success("Post deleted.") } \ No newline at end of file diff --git a/src/helpers/likes_helper.rs b/src/helpers/likes_helper.rs index 0e95885..37f55e4 100644 --- a/src/helpers/likes_helper.rs +++ b/src/helpers/likes_helper.rs @@ -2,20 +2,20 @@ //! //! Module dedicated to likes management -use crate::data::error::ResultBoxError; -use crate::helpers::database::QueryInfo; use crate::constants::database_tables_names::LIKES_TABLE; +use crate::data::error::ResultBoxError; use crate::data::user::UserID; +use crate::helpers::database; +use crate::helpers::database::QueryInfo; pub enum LikeType { USER, POST, COMMENT, - GROUP + GROUP, } impl LikeType { - /// Get matching database type pub fn to_db_type(&self) -> String { match self { @@ -46,4 +46,12 @@ pub fn is_liking(user_id: &UserID, id: u64, kind: LikeType) -> ResultBoxError 0) +} + +/// Delete all the likes associated with a post +pub fn delete_all(id: u64, kind: LikeType) -> ResultBoxError { + database::DeleteQuery::new(LIKES_TABLE) + .cond_u64("ID_type", id) + .cond_str("type", &kind.to_db_type()) + .exec() } \ No newline at end of file diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index bf3ff18..b5d0f05 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -11,7 +11,8 @@ 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::{database, friends_helper, groups_helper, user_helper}; +use crate::helpers::{database, friends_helper, groups_helper, likes_helper, user_helper}; +use crate::helpers::likes_helper::LikeType; use crate::utils::date_utils::{mysql_date, time}; impl PostVisibilityLevel { @@ -399,6 +400,16 @@ pub fn set_content(post_id: u64, new_content: &str) -> ResultBoxError { .exec() } +/// Delete a post +pub fn delete(p: &Post) -> ResultBoxError { + // TODO : delete all the notifications related with the post + + // Delete all the likes associated with the post + likes_helper::delete_all(p.id, LikeType::POST)?; + + Ok(()) +} + /// Turn a post into a database entry fn db_to_post(res: &database::RowResult) -> ResultBoxError { let user_id = if res.get_u64("ID_amis")? == 0 {