From ee6579efa0fb6d7d74728e8394a5eb18c3e67c9f Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 13 Dec 2019 17:49:58 +0100 Subject: [PATCH] Add /groups/get_my_list --- src/controllers/GroupsController.ts | 21 +++++++++++++++++++ src/controllers/Routes.ts | 5 +++++ src/helpers/GroupsHelper.ts | 31 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 src/controllers/GroupsController.ts create mode 100644 src/helpers/GroupsHelper.ts diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts new file mode 100644 index 0000000..84042e6 --- /dev/null +++ b/src/controllers/GroupsController.ts @@ -0,0 +1,21 @@ +import { RequestHandler } from "../entities/RequestHandler"; +import { GroupsHelper } from "../helpers/GroupsHelper"; + +/** + * Groups API controller + * + * @author Pierre HUBERT + */ + +export class GroupsController { + + /** + * Get the list of groups of the user + * + * @param h Request handler + */ + public static async GetListUser(h: RequestHandler) { + h.send(await GroupsHelper.GetListUser(h.getUserId())); + } + +} \ No newline at end of file diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 72c2412..992f6a2 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -4,6 +4,7 @@ import { AccountController } from "./AccountController"; import { UserController } from "./UserController"; import { ConversationsController } from "./ConversationsController"; import { SearchController } from "./SearchController"; +import { GroupsController } from "./GroupsController"; /** * Controllers routes @@ -81,4 +82,8 @@ export const Routes : Route[] = [ // Search controller {path: "/search/user", cb: (h) => SearchController.SearchUser(h)}, {path: "/user/search", cb: (h) => SearchController.SearchUser(h)}, // Legacy + + + // Groups controller + {path: "/groups/get_my_list", cb: (h) => GroupsController.GetListUser(h)}, ] \ No newline at end of file diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts new file mode 100644 index 0000000..04cf80f --- /dev/null +++ b/src/helpers/GroupsHelper.ts @@ -0,0 +1,31 @@ +import { DatabaseHelper } from "./DatabaseHelper"; + +/** + * Groups helper + * + * @author Pierre HUBERT + */ + +const GROUPS_LIST_TABLE = "comunic_groups"; +const GROUPS_MEMBERS_TABLE = "comunic_groups_members"; + +export class GroupsHelper { + + /** + * Get the list of groups of a user + * + * @param userID Target user ID + */ + public static async GetListUser(userID: number) : Promise> { + const list = await DatabaseHelper.Query({ + table: GROUPS_MEMBERS_TABLE, + where: { + user_id: userID + }, + fields: ["groups_id"] + }); + + return list.map(e => e.groups_id); + } + +} \ No newline at end of file