From d020fa8af48307983360bc66100df985fc5b16de Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Tue, 27 Sep 2022 14:04:56 -0500 Subject: [PATCH] implemented region route in api --- models/RegionsModel.js | 39 ++++++++++++++++++++++++++++++++++++++ routes/API.js | 2 ++ routes/regions.js | 38 +++++++++++++++++++++++++++++++++++++ services/RegionsService.js | 35 ++++++++++++++++++++++++++++++++++ swagger.yml | 30 +++++++++++++++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 models/RegionsModel.js create mode 100644 routes/regions.js create mode 100644 services/RegionsService.js diff --git a/models/RegionsModel.js b/models/RegionsModel.js new file mode 100644 index 0000000..e2b0c9e --- /dev/null +++ b/models/RegionsModel.js @@ -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); + } + } +} \ No newline at end of file diff --git a/routes/API.js b/routes/API.js index f4cd649..8a788cf 100644 --- a/routes/API.js +++ b/routes/API.js @@ -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); } \ No newline at end of file diff --git a/routes/regions.js b/routes/regions.js new file mode 100644 index 0000000..e0e555a --- /dev/null +++ b/routes/regions.js @@ -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); + } + }) +} \ No newline at end of file diff --git a/services/RegionsService.js b/services/RegionsService.js new file mode 100644 index 0000000..4778c81 --- /dev/null +++ b/services/RegionsService.js @@ -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); + } + } +} \ No newline at end of file diff --git a/swagger.yml b/swagger.yml index 8bff88d..46d8001 100644 --- a/swagger.yml +++ b/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"