From 50e2277a30dd99d9db46331b7805a0690a9491dd Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 27 Mar 2020 13:57:36 +0100 Subject: [PATCH] Delete user movies --- src/entities/Movie.ts | 4 ++++ src/helpers/AccountHelper.ts | 3 +++ src/helpers/MoviesHelper.ts | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/entities/Movie.ts b/src/entities/Movie.ts index 15cbf98..3b6ba72 100644 --- a/src/entities/Movie.ts +++ b/src/entities/Movie.ts @@ -32,4 +32,8 @@ export class Movie implements MovieBuilder { get url() : string { return pathUserData(this.uri); } + + get sysPath() : string { + return pathUserData(this.uri, true); + } } \ No newline at end of file diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index b7ebe01..0317a42 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -456,6 +456,9 @@ export class AccountHelper { // Delete all the likes created by the user await LikesHelper.DeleteAllUser(userID); + + // Delete all user movies + await MoviesHelper.DeleteAllUser(userID); */ // TODO : continue work diff --git a/src/helpers/MoviesHelper.ts b/src/helpers/MoviesHelper.ts index 33b9b34..37beb1b 100644 --- a/src/helpers/MoviesHelper.ts +++ b/src/helpers/MoviesHelper.ts @@ -1,5 +1,7 @@ import { Movie } from "../entities/Movie"; import { DatabaseHelper } from "./DatabaseHelper"; +import { PostsHelper } from "./PostsHelper"; +import { existsSync, unlinkSync } from "fs"; /** * Legacy movies helper @@ -59,6 +61,36 @@ export class MoviesHelper { } } + /** + * Delete a movie created by the user + * + * @param movie The movie to delete + */ + public static async Delete(movie: Movie) { + + // Delete all related posts + await PostsHelper.DeleteAllWithMovie(movie); + + // Delete associated file + if(existsSync(movie.sysPath)) + unlinkSync(movie.sysPath) + + // Remove movie from database + await DatabaseHelper.DeleteRows(MOVIES_TABLE, { + ID: movie.id + }) + } + + /** + * Delete all the movies of a given user + * + * @param userID The ID of the target user + */ + public static async DeleteAllUser(userID: number) { + for(const m of await this.GetListUser(userID)) + await this.Delete(m); + } + /** * Turn a database entry into a movie object *