diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts index 06b1736..35ad18a 100644 --- a/src/controllers/GroupsController.ts +++ b/src/controllers/GroupsController.ts @@ -3,6 +3,7 @@ import { GroupsHelper } from "../helpers/GroupsHelper"; import { GroupsAccessLevel, GroupInfo, GroupVisibilityLevel, GroupPostsCreationLevel, GroupRegistrationLevel } from "../entities/Group"; import { GroupMembershipLevels } from "../entities/GroupMember"; import { time } from "../utils/DateUtils"; +import { LikesHelper, LikesType } from "../helpers/LikesHelper"; /** * Groups API controller @@ -160,7 +161,7 @@ export class GroupsController { data["description"] = info.hasDescription ? info.description : "null"; data["url"] = info.url ? info.hasURL : "null"; - //TODO : add likes information + data["number_likes"] = await LikesHelper.Count(info.id, LikesType.GROUP); } return data; diff --git a/src/helpers/LikesHelper.ts b/src/helpers/LikesHelper.ts new file mode 100644 index 0000000..229bf93 --- /dev/null +++ b/src/helpers/LikesHelper.ts @@ -0,0 +1,52 @@ +import { DatabaseHelper } from "./DatabaseHelper"; + +/** + * Likes helper + * + * @author Pierre HUBERT + */ + +/** + * Types of likes + */ +export enum LikesType { + USER = "user", + POST = "post", + COMMENT = "comment", + GROUP = "group" +} + + +const LIKES_TABLE = "aime"; + + +/** + * Translation of kinds of likes for the database + */ +const LikesKindsDB = []; +LikesKindsDB[LikesType.USER] = "page"; +LikesKindsDB[LikesType.POST] = "texte"; +LikesKindsDB[LikesType.COMMENT] = "commentaire"; +LikesKindsDB[LikesType.GROUP] = "group"; + + + +export class LikesHelper { + + /** + * Count the number of likes of a specified element + * + * @param id The ID of the element to count + * @param type The type of the element + */ + public static async Count(id: number, type: LikesType) : Promise { + return DatabaseHelper.Count({ + table: LIKES_TABLE, + where: { + ID_type: id, + type: LikesKindsDB[type] + } + }) + } + +} \ No newline at end of file