1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 13:29:22 +00:00

Created first comment

This commit is contained in:
Pierre HUBERT 2020-03-21 11:28:32 +01:00
parent 932ae20403
commit 4ffa8a421b
2 changed files with 62 additions and 0 deletions

View File

@ -1,6 +1,9 @@
import { RequestHandler } from "../entities/RequestHandler"; import { RequestHandler } from "../entities/RequestHandler";
import { Comment } from "../entities/Comment"; import { Comment } from "../entities/Comment";
import { LikesHelper, LikesType } from "../helpers/LikesHelper"; import { LikesHelper, LikesType } from "../helpers/LikesHelper";
import { check_string_before_insert, removeHTMLNodes } from "../utils/StringUtils";
import { time } from "../utils/DateUtils";
import { CommentsHelper } from "../helpers/CommentsHelper";
/** /**
* Comments controller * Comments controller
@ -18,7 +21,49 @@ export class CommentsController {
public static async Create(h: RequestHandler) { public static async Create(h: RequestHandler) {
const postID = await h.postPostIDWithAccess("postID"); const postID = await h.postPostIDWithAccess("postID");
let content: string, image_path: string;
// Check if an image was included in the request or not
if(h.hasFile("image")) {
content = this.GetCommentContent(h, "content", false);
image_path = await h.savePostImage("image", "imgcommentaire", 700, 700);
}
else
content = this.GetCommentContent(h, "content", true);
const newComment = new Comment({
id: -1,
timeSent: time(),
postID: postID,
userID: h.getUserId(),
content: content,
imagePath: image_path
});
const commentID = await CommentsHelper.Create(newComment);
// TODO : Create notifications
// TODO : Delete any notifications targetting this user about the post
h.send({success: true, commentID: commentID});
}
/**
* Get the content of a comment included in a POST field
*
* @param h Request handler
* @param name The name of the post field
* @param need_check True if the comment must have valid content / false else
*/
private static GetCommentContent(h: RequestHandler, name: string, need_check = true) : string {
const content = h.postContent(name);
if(need_check && !check_string_before_insert(content))
h.error(400, "Please check new comment content!");
return content;
} }

View File

@ -2,6 +2,7 @@ import { Comment } from "../entities/Comment";
import { DatabaseHelper } from "./DatabaseHelper"; import { DatabaseHelper } from "./DatabaseHelper";
import { unlinkSync, existsSync } from "fs"; import { unlinkSync, existsSync } from "fs";
import { LikesHelper, LikesType } from "./LikesHelper"; import { LikesHelper, LikesType } from "./LikesHelper";
import { mysql_date, time } from "../utils/DateUtils";
/** /**
* Comments helper * Comments helper
@ -13,6 +14,22 @@ const COMMENTS_TABLE = "commentaires";
export class CommentsHelper { export class CommentsHelper {
/**
* 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 : ""
});
}
/** /**
* Get the comments of a POST * Get the comments of a POST
* *