From 53a616da15d827f6b7b85cab6923652b70b33b68 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 23 Nov 2019 15:11:33 +0100 Subject: [PATCH] Can fetch basic information about a user --- src/controllers/Routes.ts | 9 +++++++ src/controllers/UserController.ts | 43 +++++++++++++++++++++++++++++ src/entities/User.ts | 39 +++++++++++++++++++++++++++ src/helpers/UserHelper.ts | 45 +++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 src/controllers/UserController.ts create mode 100644 src/entities/User.ts create mode 100644 src/helpers/UserHelper.ts diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index cd33465..d7e532d 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -1,6 +1,7 @@ import { WelcomeController } from "./WelcomeController"; import { RequestHandler } from "../entities/RequestHandler"; import { AccountController } from "./AccountController"; +import { UserController } from "./UserController"; /** * Controllers routes @@ -34,4 +35,12 @@ export const Routes : Route[] = [ {path: "/account/id", cb: AccountController.CurrentUserID}, {path: "/user/getCurrentUserID", cb: AccountController.CurrentUserID}, // Legacy + + + // User controller + {path: "/user/getInfo", cb: UserController.GetSingle, needLogin: false}, + {path: "/user/getInfos", cb: UserController.GetSingle, needLogin: false}, // Legacy + + {path: "/user/getInfoMultiple", cb: UserController.GetMultiple, needLogin: false}, + {path: "/user/getInfosMultiple", cb: UserController.GetMultiple, needLogin: false}, // Legacy ] \ No newline at end of file diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts new file mode 100644 index 0000000..d9bbca6 --- /dev/null +++ b/src/controllers/UserController.ts @@ -0,0 +1,43 @@ +import { RequestHandler } from "../entities/RequestHandler"; +import { UserHelper } from "../helpers/UserHelper"; +import { User, UserPageStatus } from "../entities/User"; + +/** + * User information controller + * + * @author Pierre HUBERT + */ + +export class UserController { + + /** + * Get information about a single user + */ + public static async GetSingle(handler : RequestHandler) { + const userID = handler.postInt("userID"); + const user = await UserHelper.GetUserInfo(userID); + + if(!user) + handler.error(404, "Could not get user data!"); + + handler.send(UserController.UserToAPI(user)); + } + + /** + * Get information about multiple users + */ + public static async GetMultiple(handler : RequestHandler) { + + } + + private static UserToAPI(user : User) : any { + return { + "userID": user.id, + "firstName": user.firstName, + "lastName": user.lastName, + "publicPage": user.pageStatus == UserPageStatus.PUBLIC, + "openPage": user.pageStatus == UserPageStatus.OPEN, + "virtualDirectory": user.virtualDirectory, + }; + } +} \ No newline at end of file diff --git a/src/entities/User.ts b/src/entities/User.ts new file mode 100644 index 0000000..f53d046 --- /dev/null +++ b/src/entities/User.ts @@ -0,0 +1,39 @@ +/** + * User information + * + * @author Pierre HUBERT + */ + +export enum UserPageStatus { + PRIVATE, + PUBLIC, + OPEN +} + +export interface UserBuilder { + id: number, + firstName: string, + lastName: string, + timeCreate: number, + virtualDirectory: string, + pageStatus: UserPageStatus, +} + +export class User { + id: number; + firstName: string; + lastName: string; + timeCreate: number; + virtualDirectory: string; + pageStatus: UserPageStatus; + + public constructor(info : UserBuilder) { + this.id = info.id; + this.firstName = info.firstName; + this.lastName = info.lastName; + this.timeCreate = info.timeCreate; + this.virtualDirectory = info.virtualDirectory; + this.pageStatus = info.pageStatus; + } + +} \ No newline at end of file diff --git a/src/helpers/UserHelper.ts b/src/helpers/UserHelper.ts new file mode 100644 index 0000000..a317830 --- /dev/null +++ b/src/helpers/UserHelper.ts @@ -0,0 +1,45 @@ +import { User, UserPageStatus } from "../entities/User"; +import { DatabaseHelper } from "./DatabaseHelper"; + +/** + * User helper + * + * @author Pierre HUBERT + */ + +const TABLE_NAME = "utilisateurs"; + +export class UserHelper { + + /** + * Get information a single user + * + * @param id The ID of the user to get + * @returns Information about the user | null if not found + */ + public static async GetUserInfo(id: number) : Promise { + const result = await DatabaseHelper.QueryRow({ + table: TABLE_NAME, + where: { + ID: id + } + }); + + if(!result) + return null; + + return this.DbToUser(result); + } + + + private static DbToUser(row: any) : User { + return new User({ + id: row.ID, + firstName: row.prenom, + lastName: row.nom, + timeCreate: new Date(row.date_creation).getTime()/1000, + virtualDirectory: row.sous_repertoire, + pageStatus: row.pageouverte == 1 ? UserPageStatus.OPEN : (row.public == 1 ? UserPageStatus.PUBLIC : UserPageStatus.PRIVATE) + }); + } +} \ No newline at end of file