From a1613758e4b8cdc6a4a7182033bf2ce39ad5ec06 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 21 Mar 2020 08:57:51 +0100 Subject: [PATCH] Can delete information associated to a post --- src/entities/Post.ts | 4 +++ src/helpers/PostsHelper.ts | 15 +++++++++- src/helpers/SurveyHelper.ts | 55 +++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/entities/Post.ts b/src/entities/Post.ts index 451ecdb..207b3ee 100644 --- a/src/entities/Post.ts +++ b/src/entities/Post.ts @@ -73,6 +73,10 @@ export class PostFile implements PostFileBuilder { get url() : string { return pathUserData(this.path) } + + get sysPath() : string { + return pathUserData(this.path, true); + } } export interface PostLinkBuilder { diff --git a/src/helpers/PostsHelper.ts b/src/helpers/PostsHelper.ts index 22bb247..26b7c0f 100644 --- a/src/helpers/PostsHelper.ts +++ b/src/helpers/PostsHelper.ts @@ -7,6 +7,8 @@ import { GroupMembershipLevels } from "../entities/GroupMember"; import { mysql_date } from "../utils/DateUtils"; import { LikesHelper, LikesType } from "./LikesHelper"; import { CommentsHelper } from "./CommentsHelper"; +import { existsSync, unlinkSync } from "fs"; +import { SurveyHelper } from "./SurveyHelper"; /** * Posts helper @@ -493,7 +495,18 @@ export class PostsHelper { // Delete all the comments associated to the post await CommentsHelper.DeleteAll(postID); - // TODO : continue deletion + // Delete associated file (if any) + if(post.kind == PostKind.POST_KIND_IMAGE + || post.kind == PostKind.POST_KIND_PDF) { + if(existsSync(post.file.sysPath)) + unlinkSync(post.file.sysPath); + } + + // Delete associated survey (if any) + if(post.kind == PostKind.POST_KIND_SURVEY) { + if(await SurveyHelper.Exists(postID)) + await SurveyHelper.Delete(postID); + } } /** diff --git a/src/helpers/SurveyHelper.ts b/src/helpers/SurveyHelper.ts index 38b7886..03b2739 100644 --- a/src/helpers/SurveyHelper.ts +++ b/src/helpers/SurveyHelper.ts @@ -54,6 +54,61 @@ export class SurveyHelper { } } + /** + * Check out whether a survey is associated to a post or not + * + * @param postID Target post ID + */ + public static async Exists(postID: number) : Promise { + return await DatabaseHelper.Count({ + table: SURVEY_INFO_TABLE, + where: { + ID_texte: postID + } + }) > 0 + } + + /** + * Get the ID of the survey associated with a post + * + * @param postID Target post ID + */ + private static async GetID(postID: number) : Promise { + const info = await DatabaseHelper.QueryRow({ + table: SURVEY_INFO_TABLE, + where: { + ID_texte: postID + }, + fields: ["ID"] + }); + + if(info == null) + throw new Error("Survey for post " + postID + " not found!"); + + return info.ID; + } + + /** + * Delete the survey associated to a post + * + * @param postID Target post ID + */ + public static async Delete(postID: number) { + const surveyID = await this.GetID(postID); + + DatabaseHelper.DeleteRows(SURVEY_RESPONSE_TABLE, { + ID_sondage: surveyID + }); + + DatabaseHelper.DeleteRows(SURVEY_CHOICES_TABLE, { + ID_sondage: surveyID + }); + + DatabaseHelper.DeleteRows(SURVEY_INFO_TABLE, { + ID: surveyID + }); + } + /** * Get information about the survey of a post *