From e85bdf8e54f85f83a3c42d4c98c19cdb5768ae1e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 24 Dec 2019 18:47:55 +0100 Subject: [PATCH] Can create new groups --- src/controllers/GroupsController.ts | 21 +++++++++++++ src/controllers/Routes.ts | 2 ++ src/entities/NewGroup.ts | 23 ++++++++++++++ src/helpers/GroupsHelper.ts | 48 +++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 src/entities/NewGroup.ts diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts index d12ee07..2341ea9 100644 --- a/src/controllers/GroupsController.ts +++ b/src/controllers/GroupsController.ts @@ -2,6 +2,7 @@ import { RequestHandler } from "../entities/RequestHandler"; import { GroupsHelper } from "../helpers/GroupsHelper"; import { GroupsAccessLevel, GroupInfo, GroupVisibilityLevel, GroupPostsCreationLevel, GroupRegistrationLevel } from "../entities/Group"; import { GroupMembershipLevels } from "../entities/GroupMember"; +import { time } from "../utils/DateUtils"; /** * Groups API controller @@ -48,6 +49,26 @@ GROUPS_POSTS_LEVELS[GroupPostsCreationLevel.POSTS_LEVEL_ALL_MEMBERS] = "members" export class GroupsController { + /** + * Create a new group + * + * @param h Request handler + */ + public static async Create(h: RequestHandler) { + const name = h.postString("name", 3); + + const groupID = await GroupsHelper.Create({ + name: name, + userID: h.getUserId(), + timeCreate: time() + }); + + h.send({ + success: "The group has been successfully created!", + id: groupID + }); + } + /** * Get the list of groups of the user * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 27a8894..311cd52 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -85,6 +85,8 @@ export const Routes : Route[] = [ // Groups controller + {path: "/groups/create", cb: (h) => GroupsController.Create(h)}, + {path: "/groups/get_my_list", cb: (h) => GroupsController.GetListUser(h)}, {path: "/groups/get_info", cb: (h) => GroupsController.GetInfoSingle(h)}, diff --git a/src/entities/NewGroup.ts b/src/entities/NewGroup.ts new file mode 100644 index 0000000..303dd75 --- /dev/null +++ b/src/entities/NewGroup.ts @@ -0,0 +1,23 @@ +/** + * New group information + * + * @author Pierre HUBERT + */ + +export interface NewGroup { + + /** + * The name of the group to create + */ + name: string, + + /** + * The ID of the user creating the group + */ + userID: number, + + /** + * The time of creation of the group + */ + timeCreate: number +} \ No newline at end of file diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts index e4f3bbe..453b1f4 100644 --- a/src/helpers/GroupsHelper.ts +++ b/src/helpers/GroupsHelper.ts @@ -1,6 +1,8 @@ import { DatabaseHelper } from "./DatabaseHelper"; import { GroupsAccessLevel, GroupVisibilityLevel, GroupInfo } from "../entities/Group"; import { GroupMembershipLevels, GroupMember } from "../entities/GroupMember"; +import { NewGroup } from "../entities/NewGroup"; +import { time } from "../utils/DateUtils"; /** * Groups helper @@ -13,6 +15,37 @@ const GROUPS_MEMBERS_TABLE = "comunic_groups_members"; export class GroupsHelper { + + /** + * Create a new group + * + * @param info Information about the new group to create + * @throws {Error} In case of error + */ + public static async Create(info: NewGroup) : Promise { + + // Insert the group into the database + const groupID = await DatabaseHelper.InsertRow(GROUPS_LIST_TABLE, { + time_create: info.timeCreate, + userid_create: info.userID, + name: info.name + }); + + if(groupID <= 0) + throw Error("Could not create the group!"); + + await this.InsertMember({ + id: 0, + groupID: groupID, + userID: info.userID, + timeCreate: time(), + level: GroupMembershipLevels.ADMINISTRATOR, + following: true + }); + + return groupID; + } + /** * Check out whether a group exists or not * @@ -85,6 +118,21 @@ export class GroupsHelper { return result.visibility; } + /** + * Insert a new group member + * + * @param member Information about the group member to add + */ + public static async InsertMember(member: GroupMember) { + await DatabaseHelper.InsertRow(GROUPS_MEMBERS_TABLE, { + groups_id: member.groupID, + user_id: member.userID, + time_create: member.timeCreate, + level: member.level + }); + } + + /** * Get the membership level of a user for a group *