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

Like system is working

This commit is contained in:
Pierre HUBERT 2020-03-21 14:45:03 +01:00
parent aa21a2a5e6
commit 3eac46ac55
2 changed files with 96 additions and 1 deletions

View File

@ -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();
}
}

View File

@ -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
*