updates to friend route, some changes to ui components, front end api tools

This commit is contained in:
Mikayla Dobson
2022-12-01 21:52:54 -06:00
parent 8a7eaa7db6
commit 2e48231dc5
10 changed files with 152 additions and 19 deletions

View File

@@ -64,6 +64,16 @@ export default class UserCtl {
}
}
async getPendingFriendRequests(senderid: string | number) {
try {
const { ok, code, result } = await UserInstance.getPendingFriendRequests(senderid);
if (ok) return result;
throw createError(code, result);
} catch (e: any) {
throw new Error(e);
}
}
async addFriendship(userid: number | string, targetid: number | string) {
try {
const result = await UserInstance.addFriendship(userid, targetid);

View File

@@ -123,6 +123,18 @@ export class User {
}
}
async getPendingFriendRequests(senderid: number | string) {
try {
const statement = `SELECT * FROM recipin.cmp_userfriendships WHERE pending = true AND senderid = $1`
const result = await pool.query(statement, [senderid]);
if (result.rows.length) return { ok: true, code: 200, result: result.rows }
return { ok: true, code: 200, result: "No pending friend requests found" }
} catch (e: any) {
throw new Error(e);
}
}
async addFriendship(userid: number | string, targetid: number | string) {
try {
const statement = `

View File

@@ -23,10 +23,16 @@ export const friendRouter = (app: Express) => {
// get all friendships for a user
router.get('/', async (req, res, next) => {
const { user }: any = req.user;
const { pending } = req.query;
try {
const result = await UserInstance.getFriends(user.id);
res.status(200).send(result);
if (pending) {
const result = await UserInstance.getPendingFriendRequests(user.id);
res.status(200).send(result);
} else {
const result = await UserInstance.getFriends(user.id);
res.status(200).send(result);
}
} catch(e) {
next(e);
}