implements a category route
This commit is contained in:
50
models/CategoryModel.js
Normal file
50
models/CategoryModel.js
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
const db = require('../db/Pool');
|
||||||
|
const pgp = require('pg-promise')({ capSQL: true });
|
||||||
|
|
||||||
|
module.exports = class CategoryModel {
|
||||||
|
async getAll() {
|
||||||
|
try {
|
||||||
|
const statement = "SELECT * FROM category";
|
||||||
|
const result = await db.query(statement);
|
||||||
|
if (result.rows.length) return result.rows;
|
||||||
|
return null;
|
||||||
|
} catch(e) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getById(id) {
|
||||||
|
try {
|
||||||
|
const statement = "SELECT * FROM category WHERE id = $1";
|
||||||
|
const values = [id];
|
||||||
|
const result = await db.query(statement, values);
|
||||||
|
if (result.rows.length) return result.rows;
|
||||||
|
return null;
|
||||||
|
} catch(e) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getByName(name) {
|
||||||
|
try {
|
||||||
|
const statement = "SELECT * FROM category WHERE name = $1";
|
||||||
|
const values = [name];
|
||||||
|
const result = await db.query(statement, values);
|
||||||
|
if (result.rows.length) return result.rows;
|
||||||
|
return null;
|
||||||
|
} catch(e) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async insertOne(data) {
|
||||||
|
try {
|
||||||
|
const statement = pgp.helpers.insert(data, null, 'category') + "RETURNING *";
|
||||||
|
const result = await db.query(statement);
|
||||||
|
if (result.rows.length) return result.rows;
|
||||||
|
return null;
|
||||||
|
} catch(e) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
const authRouter = require('./auth');
|
const authRouter = require('./auth');
|
||||||
const userRouter = require('./user');
|
const userRouter = require('./user');
|
||||||
const productRouter = require('./product');
|
const productRouter = require('./product');
|
||||||
|
const categoryRouter = require('./category');
|
||||||
const regionsRouter = require('./regions');
|
const regionsRouter = require('./regions');
|
||||||
const orderRouter = require('./orders');
|
const orderRouter = require('./orders');
|
||||||
const cartRouter = require('./cart');
|
const cartRouter = require('./cart');
|
||||||
@@ -9,6 +10,7 @@ module.exports = (app, passport) => {
|
|||||||
authRouter(app, passport);
|
authRouter(app, passport);
|
||||||
userRouter(app);
|
userRouter(app);
|
||||||
productRouter(app);
|
productRouter(app);
|
||||||
|
categoryRouter(app);
|
||||||
regionsRouter(app);
|
regionsRouter(app);
|
||||||
orderRouter(app);
|
orderRouter(app);
|
||||||
cartRouter(app);
|
cartRouter(app);
|
||||||
|
|||||||
49
routes/category.js
Normal file
49
routes/category.js
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
const router = require('express').Router();
|
||||||
|
const CategoryService = require('../services/CategoryService');
|
||||||
|
const CategoryInstance = new CategoryService();
|
||||||
|
|
||||||
|
module.exports = (app) => {
|
||||||
|
app.use('/api/category', router);
|
||||||
|
|
||||||
|
router.get('/', async (req, res, next) => {
|
||||||
|
const { name } = req.query;
|
||||||
|
|
||||||
|
if (name) {
|
||||||
|
try {
|
||||||
|
const response = await CategoryInstance.getByName(name);
|
||||||
|
res.status(200).send(response);
|
||||||
|
} catch(e) {
|
||||||
|
next(e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
const response = await CategoryInstance.getAll();
|
||||||
|
res.status(200).send(response);
|
||||||
|
} catch(e) {
|
||||||
|
next(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
router.get('/:productid', async (req, res, next) => {
|
||||||
|
const { productid } = req.params;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await CategoryInstance.getById(productid);
|
||||||
|
res.status(200).send(response);
|
||||||
|
} catch(e) {
|
||||||
|
next(e);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
router.post('/', async (req, res, next) => {
|
||||||
|
const data = req.body;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await CategoryInstance.create(data);
|
||||||
|
res.status(201).send(response);
|
||||||
|
} catch(e) {
|
||||||
|
next(e);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
29
services/CategoryService.js
Normal file
29
services/CategoryService.js
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
const createError = require('http-errors');
|
||||||
|
const CategoryModel = require('../models/CategoryModel');
|
||||||
|
const CategoryInstance = new CategoryModel();
|
||||||
|
|
||||||
|
module.exports = class CategoryService {
|
||||||
|
async getAll() {
|
||||||
|
const result = await CategoryInstance.getAll();
|
||||||
|
if (!result) throw createError(404, "Resource not found.");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getById(id) {
|
||||||
|
const result = await CategoryInstance.getById(id);
|
||||||
|
if (!result) throw createError(404, "Resource not found.");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getByName(name) {
|
||||||
|
const result = await CategoryInstance.getByName(name);
|
||||||
|
if (!result) throw createError(404, "Resource not found.");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async create(data) {
|
||||||
|
const result = await CategoryInstance.insertOne(data);
|
||||||
|
if (!result) throw createError(404, "Resource not found.");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user