diff options
Diffstat (limited to 'src/controller/friend.ts')
-rw-r--r-- | src/controller/friend.ts | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/src/controller/friend.ts b/src/controller/friend.ts index e98f1e6..a73d3d1 100644 --- a/src/controller/friend.ts +++ b/src/controller/friend.ts @@ -37,12 +37,12 @@ const addFriend = async (req: Request, res: Response) => { if (err) { // @ts-ignore switch (err.message) { - case "jwt expired": - res.status(401).json({ error: "Request Expired" }); - break; case "invalid signature": res.status(401).json({ error: "Request Invalid" }); break; + case "jwt expired": + res.status(410).json({ error: "Request Expired" }); + break; default: res.status(500).json({ error: "Internal Server Error" }); console.error(err) @@ -62,8 +62,8 @@ const addFriend = async (req: Request, res: Response) => { if (error.code == "P2002") { res.status(409).json({ error: "Already friends" }); } else { - console.error(error); res.status(500).json({ error: "Internal Server Error" }); + console.error(error); } } } @@ -78,11 +78,16 @@ const addFriend = async (req: Request, res: Response) => { const removeFriend = async (req: Request, res: Response) => { try { // @ts-ignore - const { count } = await unfriend(req.userId, req.body.friendId) - if (count > 0) { - res.status(200).json({ message: "success" }); + if (req.userId == req.body.friendId) { + res.status(400).json({ message: "Attempted to unfriend self" }); } else { - res.status(404).json({ error: "Friend not found" }); + // @ts-ignore + const { count } = await unfriend(req.userId, req.body.friendId) + if (count > 0) { + res.status(200).json({ message: "success" }); + } else { + res.status(404).json({ error: "Friend not found" }); + } } } catch(error) { res.status(500).json({ error: "Internal Server Error" }); @@ -95,7 +100,7 @@ const listFriends = async (req: Request, res: Response) => { // @ts-ignore const friends = await list(req.userId) // @ts-ignore - res.status(200).json({ friends: friends }); + res.status(friends.length > 0 ? 200 : 201).json({ friends: friends }); } catch(error) { res.status(500).json({ error: "Internal Server Error" }); console.error(error); |