implemented region route in api
This commit is contained in:
39
models/RegionsModel.js
Normal file
39
models/RegionsModel.js
Normal file
@@ -0,0 +1,39 @@
|
||||
const db = require('../db/Pool');
|
||||
const pgp = require('pg-promise')({ capSQL: true });
|
||||
|
||||
module.exports = class RegionsModel {
|
||||
async selectAll() {
|
||||
try {
|
||||
const statement = "SELECT * FROM region";
|
||||
const result = await db.query(statement);
|
||||
if (result.rows.length) return result.rows;
|
||||
return null;
|
||||
} catch(e) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async getOne(regionid) {
|
||||
try {
|
||||
const statement = "SELECT * FROM region WHERE id = $1";
|
||||
const values = [regionid]
|
||||
const result = await db.query(statement, values);
|
||||
if (result.rows.length) return result.rows;
|
||||
return null;
|
||||
} catch(e) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
// protected
|
||||
async create(data) {
|
||||
try {
|
||||
const statement = pgp.helpers.insert(data, null, 'region') + '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 userRouter = require('./user');
|
||||
const productRouter = require('./product');
|
||||
const regionsRouter = require('./regions');
|
||||
const orderRouter = require('./orders');
|
||||
const cartRouter = require('./cart');
|
||||
|
||||
@@ -8,6 +9,7 @@ module.exports = (app, passport) => {
|
||||
authRouter(app, passport);
|
||||
userRouter(app);
|
||||
productRouter(app);
|
||||
regionsRouter(app);
|
||||
orderRouter(app);
|
||||
cartRouter(app);
|
||||
}
|
||||
38
routes/regions.js
Normal file
38
routes/regions.js
Normal file
@@ -0,0 +1,38 @@
|
||||
const router = require('express').Router();
|
||||
const RegionsService = require('../services/RegionsService');
|
||||
const RegionsInstance = new RegionsService();
|
||||
|
||||
module.exports = (app) => {
|
||||
app.use('/api/regions', router);
|
||||
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
const response = await RegionsInstance.getAll();
|
||||
res.status(200).send(response);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/:regionid', async (req, res, next) => {
|
||||
const { regionid } = req.params;
|
||||
|
||||
try {
|
||||
const response = await RegionsInstance.getOne(regionid);
|
||||
res.status(200).send(response);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/', async (req, res, next) => {
|
||||
const data = req.body;
|
||||
|
||||
try {
|
||||
const response = await RegionsInstance.create(data);
|
||||
res.status(200).send(response);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
})
|
||||
}
|
||||
35
services/RegionsService.js
Normal file
35
services/RegionsService.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const createError = require('http-errors');
|
||||
const RegionsModel = require('../models/RegionsModel');
|
||||
const RegionInstance = new RegionsModel();
|
||||
|
||||
module.exports = class RegionsService {
|
||||
async getAll() {
|
||||
try {
|
||||
const result = await RegionInstance.selectAll();
|
||||
if (!result) throw createError(404, "No region entries found.");
|
||||
return result;
|
||||
} catch(e) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async getOne(id) {
|
||||
try {
|
||||
const result = await RegionInstance.getOne(id);
|
||||
if (!result) throw createError(404, "No region entries found.");
|
||||
return result;
|
||||
} catch(e) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async create(data) {
|
||||
try {
|
||||
const result = await RegionInstance(data);
|
||||
if (!result) throw createError(401, "Unauthorized action");
|
||||
return result;
|
||||
} catch(e) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
swagger.yml
30
swagger.yml
@@ -376,6 +376,36 @@ paths:
|
||||
description: "Access forbidden"
|
||||
schema:
|
||||
type: object
|
||||
|
||||
/regions:
|
||||
get:
|
||||
summary: "Get all product regions"
|
||||
tags:
|
||||
- region
|
||||
responses:
|
||||
200:
|
||||
description: "Got region listing successfully"
|
||||
schema:
|
||||
type: object
|
||||
404:
|
||||
description: "Requested resource was not found"
|
||||
schema:
|
||||
type: object
|
||||
/regions/{regionid}:
|
||||
get:
|
||||
summary: "Get one region by its ID"
|
||||
tags:
|
||||
- region
|
||||
responses:
|
||||
200:
|
||||
description: "Got region listing successfully"
|
||||
schema:
|
||||
type: object
|
||||
404:
|
||||
description: "Requested resource was not found"
|
||||
schema:
|
||||
type: object
|
||||
|
||||
/user:
|
||||
get:
|
||||
summary: "Get all users"
|
||||
|
||||
Reference in New Issue
Block a user