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

@@ -11,8 +11,8 @@ export const collectionRoute = (app: Express) => {
router.get('/:id', restrictAccess, async (req, res, next) => {
const { id } = req.params;
try {
const result = await CollectionInstance.getOne(id);
res.status(200).send(result);
const { code, data } = await CollectionInstance.getOne(id);
res.status(code).send(data);
} catch(e) {
next(e);
}
@@ -21,8 +21,8 @@ export const collectionRoute = (app: Express) => {
// implement is admin on this route
router.get('/', restrictAccess, async (req, res, next) => {
try {
const result = await CollectionInstance.getAll();
res.status(200).send(result);
const { code, data } = await CollectionInstance.getAll();
res.status(code).send(data);
} catch(e) {
next(e);
}
@@ -32,7 +32,7 @@ export const collectionRoute = (app: Express) => {
const data = req.body;
try {
const result = await CollectionInstance.post(data);
res.status(201).send(result);
res.status(result.code).send(result.data);
} catch(e) {
next(e);
}

View File

@@ -8,8 +8,8 @@ export const courseRouter = (app: Express) => {
router.get('/', async (req, res, next) => {
try {
const result = await CourseInstance.getAll();
res.status(200).send(result);
const { code, data } = await CourseInstance.getAll();
res.status(code).send(data);
} catch(e) {
next(e);
}
@@ -19,8 +19,8 @@ export const courseRouter = (app: Express) => {
const { id } = req.params;
try {
const result = await CourseInstance.getOne(id);
res.status(200).send(result);
const { code, data } = await CourseInstance.getOne(id);
res.status(code).send(data);
} catch(e) {
next(e);
}
@@ -31,7 +31,7 @@ export const courseRouter = (app: Express) => {
try {
const result = await CourseInstance.post(data);
res.status(201).send(result);
res.status(result.code).send(result.data);
} catch(e) {
next(e);
}

View File

@@ -13,8 +13,8 @@ export const friendRouter = (app: Express) => {
const { targetid } = req.params;
try {
const result = await UserInstance.addFriendship(user.id, targetid);
res.status(200).send(result);
const { code, data } = await UserInstance.addFriendship(user.id, targetid);
res.status(code).send(data);
} catch(e) {
next(e);
}
@@ -27,11 +27,11 @@ export const friendRouter = (app: Express) => {
try {
if (pending) {
const result = await UserInstance.getPendingFriendRequests(user.id);
res.status(200).send(result);
const { code, data } = await UserInstance.getPendingFriendRequests(user.id);
res.status(code).send(data);
} else {
const result = await UserInstance.getFriends(user.id);
res.status(200).send(result);
const { code, data } = await UserInstance.getFriends(user.id);
res.status(code).send(data);
}
} catch(e) {
next(e);
@@ -44,8 +44,8 @@ export const friendRouter = (app: Express) => {
const { user }: any = req.user;
try {
const result = await UserInstance.getFriendshipByID(id, user.id);
res.status(200).send(result);
const { code, data } = await UserInstance.getFriendshipByID(id, user.id);
res.status(code).send(data);
} catch(e) {
next(e);
}
@@ -58,8 +58,8 @@ export const friendRouter = (app: Express) => {
const { user }: any = req.user;
try {
const result = await UserInstance.updateFriendship(id, user.id, data);
res.status(200).send(result);
const response = await UserInstance.updateFriendship(id, user.id, data);
res.status(response.code).send(response.data);
} catch(e) {
next(e);
}

View File

@@ -12,11 +12,11 @@ export const groceryListRoute = (app: Express) => {
try {
if (userid) {
const result = await groceryinstance.getByUserID(userid);
res.status(200).send(result);
const { code, data } = await groceryinstance.getByUserID(userid);
res.status(code).send(data);
} else {
const result = await groceryinstance.getAll();
res.status(200).send(result);
const { code, data } = await groceryinstance.getAll();
res.status(code).send(data);
}
} catch(e) {
next(e);
@@ -26,8 +26,8 @@ export const groceryListRoute = (app: Express) => {
router.get('/:id', async (req, res, next) => {
const { id } = req.params;
try {
const result = await groceryinstance.getOne(id);
res.status(200).send(result);
const { code, data } = await groceryinstance.getOne(id);
res.status(code).send(data);
} catch(e) {
next(e);
}
@@ -37,7 +37,7 @@ export const groceryListRoute = (app: Express) => {
const data = req.body;
try {
const result = await groceryinstance.post(data);
res.status(201).send(result);
res.status(result.code).send(result.data);
} catch(e) {
next(e);
}
@@ -49,7 +49,7 @@ export const groceryListRoute = (app: Express) => {
try {
const result = await groceryinstance.put(id, data);
res.status(200).send(result);
res.status(result.code).send(result.data);
} catch(e) {
next(e);
}

View File

@@ -17,11 +17,15 @@ export const routes = async (app: Express, passport: PassportStatic) => {
authRoute(app, passport);
userRoute(app);
friendRouter(app);
cuisineRouter(app);
courseRouter(app);
recipeRoute(app);
ingredientRoute(app);
// to do: refactor for ctlresponse
collectionRoute(app);
subscriptionRoute(app);
ingredientRoute(app);
groceryListRoute(app);
courseRouter(app);
// deprecate?
cuisineRouter(app);
}

View File

@@ -14,8 +14,8 @@ export const recipeRoute = (app: Express) => {
const { id } = req.params;
try {
const result = await recipectl.getOne(id);
res.status(200).send(result);
const { code, data } = await recipectl.getOne(id);
res.status(code).send(data);
} catch(e) {
next(e);
}