plutoapi-v2/src/models/partyplaner/friendship/friendship.service.ts

38 lines
1.2 KiB
TypeScript

import * as dotenv from 'dotenv';
import {Friendship} from './Friendship.interface';
import {PartyPlanerDB} from '../PartyPlaner.db';
dotenv.config();
/**
* Returns all friends of the given user
* @param useDev If the dev or prod database should be used
* @param userId The userId of the user to fetch the friends for
* @return Friendship[] A list of friends
*/
export const getFriendshipData = async (useDev: boolean, userId: string): Promise<Friendship[]> => {
let conn = await PartyPlanerDB.getConnection(useDev);
try {
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 of rows) {
friends.push({
friendshipId: row.friendship_id,
friendId: row.friend_id,
friendFirstName: row.friend_first_name,
friendLastName: row.friend_last_name,
friendUsername: row.friend_username
});
}
return friends;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};