in progress: associate ingredients to recipe on save

This commit is contained in:
Mikayla Dobson
2023-02-19 12:04:46 -06:00
parent 47360518ce
commit 63d0049450
8 changed files with 116 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
import { IRecipe } from "../schemas";
import { IIngredient, IRecipe, RecipeIngredient } from "../schemas";
import fs from 'fs';
import pool from "../db";
import { CollectionCtl } from "../controllers";
@@ -114,4 +114,24 @@ export class Recipe {
throw new Error(error);
}
}
async addIngredientToRecipe(ingredient: RecipeIngredient, recipeid: string | number) {
const { quantity, unit, id } = ingredient;
try {
const statement = `
INSERT INTO recipin.cmp_recipeingredient
(quantity, unit, ingredientid, recipeid)
VALUES ($1, $2, $3, $4) RETURNING *
`
const result = await pool.query(statement, [quantity, unit, id, recipeid]);
if (result.rows) return result.rows[0];
return [];
} catch (e: any) {
throw new Error(e);
}
}
}