support on collection route
This commit is contained in:
@@ -1,3 +1,24 @@
|
|||||||
|
import createError from "http-errors";
|
||||||
|
import { ICollection } from "../schemas";
|
||||||
|
import { Collection } from "../models/collection";
|
||||||
|
const CollectionInstance = new Collection();
|
||||||
|
|
||||||
export default class CollectionCtl {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,8 @@ export class Collection {
|
|||||||
const statement = `SELECT * FROM recipin.collection WHERE id = $1`;
|
const statement = `SELECT * FROM recipin.collection WHERE id = $1`;
|
||||||
const values = [id];
|
const values = [id];
|
||||||
const result = await pool.query(statement, values);
|
const result = await pool.query(statement, values);
|
||||||
return result;
|
if (result.rows.length) return result.rows[0];
|
||||||
|
return null;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
}
|
}
|
||||||
@@ -18,18 +19,26 @@ export class Collection {
|
|||||||
try {
|
try {
|
||||||
const statement = `SELECT * FROM recipin.collection`;
|
const statement = `SELECT * FROM recipin.collection`;
|
||||||
const result = await pool.query(statement);
|
const result = await pool.query(statement);
|
||||||
return result;
|
if (result.rows.length) return result.rows;
|
||||||
|
return null;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async post(data: ICollection) {
|
async post(data: ICollection) {
|
||||||
|
const { name, active, ismaincollection, ownerid } = data;
|
||||||
try {
|
try {
|
||||||
const statement = `
|
const statement = `
|
||||||
INSERT INTO recipin.collection
|
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) {
|
} catch (e: any) {
|
||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,36 @@
|
|||||||
import { Express, Router } from "express";
|
import { Express, Router } from "express";
|
||||||
|
import CollectionCtl from "../controllers/CollectionCtl";
|
||||||
|
const CollectionInstance = new CollectionCtl();
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
const collectionRoute = (app: Express) => {
|
export const collectionRoute = (app: Express) => {
|
||||||
app.use('/collection', router);
|
app.use('/collection', router);
|
||||||
|
|
||||||
router.get('/:id', async (req, res, next) => {
|
router.get('/:id', async (req, res, next) => {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
try {
|
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) {
|
} catch(e) {
|
||||||
next(e);
|
next(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
import { Express } from "express"
|
import { Express } from "express"
|
||||||
import { userRoute } from "./users";
|
import { userRoute } from "./users";
|
||||||
import { recipeRoute } from "./recipe";
|
import { recipeRoute } from "./recipe";
|
||||||
|
import { collectionRoute } from "./collection";
|
||||||
|
|
||||||
export const routes = (app: Express, passport?: any) => {
|
export const routes = (app: Express, passport?: any) => {
|
||||||
console.log('routes called');
|
console.log('routes called');
|
||||||
|
|
||||||
userRoute(app);
|
userRoute(app);
|
||||||
recipeRoute(app);
|
recipeRoute(app);
|
||||||
|
collectionRoute(app);
|
||||||
|
|
||||||
app.get('/hello', (req, res) => {
|
app.get('/hello', (req, res) => {
|
||||||
res.send({ message: "hello from the server!!" });
|
res.send({ message: "hello from the server!!" });
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ export const recipeRoute = (app: Express) => {
|
|||||||
|
|
||||||
router.get('/:id', async (req, res, next) => {
|
router.get('/:id', async (req, res, next) => {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
console.log('/recipe/' + id + ' called');
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await recipectl.getOne(id);
|
const result = await recipectl.getOne(id);
|
||||||
|
|||||||
Reference in New Issue
Block a user