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

Can create new groups

This commit is contained in:
Pierre HUBERT 2019-12-24 18:47:55 +01:00
parent 679bf0e04e
commit e85bdf8e54
4 changed files with 94 additions and 0 deletions

View File

@ -2,6 +2,7 @@ import { RequestHandler } from "../entities/RequestHandler";
import { GroupsHelper } from "../helpers/GroupsHelper"; import { GroupsHelper } from "../helpers/GroupsHelper";
import { GroupsAccessLevel, GroupInfo, GroupVisibilityLevel, GroupPostsCreationLevel, GroupRegistrationLevel } from "../entities/Group"; import { GroupsAccessLevel, GroupInfo, GroupVisibilityLevel, GroupPostsCreationLevel, GroupRegistrationLevel } from "../entities/Group";
import { GroupMembershipLevels } from "../entities/GroupMember"; import { GroupMembershipLevels } from "../entities/GroupMember";
import { time } from "../utils/DateUtils";
/** /**
* Groups API controller * Groups API controller
@ -48,6 +49,26 @@ GROUPS_POSTS_LEVELS[GroupPostsCreationLevel.POSTS_LEVEL_ALL_MEMBERS] = "members"
export class GroupsController { 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 * Get the list of groups of the user
* *

View File

@ -85,6 +85,8 @@ export const Routes : Route[] = [
// Groups controller // Groups controller
{path: "/groups/create", cb: (h) => GroupsController.Create(h)},
{path: "/groups/get_my_list", cb: (h) => GroupsController.GetListUser(h)}, {path: "/groups/get_my_list", cb: (h) => GroupsController.GetListUser(h)},
{path: "/groups/get_info", cb: (h) => GroupsController.GetInfoSingle(h)}, {path: "/groups/get_info", cb: (h) => GroupsController.GetInfoSingle(h)},

23
src/entities/NewGroup.ts Normal file
View File

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

View File

@ -1,6 +1,8 @@
import { DatabaseHelper } from "./DatabaseHelper"; import { DatabaseHelper } from "./DatabaseHelper";
import { GroupsAccessLevel, GroupVisibilityLevel, GroupInfo } from "../entities/Group"; import { GroupsAccessLevel, GroupVisibilityLevel, GroupInfo } from "../entities/Group";
import { GroupMembershipLevels, GroupMember } from "../entities/GroupMember"; import { GroupMembershipLevels, GroupMember } from "../entities/GroupMember";
import { NewGroup } from "../entities/NewGroup";
import { time } from "../utils/DateUtils";
/** /**
* Groups helper * Groups helper
@ -13,6 +15,37 @@ const GROUPS_MEMBERS_TABLE = "comunic_groups_members";
export class GroupsHelper { 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<number> {
// 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 * Check out whether a group exists or not
* *
@ -85,6 +118,21 @@ export class GroupsHelper {
return result.visibility; 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 * Get the membership level of a user for a group
* *