2020-01-03 16:31:39 +00:00
|
|
|
import { Comment } from "../entities/Comment";
|
|
|
|
import { DatabaseHelper } from "./DatabaseHelper";
|
2020-03-21 07:40:31 +00:00
|
|
|
import { unlinkSync, existsSync } from "fs";
|
|
|
|
import { LikesHelper, LikesType } from "./LikesHelper";
|
2020-03-21 10:28:32 +00:00
|
|
|
import { mysql_date, time } from "../utils/DateUtils";
|
2020-01-03 16:31:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Comments helper
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
const COMMENTS_TABLE = "commentaires";
|
|
|
|
|
|
|
|
export class CommentsHelper {
|
|
|
|
|
2020-03-21 10:28:32 +00:00
|
|
|
/**
|
|
|
|
* Create a new comment
|
|
|
|
*
|
|
|
|
* @param comment Information about the comment to create
|
|
|
|
*/
|
|
|
|
public static async Create(comment: Comment) : Promise<number> {
|
|
|
|
return await DatabaseHelper.InsertRow(COMMENTS_TABLE, {
|
|
|
|
ID_texte: comment.postID,
|
|
|
|
ID_personne: comment.userID,
|
|
|
|
date_envoi: mysql_date(),
|
|
|
|
time_insert: time(),
|
|
|
|
commentaire: comment.content,
|
|
|
|
image_commentaire: comment.hasImage ? comment.imagePath : ""
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-03 16:31:39 +00:00
|
|
|
/**
|
|
|
|
* Get the comments of a POST
|
|
|
|
*
|
|
|
|
* @param postID Target post ID
|
|
|
|
*/
|
|
|
|
public static async Get(postID: number) : Promise<Array<Comment>> {
|
|
|
|
const results = await DatabaseHelper.Query({
|
|
|
|
table: COMMENTS_TABLE,
|
|
|
|
where: {
|
|
|
|
ID_texte: postID
|
|
|
|
},
|
|
|
|
order: "ID"
|
|
|
|
});
|
|
|
|
|
|
|
|
return results.map(this.DbToComment);
|
|
|
|
}
|
|
|
|
|
2020-03-21 07:40:31 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-20 18:07:19 +00:00
|
|
|
/**
|
|
|
|
* Delete all the comments associated to a post
|
|
|
|
*
|
|
|
|
* @param postID Target post ID
|
|
|
|
*/
|
|
|
|
public static async DeleteAll(postID: number) {
|
2020-03-21 07:40:31 +00:00
|
|
|
|
|
|
|
// Get the comments of the post
|
|
|
|
for(const comment of await this.Get(postID)) {
|
|
|
|
await this.Delete(comment);
|
|
|
|
}
|
2020-03-20 18:07:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 16:31:39 +00:00
|
|
|
/**
|
|
|
|
* Turn a database entry into a Comment object
|
|
|
|
*
|
|
|
|
* @param row Database entry
|
|
|
|
*/
|
|
|
|
private static DbToComment(row: any) : Comment {
|
|
|
|
return new Comment({
|
|
|
|
id: row.ID,
|
|
|
|
userID: row.ID_personne,
|
|
|
|
postID: row.ID_texte,
|
|
|
|
timeSent: row.time_insert == null ? new Date(row.date_envoi).getTime() / 100 : row.time_insert,
|
|
|
|
content: row.commentaire,
|
|
|
|
imagePath: (row.image_commentaire != null && row.image_commentaire != "") ? row.image_commentaire.replace("file:", "") : null
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|