mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-04 03:24:04 +00:00 
			
		
		
		
	Can create new groups
This commit is contained in:
		@@ -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
 | 
			
		||||
	 * 
 | 
			
		||||
 
 | 
			
		||||
@@ -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)},
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										23
									
								
								src/entities/NewGroup.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/entities/NewGroup.ts
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
@@ -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<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
 | 
			
		||||
	 * 
 | 
			
		||||
@@ -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
 | 
			
		||||
	 * 
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user