built cuisine route
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
import createError from "http-errors";
|
||||||
|
import { ICuisine } from "../schemas";
|
||||||
|
import Cuisine from "../models/cuisine";
|
||||||
|
const CuisineInstance = new Cuisine();
|
||||||
|
|
||||||
|
export default class CuisineCtl {
|
||||||
|
async getAll() {
|
||||||
|
try {
|
||||||
|
const result = await CuisineInstance.getAll();
|
||||||
|
if (!result) throw createError('404', 'No cuisines found');
|
||||||
|
return result;
|
||||||
|
} catch (e: any) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getOne(id: string) {
|
||||||
|
try {
|
||||||
|
const result = await CuisineInstance.getOne(id);
|
||||||
|
if (!result) throw createError('404', 'No cuisine found with id ' + id);
|
||||||
|
return result;
|
||||||
|
} catch (e: any) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async post(data: ICuisine) {
|
||||||
|
try {
|
||||||
|
const result = await CuisineInstance.post(data);
|
||||||
|
if (!result) throw createError('400', 'Bad request');
|
||||||
|
return result;
|
||||||
|
} catch (e: any) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
import CollectionCtl from "./CollectionCtl";
|
import CollectionCtl from "./CollectionCtl";
|
||||||
|
import CuisineCtl from "./CuisineCtl";
|
||||||
import GroceryListCtl from "./GroceryListCtl";
|
import GroceryListCtl from "./GroceryListCtl";
|
||||||
import IngredientCtl from "./IngredientCtl";
|
import IngredientCtl from "./IngredientCtl";
|
||||||
import RecipeCtl from "./RecipeCtl";
|
import RecipeCtl from "./RecipeCtl";
|
||||||
import UserCtl from "./UserCtl";
|
import UserCtl from "./UserCtl";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
CollectionCtl, GroceryListCtl, IngredientCtl, RecipeCtl, UserCtl
|
CollectionCtl, CuisineCtl, GroceryListCtl, IngredientCtl, RecipeCtl, UserCtl
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import pool from "../db";
|
||||||
|
import now from "../util/now";
|
||||||
|
import { ICuisine } from "../schemas";
|
||||||
|
|
||||||
|
export default class Cuisine {
|
||||||
|
async getOne(id: string) {
|
||||||
|
try {
|
||||||
|
const statement = `SELECT * FROM recipin.cuisine 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 getAll() {
|
||||||
|
try {
|
||||||
|
const statement = `SELECT * FROM recipin.cuisine`;
|
||||||
|
const result = await pool.query(statement);
|
||||||
|
if (result.rows.length) return result.rows;
|
||||||
|
return null;
|
||||||
|
} catch (e: any) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async post(data: ICuisine) {
|
||||||
|
try {
|
||||||
|
const { name } = data;
|
||||||
|
const statement = `
|
||||||
|
INSERT INTO recipin.cuisine
|
||||||
|
(name, datecreated, datemodified, active)
|
||||||
|
VALUES ($1, $2, $3, $4) RETURNING *`;
|
||||||
|
const values = [name, now, now, true];
|
||||||
|
const result = await pool.query(statement, values);
|
||||||
|
if (result.rows.length) return result.rows[0];
|
||||||
|
return null;
|
||||||
|
} catch (e: any) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import { Express, Router } from 'express';
|
||||||
|
import { CuisineCtl } from '../controllers';
|
||||||
|
const CuisineInstance = new CuisineCtl();
|
||||||
|
|
||||||
|
const router = Router();
|
||||||
|
|
||||||
|
export const cuisineRouter = (app: Express) => {
|
||||||
|
app.use('/cuisine', router);
|
||||||
|
|
||||||
|
router.get('/', async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const result = await CuisineInstance.getAll();
|
||||||
|
res.status(200).send(result);
|
||||||
|
} catch(e) {
|
||||||
|
next(e);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
router.get('/:id', async (req, res, next) => {
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await CuisineInstance.getOne(id);
|
||||||
|
res.status(200).send(result);
|
||||||
|
} catch(e) {
|
||||||
|
next(e);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
router.post('/', async (req, res, next) => {
|
||||||
|
const data = req.body;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await CuisineInstance.post(data);
|
||||||
|
res.status(201).send(result);
|
||||||
|
} catch(e) {
|
||||||
|
next(e);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ import { groceryListRoute } from "./groceryList";
|
|||||||
import { authRoute } from "./auth";
|
import { authRoute } from "./auth";
|
||||||
import { subscriptionRoute } from "./subscription";
|
import { subscriptionRoute } from "./subscription";
|
||||||
import { friendRouter } from "./friend";
|
import { friendRouter } from "./friend";
|
||||||
|
import { cuisineRouter } from "./cuisine";
|
||||||
|
|
||||||
export const routes = async (app: Express, passport: PassportStatic) => {
|
export const routes = async (app: Express, passport: PassportStatic) => {
|
||||||
console.log('routes called');
|
console.log('routes called');
|
||||||
@@ -15,6 +16,7 @@ export const routes = async (app: Express, passport: PassportStatic) => {
|
|||||||
authRoute(app, passport);
|
authRoute(app, passport);
|
||||||
userRoute(app);
|
userRoute(app);
|
||||||
friendRouter(app);
|
friendRouter(app);
|
||||||
|
cuisineRouter(app);
|
||||||
recipeRoute(app);
|
recipeRoute(app);
|
||||||
collectionRoute(app);
|
collectionRoute(app);
|
||||||
subscriptionRoute(app);
|
subscriptionRoute(app);
|
||||||
|
|||||||
Reference in New Issue
Block a user