refactoring on back end

This commit is contained in:
Mikayla Dobson
2022-12-17 13:30:55 -06:00
parent be375aad8f
commit 9a43f58ee3
15 changed files with 117 additions and 90 deletions

View File

@@ -1,46 +1,50 @@
import createError from "http-errors";
import { ICollection } from "../schemas";
import { Collection } from "../models/collection";
import { StatusCode } from "../util/types";
import ControllerResponse from "../util/ControllerResponse";
const CollectionInstance = new Collection();
export default class CollectionCtl {
async getOne(id: number | string) {
const result = await CollectionInstance.getOne(id);
if (!result) throw createError('404', 'Collection not found');
return result;
const ok: boolean = result !== null;
const code: StatusCode = ok ? StatusCode.OK : StatusCode.NotFound;
const data: string | ICollection = result || "No collection found with this ID";
return new ControllerResponse(code, data);
}
async getAll() {
const result = await CollectionInstance.getAll();
if (!result) throw createError('404', 'No collections found');
return result;
const ok = result !== null;
const code = ok ? StatusCode.OK : StatusCode.NotFound;
const data = result || "No collections found"
return new ControllerResponse(code, data);
}
async getUserDefault(id: number | string) {
const result = await CollectionInstance.getUserDefault(id);
if (!result) throw createError('404', "No default collection found. Contact an admin, this isn't supposed to happen");
return result;
const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound;
const data = result || "No default collection found"
return new ControllerResponse(code, data);
}
async post(data: ICollection) {
const result = await CollectionInstance.post(data);
if (!result) throw createError('400', 'Bad request');
return result;
async post(body: ICollection) {
const result = await CollectionInstance.post(body);
const code = (result !== null) ? StatusCode.NewContent : StatusCode.BadRequest;
const data = result || "Something went wrong"
return new ControllerResponse(code, data);
}
async getSubscriptions(userid: number | string) {
const result = await CollectionInstance.getSubscriptions(userid);
if (!result) throw createError('404', 'No subscriptions found');
return result;
const code = (result !== null) ? StatusCode.OK : StatusCode.NoContent;
return new ControllerResponse(code, (result || "No subscriptions found"));
}
async postSubscription(collectionid: number | string, userid: number | string) {
const { ok, code, data } = await CollectionInstance.postSubscription(collectionid, userid);
if (ok) {
return data;
} else {
throw createError(code, data as string);
}
const { code, data } = await CollectionInstance.postSubscription(collectionid, userid);
return new ControllerResponse(code, data);
}
}

View File

@@ -1,14 +1,16 @@
import createError from 'http-errors';
import { ICourse } from '../schemas';
import Course from '../models/course';
import ControllerResponse from '../util/ControllerResponse';
import { StatusCode } from '../util/types';
const CourseInstance = new Course();
export default class CourseCtl {
async getAll() {
try {
const result = await CourseInstance.getAll();
if (!result) throw createError('404', 'No cuisines found');
return result;
const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound;
return new ControllerResponse(code, (result || "No course data found"));
} catch (e: any) {
throw new Error(e);
}
@@ -17,8 +19,8 @@ export default class CourseCtl {
async getOne(id: string) {
try {
const result = await CourseInstance.getOne(id);
if (!result) throw createError('404', 'No cuisine found with id ' + id);
return result;
const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound;
return new ControllerResponse(code, (result || "No course found with this ID"))
} catch (e: any) {
throw new Error(e);
}
@@ -27,8 +29,8 @@ export default class CourseCtl {
async post(data: ICourse) {
try {
const result = await CourseInstance.post(data);
if (!result) throw createError('400', 'Bad request');
return result;
const code = (result !== null) ? StatusCode.NewContent : StatusCode.BadRequest;
return new ControllerResponse(code, (result || "Something went wrong"));
} catch (e: any) {
throw new Error(e);
}

View File

@@ -1,14 +1,16 @@
import createError from "http-errors";
import { IGroceryList } from "../schemas";
import { GroceryList } from "../models/groceryList";
import { StatusCode } from "../util/types";
import ControllerResponse from "../util/ControllerResponse";
const GroceryInstance = new GroceryList();
export default class GroceryListCtl {
async getAll() {
try {
const result = await GroceryInstance.getAll();
if (!result) throw createError('404', 'No results found');
return result;
const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound;
return new ControllerResponse(code, (result || "No grocery list data found"));
} catch (e: any) {
throw new Error(e);
}
@@ -17,8 +19,8 @@ export default class GroceryListCtl {
async getOne(id: string) {
try {
const result = await GroceryInstance.getOne(id);
if (!result) throw createError('404', 'No results found');
return result;
const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound;
return new ControllerResponse(code, (result || "No grocery list data found with this ID"));
} catch (e: any) {
throw new Error(e);
}
@@ -27,8 +29,8 @@ export default class GroceryListCtl {
async getByUserID(userid: string) {
try {
const result = await GroceryInstance.getByUserID(userid);
if (!result) throw createError('404', 'No results found');
return result;
const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound;
return new ControllerResponse(code, (result || "No grocery list data found matching this user ID"));
} catch (e: any) {
throw new Error(e);
}
@@ -37,8 +39,8 @@ export default class GroceryListCtl {
async post(data: IGroceryList) {
try {
const result = await GroceryInstance.post(data);
if (!result) throw createError('400', 'Bad request');
return result;
const code = (result !== null) ? StatusCode.NewContent : StatusCode.BadRequest;
return new ControllerResponse(code, (result || "Something went wrong"));
} catch (e: any) {
throw new Error(e);
}
@@ -47,8 +49,8 @@ export default class GroceryListCtl {
async put(id: string, data: IGroceryList) {
try {
const result = await GroceryInstance.put(id, data);
if (!result) throw createError('400', 'Bad request');
return result;
const code = (result !== null) ? StatusCode.OK : StatusCode.BadRequest;
return new ControllerResponse(code, (result || "Something went wrong"));
} catch (e: any) {
throw new Error(e);
}

View File

@@ -1,6 +1,7 @@
import { IIngredient } from "../schemas";
import { Ingredient } from "../models/ingredient";
import ControllerResponse from "../util/ControllerResponse";
import { StatusCode } from "../util/types";
const IngredientInstance = new Ingredient();
export default class IngredientCtl {
@@ -8,7 +9,8 @@ export default class IngredientCtl {
try {
const result = await IngredientInstance.getAll();
const ok = result !== null;
return new ControllerResponse(ok, (ok ? 200 : 404), (ok ? result : "No recipes found"));
const code = ok ? StatusCode.OK : StatusCode.NotFound;
return new ControllerResponse(code, (result || "No ingredients found"));
} catch (e: any) {
throw new Error(e);
}
@@ -18,7 +20,8 @@ export default class IngredientCtl {
try {
const result = await IngredientInstance.getOne(id);
const ok = result !== null;
return new ControllerResponse(ok, (ok ? 200 : 404), (ok ? result : "No recipe found with this ID"));
const code = ok ? StatusCode.OK : StatusCode.NotFound;
return new ControllerResponse(code, (result || "No ingredient found with this ID"));
} catch (e: any) {
throw new Error(e);
}
@@ -28,7 +31,8 @@ export default class IngredientCtl {
try {
const result = await IngredientInstance.post(data);
const ok = result !== null;
return new ControllerResponse(ok, (ok ? 201 : 400), (ok ? result : "Something went wrong"));
const code = ok ? StatusCode.NewContent : StatusCode.BadRequest;
return new ControllerResponse(code, (result || "Something went wrong"));
} catch (e: any) {
throw new Error(e);
}
@@ -38,7 +42,8 @@ export default class IngredientCtl {
try {
const result = await IngredientInstance.put(id, data);
const ok = result !== null;
return new ControllerResponse(ok, (ok ? 200 : 400), (ok ? result : "Something went wrong"));
const code = ok ? StatusCode.OK : StatusCode.BadRequest;
return new ControllerResponse(code, (result || "Something went wrong"));
} catch (e: any) {
throw new Error(e);
}

View File

@@ -1,6 +1,7 @@
import { IRecipe } from "../schemas";
import { Recipe } from "../models/recipe";
import ControllerResponse from "../util/ControllerResponse";
import { StatusCode } from "../util/types";
const RecipeInstance = new Recipe();
export default class RecipeCtl {
@@ -8,7 +9,8 @@ export default class RecipeCtl {
try {
const result = await RecipeInstance.getOneByID(id);
const ok = result !== null;
return new ControllerResponse(ok, (ok ? 200 : 404), (ok ? result : "No recipe found with this ID"));
const code = ok ? StatusCode.OK : StatusCode.NotFound;
return new ControllerResponse(code, (result || "No recipe found with this ID"));
} catch (error: any) {
throw new Error(error);
}
@@ -18,7 +20,8 @@ export default class RecipeCtl {
try {
const result = await RecipeInstance.getAllAuthored(id);
const ok = result !== null;
return new ControllerResponse(ok, (ok ? 200 : 404), (ok ? result : "No recipes found authored by user with this ID"));
const code = ok ? StatusCode.OK : StatusCode.NotFound;
return new ControllerResponse(code, (result || "No recipes found for this user"));
} catch (e: any) {
throw new Error(e);
}
@@ -36,7 +39,8 @@ export default class RecipeCtl {
try {
const result = await RecipeInstance.updateOneByID(id, data);
const ok = result !== null;
return new ControllerResponse(ok, (ok ? 200 : 400), (ok ? result : "Something went wrong"));
const code = ok ? StatusCode.OK : StatusCode.BadRequest;
return new ControllerResponse(code, (result || "Something went wrong"));
} catch (error: any) {
throw new Error(error);
}
@@ -46,7 +50,8 @@ export default class RecipeCtl {
try {
const result = await RecipeInstance.post(userid, data);
const ok = result !== null;
return new ControllerResponse(ok, (ok ? 201 : 400), (ok ? result : "Something went wrong"));
const code = ok ? StatusCode.NewContent : StatusCode.BadRequest;
return new ControllerResponse(code, (result || "Something went wrong"));
} catch (error: any) {
throw new Error(error);
}

View File

@@ -16,7 +16,7 @@ export default class UserCtl {
const data: IUser[] | string = ok ? users! : "No users found.";
// send formatted response with either data or informative error message
return new ControllerResponse<IUser[] | string>(ok, code, data)
return new ControllerResponse<IUser[] | string>(code, data, ok)
} catch (error: any) {
throw new Error(error);
}
@@ -28,7 +28,7 @@ export default class UserCtl {
const ok = response !== null;
const code = ok ? StatusCode.NewContent : StatusCode.BadRequest
const data = ok ? response : "Bad request"
return new ControllerResponse(ok, code, data);
return new ControllerResponse(code, data);
} catch (error: any) {
throw new Error(error);
}
@@ -40,7 +40,7 @@ export default class UserCtl {
const ok = user !== null;
const code = ok ? StatusCode.OK : StatusCode.NotFound;
const data = ok ? user : "User by this ID not found";
return new ControllerResponse(ok, code, data);
return new ControllerResponse(code, data);
} catch (error: any) {
throw new Error(error);
}
@@ -52,7 +52,7 @@ export default class UserCtl {
const ok = result !== null;
const code = ok ? StatusCode.OK : StatusCode.BadRequest;
const data = ok ? result : "Update unsuccessful"
return new ControllerResponse(ok, code, data);
return new ControllerResponse(code, data);
} catch (error: any) {
throw new Error(error);
}
@@ -64,7 +64,7 @@ export default class UserCtl {
const ok = result !== null
const code = ok ? StatusCode.OK : StatusCode.NotFound;
const data = ok ? result : "No friends found"
return new ControllerResponse(ok, code, data);
return new ControllerResponse(code, data);
} catch (e: any) {
throw new Error(e);
}
@@ -73,7 +73,7 @@ export default class UserCtl {
async getFriendshipByID(id: number | string, userid: number | string) {
try {
const { ok, code, result } = await UserInstance.getFriendshipByID(id, userid);
return new ControllerResponse(ok, code, result);
return new ControllerResponse(code, result);
} catch (e: any) {
throw new Error(e);
}
@@ -82,7 +82,7 @@ export default class UserCtl {
async getPendingFriendRequests(senderid: string | number) {
try {
const { ok, code, result } = await UserInstance.getPendingFriendRequests(senderid);
return new ControllerResponse(ok, code, result);
return new ControllerResponse(code, result);
} catch (e: any) {
throw new Error(e);
}
@@ -94,7 +94,7 @@ export default class UserCtl {
const ok = result !== null;
const code = ok ? StatusCode.NewContent : StatusCode.BadRequest;
const data = ok ? result : "Something went wrong"
return new ControllerResponse(ok, code, data);
return new ControllerResponse(code, data);
} catch (e: any) {
throw new Error(e);
}
@@ -103,7 +103,7 @@ export default class UserCtl {
async updateFriendship(id: number | string, userid: number | string, data: { active: boolean, pending: boolean, dateterminated?: string }) {
try {
const { ok, code, result } = await UserInstance.updateFriendship(id, userid, data);
return new ControllerResponse(ok, code, result);
return new ControllerResponse(code, result);
} catch (e: any) {
throw new Error(e);
}