add recipe workflow, viewing for collections
This commit is contained in:
@@ -15,6 +15,14 @@ export default class CollectionCtl {
|
||||
return new ControllerResponse(code, data);
|
||||
}
|
||||
|
||||
async getRecipesFromOne(id: number | string) {
|
||||
const result = await CollectionInstance.getRecipesFromOne(id);
|
||||
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();
|
||||
const ok = result !== null;
|
||||
|
||||
@@ -18,6 +18,23 @@ export class Collection {
|
||||
}
|
||||
}
|
||||
|
||||
async getRecipesFromOne(id: number | string) {
|
||||
try {
|
||||
const statement = `
|
||||
SELECT * FROM recipin.recipe
|
||||
INNER JOIN recipin.cmp_recipecollection
|
||||
ON recipe.id = cmp_recipecollection.recipeid
|
||||
WHERE cmp_recipecollection.collectionid = $1;
|
||||
`;
|
||||
const values = [id];
|
||||
const result = await pool.query(statement, values);
|
||||
if (result.rows.length) return result.rows;
|
||||
return null;
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async getAllAuthored(id: number | string) {
|
||||
console.log(id, typeof id);
|
||||
try {
|
||||
|
||||
@@ -10,11 +10,18 @@ const router = Router();
|
||||
export const collectionRoute = (app: Express) => {
|
||||
app.use('/app/collection', router);
|
||||
|
||||
router.get('/:id', async (req, res, next) => {
|
||||
router.get('/:id', restrictAccess, async (req, res, next) => {
|
||||
const { id } = req.params;
|
||||
const { getRecipes } = req.query;
|
||||
|
||||
try {
|
||||
const { code, data } = await CollectionInstance.getOne(id);
|
||||
res.status(code).send(data);
|
||||
if (getRecipes || getRecipes == "true") {
|
||||
const { code, data } = await CollectionInstance.getRecipesFromOne(id);
|
||||
res.status(code).send(data);
|
||||
} else {
|
||||
const { code, data } = await CollectionInstance.getOne(id);
|
||||
res.status(code).send(data);
|
||||
}
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
@@ -45,7 +52,7 @@ export const collectionRoute = (app: Express) => {
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/', async (req, res, next) => {
|
||||
router.post('/', restrictAccess, async (req, res, next) => {
|
||||
const data = req.body;
|
||||
|
||||
try {
|
||||
|
||||
@@ -22,12 +22,12 @@ export const recipeRoute = (app: Express) => {
|
||||
})
|
||||
|
||||
router.get('/', restrictAccess, async (req, res, next) => {
|
||||
const user = req.session.user as IUser;
|
||||
const { filterby } = req.query;
|
||||
const user = req.user as IUser;
|
||||
const { filter } = req.query;
|
||||
|
||||
try {
|
||||
let result: CtlResponse<IRecipe[] | string>;
|
||||
switch (filterby) {
|
||||
switch (filter) {
|
||||
case "myrecipes":
|
||||
result = await recipectl.getAllAuthored(user.id as number);
|
||||
break;
|
||||
@@ -55,7 +55,7 @@ export const recipeRoute = (app: Express) => {
|
||||
})
|
||||
|
||||
router.post('/', restrictAccess, async (req, res, next) => {
|
||||
const user = req.session.user as IUser;
|
||||
const user = req.user as IUser;
|
||||
const data = req.body;
|
||||
|
||||
try {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Express, Router } from "express"
|
||||
import { restrictAccess } from "../auth/middlewares";
|
||||
import { CollectionCtl } from "../controllers";
|
||||
import { IUser } from "../schemas";
|
||||
const CollectionInstance = new CollectionCtl();
|
||||
const router = Router();
|
||||
|
||||
@@ -8,12 +9,11 @@ export const subscriptionRoute = (app: Express) => {
|
||||
app.use('/app/subscription', router);
|
||||
|
||||
router.get('/', async (req, res, next) => {
|
||||
// @ts-ignore
|
||||
const { user } = req.session.user;
|
||||
const user = req.user as IUser;
|
||||
if (!user) return;
|
||||
|
||||
try {
|
||||
const result = await CollectionInstance.getSubscriptions(user.id as string);
|
||||
const result = await CollectionInstance.getSubscriptions(user.id!);
|
||||
res.status(200).send(result);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
@@ -21,12 +21,11 @@ export const subscriptionRoute = (app: Express) => {
|
||||
})
|
||||
|
||||
router.post('/', restrictAccess, async (req, res, next) => {
|
||||
// @ts-ignore
|
||||
const { user } = req.session.user;
|
||||
const user = req.user as IUser;
|
||||
const { collection } = req.query;
|
||||
|
||||
try {
|
||||
const result = await CollectionInstance.postSubscription(collection as string, user.id as string);
|
||||
const result = await CollectionInstance.postSubscription(collection as string, user.id!);
|
||||
res.status(201).send(result);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
|
||||
Reference in New Issue
Block a user