From 3eac46ac55eb72cdf657bc625b8ac3ac26694655 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 21 Mar 2020 14:45:03 +0100 Subject: [PATCH] Like system is working --- src/controllers/LikesController.ts | 60 +++++++++++++++++++++++++++++- src/helpers/LikesHelper.ts | 37 ++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/controllers/LikesController.ts b/src/controllers/LikesController.ts index d2967ff..da119ec 100644 --- a/src/controllers/LikesController.ts +++ b/src/controllers/LikesController.ts @@ -1,4 +1,7 @@ import { RequestHandler } from "../entities/RequestHandler"; +import { LikesType, LikesHelper } from "../helpers/LikesHelper"; +import { UserHelper } from "../helpers/UserHelper"; +import { GroupsAccessLevel } from "../entities/Group"; /** * Likes controller @@ -13,7 +16,62 @@ export class LikesController { * @param h Request handler */ public static async Update(h: RequestHandler) { - // TODO : implement + const req_type = h.postString("type"); + const is_linking = h.postBool("like"); + + let id: number; + let type: LikesType; + + switch(req_type) { + + // In case of user + case "user": + id = await h.postUserId("id"); + type = LikesType.USER; + + if(!await UserHelper.CanSeeUserPage(h.getUserId(), id)) + h.error(401, "You cannot access this user information!") + + break; + + + // In case of post + case "post": + id = await h.postPostIDWithAccess("id"); + type = LikesType.POST; + + // TODO : delete any notification targetting this user about the post + + break; + + + // In case of comment + case "comment": + id = await h.postCommentIDWithAccess("id"); + type = LikesType.COMMENT; + break; + + + // In case of group + case "group": + id = await h.postGroupIDWithAccess("id", GroupsAccessLevel.VIEW_ACCESS); + type = LikesType.GROUP; + break; + + + // Default case : throw an error + default: + throw Error("Unsupported like type : "+req_type+" !"); + + } + + + // Update like status + await LikesHelper.Update(h.getUserId(), is_linking, id, type); + + + h.success(); + } } \ No newline at end of file diff --git a/src/helpers/LikesHelper.ts b/src/helpers/LikesHelper.ts index 80c4b3c..b8506df 100644 --- a/src/helpers/LikesHelper.ts +++ b/src/helpers/LikesHelper.ts @@ -1,4 +1,5 @@ import { DatabaseHelper } from "./DatabaseHelper"; +import { mysql_date } from "../utils/DateUtils"; /** * Likes helper @@ -70,6 +71,42 @@ export class LikesHelper { }) == 1; } + /** + * Update like status + * + * @param userID The ID of the user updating like status + * @param isLiking New like status of the user + * @param id The ID of the component element to update + * @param kind The kind of the element + */ + public static async Update(userID: number, isLiking: boolean, id: number, kind: LikesType) { + + // If not liking anymore + if(!isLiking) { + await DatabaseHelper.DeleteRows(LIKES_TABLE, { + ID_personne: userID, + ID_type: id, + type: LikesKindsDB[kind] + }) + + return; + } + + // Else we make the user liking the element + + // Check if the user is already liking the element + if(await this.IsLiking(userID, id, kind)) + return; + + // Insert in the database + await DatabaseHelper.InsertRow(LIKES_TABLE, { + ID_personne: userID, + ID_type: id, + Date_envoi: mysql_date(), + type: LikesKindsDB[kind] + }); + } + /** * Delete all the likes related to an element *