1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-12-29 06:58:51 +00:00

Can edit comment content

This commit is contained in:
Pierre HUBERT 2020-03-21 12:01:42 +01:00
parent 77695ac8f4
commit 9a0e746ebc
3 changed files with 64 additions and 0 deletions

View File

@ -62,6 +62,20 @@ export class CommentsController {
h.send(await this.CommentToAPI(h, comment))
}
/**
* Edit (update) a comment content
*
* @param h Request handler
*/
public static async Edit(h: RequestHandler) {
const commentID = await this.GetPostCommentIDWithFullAccess(h, "commentID");
const newContent = this.GetCommentContent(h, "content", true);
await CommentsHelper.Edit(commentID, newContent);
h.success()
}
/**
* Get the content of a comment included in a POST field
*
@ -78,6 +92,21 @@ export class CommentsController {
return content;
}
/**
* Get a comment ID on which current user has full access
*
* @param h Request handler
* @param name The name of the POST field containing the comment ID
*/
private static async GetPostCommentIDWithFullAccess(h: RequestHandler, name: string) : Promise<number> {
const commentID = h.postInt(name);
if(!await CommentsHelper.IsOwner(h.getUserId(), commentID))
h.error(401, "You are not the owner of this comment!");
return commentID;
}
/**
* Turn a list of comment object into API entries

View File

@ -209,6 +209,8 @@ export const Routes : Route[] = [
{path: "/comments/get_single", cb: (h) => CommentsController.GetSingle(h)},
{path: "/comments/edit", cb: (h) => CommentsController.Edit(h)},
// Notifications controller
{path: "/notifications/count_unread", cb: (h) => NotificationsController.CountUnread(h)},

View File

@ -30,6 +30,24 @@ export class CommentsHelper {
});
}
/**
* Edit the content of a comment
*
* @param commentID Target comment
* @param newContent New content
*/
public static async Edit(commentID: number, newContent: string) {
await DatabaseHelper.UpdateRows({
table: COMMENTS_TABLE,
where: {
ID: commentID
},
set: {
commentaire: newContent
}
})
}
/**
* Get the comments of a POST
*
@ -61,6 +79,21 @@ export class CommentsHelper {
}) > 0;
}
/**
* Check out whether a comment belongs to a user or not
*
* @param commentID Target comment ID
*/
public static async IsOwner(userID: number, commentID: number) : Promise<boolean> {
return await DatabaseHelper.Count({
table: COMMENTS_TABLE,
where: {
ID: commentID,
ID_personne: userID
}
}) > 0;
}
/**
* Get information about a single comment
*