From 1bf7b8e22e99bd143fad5677cbd8206bbc75d2b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Thu, 26 Aug 2021 18:22:49 +0200 Subject: [PATCH] API-18: Friendship data endpoint --- src/models/partyplaner/data/Data.router.ts | 32 +++++++++++ src/models/partyplaner/data/data.service.ts | 54 ++++++++++++++++++- .../partyplaner/userService/user.service.ts | 36 ++++++------- 3 files changed, 103 insertions(+), 19 deletions(-) diff --git a/src/models/partyplaner/data/Data.router.ts b/src/models/partyplaner/data/Data.router.ts index e60b51b..0f4917d 100644 --- a/src/models/partyplaner/data/Data.router.ts +++ b/src/models/partyplaner/data/Data.router.ts @@ -74,3 +74,35 @@ dataRouter.get('/session/:isDevCall', async (req: Request, res: Response) => { res.status(500).send({'message': 'Internal Server Error. Try again later.'}); } }); + +dataRouter.get('/friendship/:isDevCall', async (req: Request, res: Response) => { + try { + let userId = (req.query.userId ?? '').toString(); + let sessionId = (req.query.sessionId ?? '').toString(); + let sessionKey = (req.query.sessionKey ?? '').toString(); + let useDev: boolean = (req.params.isDevCall ?? '') === 'dev'; // TBD + + if (userId === '' || sessionId === '' || sessionKey === '') { + res.status(400).send({ + 'status': 'WRONG_PARAMS', + 'message': 'Missing or wrong parameters' + }); + return; + } + + if (!await UserService.checkSession(useDev, userId, sessionId, sessionKey)) { + res.status(403).send({ + 'status': 'INVALID_SESSION', + 'message': 'The user or session could not be found or the session is invalid' + }); + return; + } + + let data = await DataService.getFriendshipData(useDev, userId); + + res.status(200).send(data); + } catch (e) { + logger.error('Error handling a request: ' + e.message); + res.status(500).send({'message': 'Internal Server Error. Try again later.'}); + } +}); diff --git a/src/models/partyplaner/data/data.service.ts b/src/models/partyplaner/data/data.service.ts index 61d4d97..5ff9ecd 100644 --- a/src/models/partyplaner/data/data.service.ts +++ b/src/models/partyplaner/data/data.service.ts @@ -41,6 +41,17 @@ export interface SessionData { lastIp: string; } +/** + * Used in the getFriendshipData method as a return value + */ +export interface Friendship { + friendshipId: string; + friendId: string; + friendFirstName: string; + friendLastName: string; + friendUsername: string; +} + /** * Returns all data about the given user * @param useDev If the dev or prod database should be used @@ -88,7 +99,7 @@ export const getUserData = async (useDev: boolean, userId: string): Promise => { let conn; @@ -123,3 +134,44 @@ export const getSessionData = async (useDev: boolean, userId: string): Promise => { + let conn; + try { + if (useDev) { + conn = await dev_pool.getConnection(); + } else { + conn = await prod_pool.getConnection(); + } + + let rows = await conn.query('SELECT f.friendship_id, f.friend_id, u.first_name as friend_first_name, u.last_name as friend_last_name, u.username as friend_username FROM friendships f LEFT OUTER JOIN users u ON f.friend_id = u.user_id WHERE f.user_id = ?', userId); + + let friends: Friendship[] = []; + + for (let row in rows) { + if (row !== 'meta') { + friends.push({ + friendshipId: rows[row].friendship_id, + friendId: rows[row].friend_id, + friendFirstName: rows[row].friend_first_name, + friendLastName: rows[row].friend_last_name, + friendUsername: rows[row].friend_username + }); + } + } + + return friends; + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } +}; diff --git a/src/models/partyplaner/userService/user.service.ts b/src/models/partyplaner/userService/user.service.ts index 966c8a8..31183fe 100644 --- a/src/models/partyplaner/userService/user.service.ts +++ b/src/models/partyplaner/userService/user.service.ts @@ -20,6 +20,24 @@ const dev_pool = mariadb.createPool({ connectionLimit: 5 }); +/** + * Used in the registerUser and loginUser methods as return value + */ +export interface Session { + userId: string; + sessionId: string; + sessionKey: string; +} + +/** + * Used in the checkUsernameAndEmail method as return value + */ +export interface Status { + hasProblems: boolean; + messages: string[]; + status: string[]; +} + /** * Fetches all usernames and emails from the database and returns them in an object * @param useDev If the dev or prod database should be used @@ -59,15 +77,6 @@ export const getExistingUsernamesAndEmails = async (useDev: boolean): Promise