in progress: handle add ingredients to recipe
This commit is contained in:
@@ -16,6 +16,17 @@ export default class IngredientCtl {
|
||||
}
|
||||
}
|
||||
|
||||
async getAllForRecipe(recipeid: string) {
|
||||
try {
|
||||
const result = await IngredientInstance.getAllForRecipe(recipeid);
|
||||
const ok = result !== null;
|
||||
const code = ok ? StatusCode.OK : StatusCode.NotFound;
|
||||
return new ControllerResponse(code, (result || "No ingredient found with this recipe ID"));
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async getOne(id: string) {
|
||||
try {
|
||||
const result = await IngredientInstance.getOne(id);
|
||||
|
||||
@@ -25,6 +25,17 @@ export class Ingredient {
|
||||
}
|
||||
}
|
||||
|
||||
async getAllForRecipe(recipeid: string) {
|
||||
try {
|
||||
const statement = `SELECT * FROM recipin.cmp_recipeingredient WHERE recipeid = $1`;
|
||||
const result = await pool.query(statement, [recipeid]);
|
||||
if (result.rows.length) return result.rows[0];
|
||||
return null;
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async post(data: IIngredient) {
|
||||
try {
|
||||
const statement = `
|
||||
|
||||
@@ -10,9 +10,16 @@ export const ingredientRoute = (app: Express) => {
|
||||
app.use('/app/ingredient', router);
|
||||
|
||||
router.get('/', async (req, res, next) => {
|
||||
const { recipeID } = req.query;
|
||||
|
||||
try {
|
||||
const result: CtlResponse<IIngredient[] | string> = await IngredientInstance.getAll();
|
||||
res.status(result.code).send(result.data);
|
||||
if (recipeID) {
|
||||
const result = await IngredientInstance.getAllForRecipe(recipeID as string);
|
||||
res.status(result.code).send(result.data);
|
||||
} else {
|
||||
const result: CtlResponse<IIngredient[] | string> = await IngredientInstance.getAll();
|
||||
res.status(result.code).send(result.data);
|
||||
}
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
|
||||
@@ -59,6 +59,8 @@ export const recipeRoute = (app: Express) => {
|
||||
const data = req.body;
|
||||
const { addIngredients, recipeID } = req.query;
|
||||
|
||||
console.log(data);
|
||||
|
||||
try {
|
||||
if (addIngredients) {
|
||||
const result = await recipectl.addIngredientToRecipe(data, recipeID as string);
|
||||
|
||||
Reference in New Issue
Block a user