1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-26 07:19:23 +00:00

Can fetch basic information about a user

This commit is contained in:
Pierre HUBERT 2019-11-23 15:11:33 +01:00
parent c1068be8f1
commit 53a616da15
4 changed files with 136 additions and 0 deletions

View File

@ -1,6 +1,7 @@
import { WelcomeController } from "./WelcomeController"; import { WelcomeController } from "./WelcomeController";
import { RequestHandler } from "../entities/RequestHandler"; import { RequestHandler } from "../entities/RequestHandler";
import { AccountController } from "./AccountController"; import { AccountController } from "./AccountController";
import { UserController } from "./UserController";
/** /**
* Controllers routes * Controllers routes
@ -34,4 +35,12 @@ export const Routes : Route[] = [
{path: "/account/id", cb: AccountController.CurrentUserID}, {path: "/account/id", cb: AccountController.CurrentUserID},
{path: "/user/getCurrentUserID", cb: AccountController.CurrentUserID}, // Legacy {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
] ]

View File

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

39
src/entities/User.ts Normal file
View File

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

45
src/helpers/UserHelper.ts Normal file
View File

@ -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<User|null> {
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)
});
}
}