providing labels for status codes
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { IUser } from '../schemas';
|
import { IUser } from '../schemas';
|
||||||
import { User } from "../models/user";
|
import { User } from "../models/user";
|
||||||
import ControllerResponse from '../util/ControllerResponse';
|
import ControllerResponse from '../util/ControllerResponse';
|
||||||
|
import { StatusCode } from '../util/types';
|
||||||
const UserInstance = new User();
|
const UserInstance = new User();
|
||||||
|
|
||||||
export default class UserCtl {
|
export default class UserCtl {
|
||||||
@@ -10,8 +11,8 @@ export default class UserCtl {
|
|||||||
async getAll() {
|
async getAll() {
|
||||||
try {
|
try {
|
||||||
const users = await UserInstance.getAllUsers();
|
const users = await UserInstance.getAllUsers();
|
||||||
const ok = users.length > 0;
|
const ok: boolean = users !== null;
|
||||||
const code = ok ? 200 : 404;
|
const code: StatusCode = ok ? StatusCode.OK : StatusCode.NotFound;
|
||||||
const data = ok ? users : "No users found.";
|
const data = ok ? users : "No users found.";
|
||||||
return new ControllerResponse(ok, code, data)
|
return new ControllerResponse(ok, code, data)
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
@@ -23,7 +24,7 @@ export default class UserCtl {
|
|||||||
try {
|
try {
|
||||||
const response = await UserInstance.post(body);
|
const response = await UserInstance.post(body);
|
||||||
const ok: boolean = response !== null;
|
const ok: boolean = response !== null;
|
||||||
const code = ok ? 201 : 400
|
const code = ok ? StatusCode.NewContent : StatusCode.BadRequest
|
||||||
const data = ok ? response : "Bad request"
|
const data = ok ? response : "Bad request"
|
||||||
return new ControllerResponse(ok, code, data);
|
return new ControllerResponse(ok, code, data);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
@@ -35,7 +36,7 @@ export default class UserCtl {
|
|||||||
try {
|
try {
|
||||||
const user = await UserInstance.getOneByID(id);
|
const user = await UserInstance.getOneByID(id);
|
||||||
const ok: boolean = user !== null;
|
const ok: boolean = user !== null;
|
||||||
const code = ok ? 200 : 404;
|
const code = ok ? StatusCode.OK : StatusCode.NotFound;
|
||||||
const data = ok ? user : "User by this ID not found";
|
const data = ok ? user : "User by this ID not found";
|
||||||
return new ControllerResponse(ok, code, data);
|
return new ControllerResponse(ok, code, data);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
@@ -47,7 +48,7 @@ export default class UserCtl {
|
|||||||
try {
|
try {
|
||||||
const result = await UserInstance.updateOneByID(id, body);
|
const result = await UserInstance.updateOneByID(id, body);
|
||||||
const ok = result !== null;
|
const ok = result !== null;
|
||||||
const code = ok ? 200 : 400;
|
const code = ok ? StatusCode.OK : StatusCode.BadRequest;
|
||||||
const data = ok ? result : "Update unsuccessful"
|
const data = ok ? result : "Update unsuccessful"
|
||||||
return new ControllerResponse(ok, code, data);
|
return new ControllerResponse(ok, code, data);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
@@ -59,7 +60,7 @@ export default class UserCtl {
|
|||||||
try {
|
try {
|
||||||
const result = await UserInstance.getFriends(id);
|
const result = await UserInstance.getFriends(id);
|
||||||
const ok = result !== null
|
const ok = result !== null
|
||||||
const code = ok ? 200 : 404;
|
const code = ok ? StatusCode.OK : StatusCode.NotFound;
|
||||||
const data = ok ? result : "No friends found"
|
const data = ok ? result : "No friends found"
|
||||||
return new ControllerResponse(ok, code, data);
|
return new ControllerResponse(ok, code, data);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
@@ -89,7 +90,7 @@ export default class UserCtl {
|
|||||||
try {
|
try {
|
||||||
const result = await UserInstance.addFriendship(userid, targetid);
|
const result = await UserInstance.addFriendship(userid, targetid);
|
||||||
const ok = result !== null;
|
const ok = result !== null;
|
||||||
const code = ok ? 201 : 400;
|
const code = ok ? StatusCode.NewContent : StatusCode.BadRequest;
|
||||||
const data = ok ? result : "Something went wrong"
|
const data = ok ? result : "Something went wrong"
|
||||||
return new ControllerResponse(ok, code, data);
|
return new ControllerResponse(ok, code, data);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import fs from "fs";
|
|||||||
import pool from '../db';
|
import pool from '../db';
|
||||||
import now from "../util/now";
|
import now from "../util/now";
|
||||||
import { appRoot } from "../appRoot";
|
import { appRoot } from "../appRoot";
|
||||||
|
import { StatusCode } from "../util/types";
|
||||||
|
|
||||||
export class User {
|
export class User {
|
||||||
async getAllUsers() {
|
async getAllUsers() {
|
||||||
@@ -11,7 +12,7 @@ export class User {
|
|||||||
const statement = `SELECT * FROM recipin.appusers`;
|
const statement = `SELECT * FROM recipin.appusers`;
|
||||||
const result = await pool.query(statement);
|
const result = await pool.query(statement);
|
||||||
if (result.rows.length) return result.rows;
|
if (result.rows.length) return result.rows;
|
||||||
return [];
|
return null;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
throw new Error(error);
|
throw new Error(error);
|
||||||
}
|
}
|
||||||
@@ -115,13 +116,13 @@ export class User {
|
|||||||
if (row.senderid == userid || row.targetid == userid) {
|
if (row.senderid == userid || row.targetid == userid) {
|
||||||
const sql = fs.readFileSync(appRoot + '/db/sql/get/friendshipbyid.sql').toString();
|
const sql = fs.readFileSync(appRoot + '/db/sql/get/friendshipbyid.sql').toString();
|
||||||
const formattedResult = await pool.query(sql, [id]);
|
const formattedResult = await pool.query(sql, [id]);
|
||||||
if (formattedResult.rows.length) return { ok: true, code: 200, result: formattedResult.rows }
|
if (formattedResult.rows.length) return { ok: true, code: StatusCode.OK, result: formattedResult.rows }
|
||||||
return { ok: false, code: 400, result: "Something went wrong" }
|
return { ok: false, code: StatusCode.BadRequest, result: "Something went wrong" }
|
||||||
}
|
}
|
||||||
return { ok: true, code: 403, result: "Not authorized to access this resource" }
|
return { ok: true, code: StatusCode.Forbidden, result: "Not authorized to access this resource" }
|
||||||
}
|
}
|
||||||
|
|
||||||
return { ok: false, code: 404, result: "No friendship found with that ID" }
|
return { ok: false, code: StatusCode.NotFound, result: "No friendship found with that ID" }
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
}
|
}
|
||||||
@@ -132,8 +133,8 @@ export class User {
|
|||||||
const statement = `SELECT * FROM recipin.cmp_userfriendships WHERE pending = true AND senderid = $1`
|
const statement = `SELECT * FROM recipin.cmp_userfriendships WHERE pending = true AND senderid = $1`
|
||||||
const result = await pool.query(statement, [senderid]);
|
const result = await pool.query(statement, [senderid]);
|
||||||
|
|
||||||
if (result.rows.length) return { ok: true, code: 200, result: result.rows }
|
if (result.rows.length) return { ok: true, code: StatusCode.OK, result: result.rows }
|
||||||
return { ok: true, code: 200, result: "No pending friend requests found" }
|
return { ok: true, code: StatusCode.NotFound, result: "No pending friend requests found" }
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
}
|
}
|
||||||
@@ -162,9 +163,9 @@ export class User {
|
|||||||
try {
|
try {
|
||||||
const query = `SELECT * FROM recipin.cmp_userfriendships WHERE id = $1`;
|
const query = `SELECT * FROM recipin.cmp_userfriendships WHERE id = $1`;
|
||||||
const friendship = await pool.query(query, [id]);
|
const friendship = await pool.query(query, [id]);
|
||||||
if (!friendship.rows.length) return { ok: false, code: 404, result: "Friendship with this code not found" };
|
if (!friendship.rows.length) return { ok: false, code: StatusCode.NotFound, result: "Friendship with this code not found" };
|
||||||
if (!(friendship.rows[0].active) && friendship.rows[0].senderid == userid) {
|
if (!(friendship.rows[0].active) && friendship.rows[0].senderid == userid) {
|
||||||
return { ok: false, code: 403, result: "Please wait for friend request to be accepted" }
|
return { ok: false, code: StatusCode.Forbidden, result: "Please wait for friend request to be accepted" }
|
||||||
}
|
}
|
||||||
|
|
||||||
const statement = `
|
const statement = `
|
||||||
@@ -177,8 +178,8 @@ export class User {
|
|||||||
`
|
`
|
||||||
const values = [data.active, data.pending, (data.dateterminated || null), id];
|
const values = [data.active, data.pending, (data.dateterminated || null), id];
|
||||||
const result = await pool.query(statement, values);
|
const result = await pool.query(statement, values);
|
||||||
if (result.rows.length) return { ok: true, code: 200, result: result.rows[0] }
|
if (result.rows.length) return { ok: true, code: StatusCode.OK, result: result.rows[0] }
|
||||||
return { ok: false, code: 400, result: "Bad request" }
|
return { ok: false, code: StatusCode.BadRequest, result: "Bad request" }
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import { CtlResponse } from "./types";
|
import { CtlResponse, StatusCode } from "./types";
|
||||||
|
|
||||||
export default class ControllerResponse<T> implements CtlResponse<T> {
|
export default class ControllerResponse<T> implements CtlResponse<T> {
|
||||||
ok: boolean
|
ok: boolean
|
||||||
code: number
|
code: StatusCode
|
||||||
data: T | T[] | string
|
data: T | string
|
||||||
|
|
||||||
constructor(ok: boolean, code: number, data: T | T[] | string) {
|
constructor(ok: boolean, code: StatusCode, data: T | string) {
|
||||||
this.ok = ok
|
this.ok = ok
|
||||||
this.code = code
|
this.code = code
|
||||||
this.data = data
|
this.data = data
|
||||||
|
|||||||
@@ -1,5 +1,17 @@
|
|||||||
export interface CtlResponse<T> {
|
export interface CtlResponse<T> {
|
||||||
ok: boolean
|
ok: boolean
|
||||||
code: number
|
code: number
|
||||||
data: T | T[] | string
|
data: T | string
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum StatusCode {
|
||||||
|
OK = 200,
|
||||||
|
NewContent = 201,
|
||||||
|
NoContent = 204,
|
||||||
|
NotModified = 304,
|
||||||
|
BadRequest = 400,
|
||||||
|
Unauthorized = 401,
|
||||||
|
Forbidden = 403,
|
||||||
|
NotFound = 404,
|
||||||
|
ServerError = 500
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user