api maintenance

This commit is contained in:
Mikayla Dobson
2023-02-18 10:58:58 -06:00
parent 9e146f0825
commit a7f3fd6e10
18 changed files with 180 additions and 32 deletions

4
server/controllers/UserCtl.d.ts vendored Normal file
View File

@@ -0,0 +1,4 @@
/**
* @method getAll
* @returns { ControllerResponse<IUser[] | string> }
*/

View File

@@ -5,6 +5,18 @@ import { StatusCode } from '../util/types';
const UserInstance = new User();
export default class UserCtl {
/* * * * * * * * * * * * * * * * * * * * * * * * * * *
* FIRST SECTION:
* METHODS SPECIFIC TO USERS AND USER DATA
* * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* ### @method getAll
* returns all available user entries
*
* @params (none)
* @returns list of users, or an explanatory string if no response is received
*/
async getAll() {
try {
// attempt to get users from database
@@ -22,6 +34,11 @@ export default class UserCtl {
}
}
/**
* ### @method post
* @param body - serialized user data as { IUser }
* @returns the newly inserted user entry, or an explanatory string
*/
async post(body: IUser) {
try {
const response = await UserInstance.post(body);
@@ -34,6 +51,11 @@ export default class UserCtl {
}
}
/**
* ### @method getOne
* @param id - user id to query
* @returns the user entry, if found, or an explanatory string if none was found
*/
async getOne(id: number | string) {
try {
const user = await UserInstance.getOneByID(id);
@@ -46,6 +68,12 @@ export default class UserCtl {
}
}
/**
* ### @method updateOne
* @param id - user id to update
* @param body - the new user body to update with
* @returns the updated user body, or an explanatory string
*/
async updateOne(id: number | string, body: IUser) {
try {
const result = await UserInstance.updateOneByID(id, body);
@@ -58,6 +86,16 @@ export default class UserCtl {
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * *
* SECOND SECTION:
* METHODS SPECIFIC TO FRIENDSHIPS BETWEEN USERS
* * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* ### @method getFriends
* @param id - get all friendship entries for a user, regardless of status
* @returns a list of friendship entries, or an explanatory string if none are found
*/
async getFriends(id: number | string) {
try {
const result = await UserInstance.getFriends(id);
@@ -70,6 +108,12 @@ export default class UserCtl {
}
}
/**
* ### @method getFriendshipByID
* @param id - the ID of the friendship in question
* @param userid - the user ID of the logged in user, to verify permissions
* @returns a friendship entry, or an explanatory string
*/
async getFriendshipByID(id: number | string, userid: number | string) {
try {
const { ok, code, result } = await UserInstance.getFriendshipByID(id, userid);
@@ -79,6 +123,11 @@ export default class UserCtl {
}
}
/**
* ### @method getPendingFriendRequests
*
* *IMPORTANT*: I don't think this one works the way I think it does
*/
async getPendingFriendRequests(recipient: string | number) {
try {
const { ok, code, result } = await UserInstance.getPendingFriendRequests(recipient);
@@ -88,6 +137,15 @@ export default class UserCtl {
}
}
async getAcceptedFriends(userid: number | string) {
try {
const { code, result } = await UserInstance.getAcceptedFriends(userid);
return new ControllerResponse(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

@@ -140,6 +140,18 @@ export class User {
}
}
async getAcceptedFriends(userid: number | string) {
try {
const statement = `SELECT * FROM recipin.cmp_userfriendships WHERE active = true AND (senderid = $1) OR (targetid = $1);`
const result = await pool.query(statement, [userid]);
if (result.rows.length) return { ok: true, code: StatusCode.OK, result: result.rows }
return { ok: true, code: StatusCode.NotFound, 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

@@ -2,6 +2,7 @@ import { Express, Router } from 'express';
import { restrictAccess } from '../auth/middlewares';
import { UserCtl } from '../controllers';
import { IUser } from '../schemas';
import { StatusCode } from '../util/types';
const UserInstance = new UserCtl();
const router = Router();
@@ -24,12 +25,15 @@ export const friendRouter = (app: Express) => {
// get all friendships for a user
router.get('/', async (req, res, next) => {
const user = req.user as IUser;
const { pending, targetUser } = req.query;
const { pending, accepted, targetUser } = req.query;
try {
if (pending) {
const { code, data } = await UserInstance.getPendingFriendRequests(user.id as number);
res.status(code).send(data);
} else if (accepted) {
const { code, data } = await UserInstance.getAcceptedFriends(user.id as number);
res.status(code).send(data);
} else {
if (targetUser) {
const { code, data } = await UserInstance.getFriends(parseInt(targetUser as string));
@@ -39,6 +43,9 @@ export const friendRouter = (app: Express) => {
res.status(code).send(data);
}
}
// send server error in case any of these conditions not landing
res.status(StatusCode.ServerError).json({ message: "An unexpected error occurred." });
} catch(e) {
next(e);
}