grocery list route
This commit is contained in:
@@ -1,21 +1,56 @@
|
||||
import createError from "http-errors";
|
||||
import { IGroceryList } from "../schemas";
|
||||
import { GroceryList } from "../models/groceryList";
|
||||
const GroceryInstance = new GroceryList();
|
||||
|
||||
export default class GroceryListCtl {
|
||||
async getAll() {
|
||||
|
||||
try {
|
||||
const result = await GroceryInstance.getAll();
|
||||
if (!result) throw createError('404', 'No results found');
|
||||
return result;
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async getOne(id: string) {
|
||||
try {
|
||||
const result = await GroceryInstance.getOne(id);
|
||||
if (!result) throw createError('404', 'No results found');
|
||||
return result;
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async getByUserID(userid: string) {
|
||||
try {
|
||||
const result = await GroceryInstance.getByUserID(userid);
|
||||
if (!result) throw createError('404', 'No results found');
|
||||
return result;
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async post(data: IGroceryList) {
|
||||
|
||||
try {
|
||||
const result = await GroceryInstance.post(data);
|
||||
if (!result) throw createError('400', 'Bad request');
|
||||
return result;
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async put(id: string, data: IGroceryList) {
|
||||
|
||||
try {
|
||||
const result = await GroceryInstance.put(id, data);
|
||||
if (!result) throw createError('400', 'Bad request');
|
||||
return result;
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,18 +3,73 @@ import pool from "../db";
|
||||
|
||||
export class GroceryList {
|
||||
async getAll() {
|
||||
|
||||
try {
|
||||
const statement = 'SELECT * FROM recipin.grocerylist';
|
||||
const result = await pool.query(statement);
|
||||
if (result.rows.length) return result.rows;
|
||||
return null;
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async getOne(id: string) {
|
||||
try {
|
||||
const statement = `SELECT * FROM recipin.grocerylist WHERE id = $1`
|
||||
const result = await pool.query(statement, [id]);
|
||||
if (result.rows.length) return result.rows[0];
|
||||
return null;
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async getByUserID(userid: string) {
|
||||
try {
|
||||
const statement = `SELECT * FROM recipin.grocerylist WHERE ownerid = $1`
|
||||
const result = await pool.query(statement, [userid]);
|
||||
if (result.rows.length) return result.rows;
|
||||
return null;
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async post(data: IGroceryList) {
|
||||
|
||||
try {
|
||||
// assumes that any new list will be active on creation
|
||||
const statement = `
|
||||
INSERT INTO recipin.grocerylist
|
||||
(name, active, ownerid)
|
||||
VALUES ($1, true, $2)
|
||||
RETURNING *;
|
||||
`
|
||||
const values = [data.name, data.ownerid];
|
||||
const result = await pool.query(statement, values);
|
||||
if (result.rows.length) return result.rows[0];
|
||||
return null;
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async put(id: string, data: IGroceryList) {
|
||||
try {
|
||||
const statement = `
|
||||
UPDATE recipin.grocerylist
|
||||
SET name = $1,
|
||||
active = $2,
|
||||
ownerid = $3
|
||||
WHERE id = $4
|
||||
RETURNING *
|
||||
`
|
||||
const values = [data.name, data.active, data.ownerid, id];
|
||||
|
||||
const result = await pool.query(statement, values);
|
||||
if (result.rows.length) return result.rows[0];
|
||||
return null;
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Express, Router } from "express";
|
||||
import { GroceryListCtl } from "../controllers";
|
||||
const groceryinstance = new GroceryListCtl();
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -7,18 +8,50 @@ export const groceryListRoute = (app: Express) => {
|
||||
app.use('/grocery-list', router);
|
||||
|
||||
router.get('/', async (req, res, next) => {
|
||||
const userid = req.query.userid as string;
|
||||
|
||||
try {
|
||||
if (userid) {
|
||||
const result = await groceryinstance.getByUserID(userid);
|
||||
res.status(200).send(result);
|
||||
} else {
|
||||
const result = await groceryinstance.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 groceryinstance.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 groceryinstance.post(data);
|
||||
res.status(201).send(result);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
})
|
||||
|
||||
router.put('/:id', async (req, res, next) => {
|
||||
const { id } = req.params;
|
||||
const data = req.body;
|
||||
|
||||
try {
|
||||
const result = await groceryinstance.put(id, data);
|
||||
res.status(200).send(result);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { userRoute } from "./users";
|
||||
import { recipeRoute } from "./recipe";
|
||||
import { collectionRoute } from "./collection";
|
||||
import { ingredientRoute } from "./ingredient";
|
||||
import { groceryListRoute } from "./groceryList";
|
||||
|
||||
export const routes = (app: Express, passport?: any) => {
|
||||
console.log('routes called');
|
||||
@@ -11,6 +12,7 @@ export const routes = (app: Express, passport?: any) => {
|
||||
recipeRoute(app);
|
||||
collectionRoute(app);
|
||||
ingredientRoute(app);
|
||||
groceryListRoute(app);
|
||||
|
||||
app.get('/hello', (req, res) => {
|
||||
res.send({ message: "hello from the server!!" });
|
||||
|
||||
@@ -33,7 +33,7 @@ export interface ICollection {
|
||||
|
||||
export interface IGroceryList {
|
||||
id: number
|
||||
listname: string
|
||||
name: string
|
||||
recipes?: IRecipe["id"][]
|
||||
active: boolean
|
||||
ownerid: IUser["id"]
|
||||
|
||||
Reference in New Issue
Block a user