support on collection route

This commit is contained in:
Mikayla Dobson
2022-11-19 12:02:28 -06:00
parent c6156e237e
commit bf16841b95
5 changed files with 59 additions and 6 deletions

View File

@@ -1,3 +1,24 @@
export default class CollectionCtl {
import createError from "http-errors";
import { ICollection } from "../schemas";
import { Collection } from "../models/collection";
const CollectionInstance = new Collection();
export default class CollectionCtl {
async getOne(id: string) {
const result = await CollectionInstance.getOne(id);
if (!result) throw createError('404', 'Collection not found');
return result;
}
async getAll() {
const result = await CollectionInstance.getAll();
if (!result) throw createError('404', 'No collections found');
return result;
}
async post(data: ICollection) {
const result = await CollectionInstance.post(data);
if (!result) throw createError('400', 'Bad request');
return result;
}
}

View File

@@ -7,7 +7,8 @@ export class Collection {
const statement = `SELECT * FROM recipin.collection WHERE id = $1`;
const values = [id];
const result = await pool.query(statement, values);
return result;
if (result.rows.length) return result.rows[0];
return null;
} catch (e: any) {
throw new Error(e);
}
@@ -18,18 +19,26 @@ export class Collection {
try {
const statement = `SELECT * FROM recipin.collection`;
const result = await pool.query(statement);
return result;
if (result.rows.length) return result.rows;
return null;
} catch (e: any) {
throw new Error(e);
}
}
async post(data: ICollection) {
const { name, active, ismaincollection, ownerid } = data;
try {
const statement = `
INSERT INTO recipin.collection
()
(name, active, ismaincollection, ownerid)
VALUES ($1, $2, $3, $4)
RETURNING *;
`
const values = [name, active, ismaincollection, ownerid];
const result = await pool.query(statement, values);
if (result.rows.length) return result.rows;
return null;
} catch (e: any) {
throw new Error(e);
}

View File

@@ -1,14 +1,36 @@
import { Express, Router } from "express";
import CollectionCtl from "../controllers/CollectionCtl";
const CollectionInstance = new CollectionCtl();
const router = Router();
const collectionRoute = (app: Express) => {
export const collectionRoute = (app: Express) => {
app.use('/collection', router);
router.get('/:id', async (req, res, next) => {
const { id } = req.params;
try {
const result = await CollectionInstance.getOne(id);
res.status(200).send(result);
} catch(e) {
next(e);
}
})
router.get('/', async (req, res, next) => {
try {
const result = await CollectionInstance.getAll();
res.status(200).send(result);
} catch(e) {
next(e);
}
})
router.post('/', async (req, res, next) => {
const data = req.body;
try {
const result = await CollectionInstance.post(data);
res.status(201).send(result);
} catch(e) {
next(e);
}

View File

@@ -1,12 +1,14 @@
import { Express } from "express"
import { userRoute } from "./users";
import { recipeRoute } from "./recipe";
import { collectionRoute } from "./collection";
export const routes = (app: Express, passport?: any) => {
console.log('routes called');
userRoute(app);
recipeRoute(app);
collectionRoute(app);
app.get('/hello', (req, res) => {
res.send({ message: "hello from the server!!" });

View File

@@ -9,7 +9,6 @@ export const recipeRoute = (app: Express) => {
router.get('/:id', async (req, res, next) => {
const { id } = req.params;
console.log('/recipe/' + id + ' called');
try {
const result = await recipectl.getOne(id);