mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-03 11:04:02 +00:00 
			
		
		
		
	Can delete comments
This commit is contained in:
		@@ -1,5 +1,7 @@
 | 
			
		||||
import { Comment } from "../entities/Comment";
 | 
			
		||||
import { DatabaseHelper } from "./DatabaseHelper";
 | 
			
		||||
import { unlinkSync, existsSync } from "fs";
 | 
			
		||||
import { LikesHelper, LikesType } from "./LikesHelper";
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Comments helper
 | 
			
		||||
@@ -28,13 +30,38 @@ export class CommentsHelper {
 | 
			
		||||
		return results.map(this.DbToComment);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Permanently delete a comment
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param comment Information about the comment to delete
 | 
			
		||||
	 */
 | 
			
		||||
	public static async Delete(comment: Comment) {
 | 
			
		||||
 | 
			
		||||
		// Delete associated image (if any)
 | 
			
		||||
		if(comment.hasImage && existsSync(comment.imageSysPath)) {
 | 
			
		||||
			unlinkSync(comment.imageSysPath);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Delete likes associated with the comment
 | 
			
		||||
		await LikesHelper.DeleteAll(comment.id, LikesType.COMMENT);
 | 
			
		||||
 | 
			
		||||
		// Delete the comment from the database
 | 
			
		||||
		await DatabaseHelper.DeleteRows(COMMENTS_TABLE, {
 | 
			
		||||
			ID: comment.id
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Delete all the comments associated to a post
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param postID Target post ID
 | 
			
		||||
	 */
 | 
			
		||||
	public static async DeleteAll(postID: number) {
 | 
			
		||||
		// TODO : implement
 | 
			
		||||
		
 | 
			
		||||
		// Get the comments of the post
 | 
			
		||||
		for(const comment of await this.Get(postID)) {
 | 
			
		||||
			await this.Delete(comment);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user