added course route
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
import createError from 'http-errors';
|
||||||
|
import { ICourse } from '../schemas';
|
||||||
|
import Course from '../models/course';
|
||||||
|
const CourseInstance = new Course();
|
||||||
|
|
||||||
|
export default class CourseCtl {
|
||||||
|
async getAll() {
|
||||||
|
try {
|
||||||
|
const result = await CourseInstance.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 CourseInstance.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: ICourse) {
|
||||||
|
try {
|
||||||
|
const result = await CourseInstance.post(data);
|
||||||
|
if (!result) throw createError('400', 'Bad request');
|
||||||
|
return result;
|
||||||
|
} catch (e: any) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,8 @@ 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";
|
||||||
|
import CourseCtl from "./CourseCtl";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
CollectionCtl, CuisineCtl, GroceryListCtl, IngredientCtl, RecipeCtl, UserCtl
|
CollectionCtl, CuisineCtl, CourseCtl, GroceryListCtl, IngredientCtl, RecipeCtl, UserCtl
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import now from "../util/now";
|
||||||
|
import pool from "../db";
|
||||||
|
import { ICourse } from "../schemas";
|
||||||
|
|
||||||
|
export default class Course {
|
||||||
|
async getOne(id: string) {
|
||||||
|
try {
|
||||||
|
const statement = `SELECT * FROM recipin.course 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.course`;
|
||||||
|
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: ICourse) {
|
||||||
|
try {
|
||||||
|
const { name } = data;
|
||||||
|
const statement = `
|
||||||
|
INSERT INTO recipin.course
|
||||||
|
(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,39 @@
|
|||||||
|
import { Express, Router } from 'express';
|
||||||
|
import { CourseCtl } from '../controllers';
|
||||||
|
const CourseInstance = new CourseCtl();
|
||||||
|
const router = Router();
|
||||||
|
|
||||||
|
export const courseRouter = (app: Express) => {
|
||||||
|
app.use('/course', router);
|
||||||
|
|
||||||
|
router.get('/', async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const result = await CourseInstance.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 CourseInstance.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 CourseInstance.post(data);
|
||||||
|
res.status(201).send(result);
|
||||||
|
} catch(e) {
|
||||||
|
next(e);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import { authRoute } from "./auth";
|
|||||||
import { subscriptionRoute } from "./subscription";
|
import { subscriptionRoute } from "./subscription";
|
||||||
import { friendRouter } from "./friend";
|
import { friendRouter } from "./friend";
|
||||||
import { cuisineRouter } from "./cuisine";
|
import { cuisineRouter } from "./cuisine";
|
||||||
|
import { courseRouter } from "./course";
|
||||||
|
|
||||||
export const routes = async (app: Express, passport: PassportStatic) => {
|
export const routes = async (app: Express, passport: PassportStatic) => {
|
||||||
console.log('routes called');
|
console.log('routes called');
|
||||||
@@ -17,6 +18,7 @@ export const routes = async (app: Express, passport: PassportStatic) => {
|
|||||||
userRoute(app);
|
userRoute(app);
|
||||||
friendRouter(app);
|
friendRouter(app);
|
||||||
cuisineRouter(app);
|
cuisineRouter(app);
|
||||||
|
courseRouter(app);
|
||||||
recipeRoute(app);
|
recipeRoute(app);
|
||||||
collectionRoute(app);
|
collectionRoute(app);
|
||||||
subscriptionRoute(app);
|
subscriptionRoute(app);
|
||||||
|
|||||||
Reference in New Issue
Block a user