diff --git a/server/controllers/RecipeCtl.ts b/server/controllers/RecipeCtl.ts index e69de29..f86143e 100644 --- a/server/controllers/RecipeCtl.ts +++ b/server/controllers/RecipeCtl.ts @@ -0,0 +1,36 @@ +import createError from "http-errors"; +import { IRecipe } from "../schemas"; +import { Recipe } from "../models/recipe"; +const RecipeInstance = new Recipe(); + +export default class RecipeCtl { + async getOne(id: string) { + try { + const result = await RecipeInstance.getOneByID(id); + if (!result) throw createError('404', "Recipe not found"); + return result; + } catch (error: any) { + throw new Error(error); + } + } + + async updateOne(id: string, data: IRecipe) { + try { + const result = await RecipeInstance.updateOneByID(id, data); + if (!result) throw createError('400', "Bad request"); + return result; + } catch (error: any) { + throw new Error(error); + } + } + + async post(data: IRecipe) { + try { + const result = await RecipeInstance.post(data); + if (!result) throw createError('400', "Bad request"); + return result; + } catch (error: any) { + throw new Error(error); + } + } +} \ No newline at end of file diff --git a/server/models/recipe.ts b/server/models/recipe.ts index 080491f..01e33bc 100644 --- a/server/models/recipe.ts +++ b/server/models/recipe.ts @@ -1,5 +1,53 @@ +import { IRecipe } from "../schemas"; +import pgPromise from "pg-promise"; +import pool from "../db"; + +const pgp = pgPromise({ capSQL: true }); + export class Recipe { async getOneByID(id: string) { + try { + const statement = `SELECT * FROM recipin.recipe WHERE id = $1`; + const values = [id]; + const result = await pool.query(statement, values); + if (result.rows) return result.rows[0]; + return null; + } catch (error: any) { + throw new Error(error); + } + } + async updateOneByID(id: string, data: IRecipe) { + const { name, description, preptime } = data; + try { + const statement = ` + UPDATE recipin.recipe + SET name = $1, + description = $2, + preptime = $3 + WHERE id = $4 + RETURNING *; + ` + + const result = await pool.query(statement, [name, description, preptime, id]); + if (result.rows) return result.rows[0]; + return null; + } catch (error: any) { + throw new Error(error); + } + } + + async post(data: IRecipe) { + const { name, description, preptime } = data; + + try { + const statement = `INSERT INTO recipin.recipe (name, description, preptime, authoruserid) VALUES ($1, $2, $3, (SELECT id FROM recipin.appusers WHERE id = 1)) RETURNING *;` + const values = [name, description, preptime]; + const result = await pool.query(statement, values); + if (result.rows) return result.rows[0]; + return null; + } catch (error: any) { + throw new Error(error); + } } } \ No newline at end of file diff --git a/server/routes/index.ts b/server/routes/index.ts index 88e4b8a..0486924 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -1,10 +1,12 @@ import { Express } from "express" import { userRoute } from "./users"; +import { recipeRoute } from "./recipe"; export const routes = (app: Express, passport?: any) => { console.log('routes called'); userRoute(app); + recipeRoute(app); app.get('/hello', (req, res) => { res.send({ message: "hello from the server!!" }); diff --git a/server/routes/recipe.ts b/server/routes/recipe.ts index e69de29..24ad5ca 100644 --- a/server/routes/recipe.ts +++ b/server/routes/recipe.ts @@ -0,0 +1,45 @@ +import { Express, Router } from "express" +import RecipeCtl from "../controllers/RecipeCtl"; +const recipectl = new RecipeCtl(); + +const router = Router(); + +export const recipeRoute = (app: Express) => { + app.use('/recipe', router); + + router.get('/:id', async (req, res, next) => { + const { id } = req.params; + console.log('/recipe/' + id + ' called'); + + try { + const result = await recipectl.getOne(id); + res.status(200).send(result); + } catch(e) { + next(e); + } + }) + + router.put('/:id', async (req, res, next) => { + const data = req.body; + const { id } = req.params; + + try { + const result = await recipectl.updateOne(id, data); + res.status(200).send(result); + } catch(e) { + next(e); + } + }) + + router.post('/', async (req, res, next) => { + const data = req.body; + console.log(data); + + try { + const result = await recipectl.post(data); + res.status(201).send(result); + } catch(e) { + next(e); + } + }) +} \ No newline at end of file diff --git a/server/routes/users.ts b/server/routes/users.ts index 1275cb7..0255d66 100644 --- a/server/routes/users.ts +++ b/server/routes/users.ts @@ -1,4 +1,3 @@ -import pool from '../db'; import { Express, Router } from 'express'; import UserCtl from '../controllers/UserCtl'; const router = Router();