From 557c13b65eb3732b22b73b483e633a2aa1247d56 Mon Sep 17 00:00:00 2001 From: firmanjs Date: Tue, 4 Aug 2020 11:35:17 +0700 Subject: [PATCH 1/7] refactoring item --- controllers/item/ItemController.js | 10 +++++----- routes/ItemRoute.js | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/controllers/item/ItemController.js b/controllers/item/ItemController.js index a6da838..46f1d1f 100644 --- a/controllers/item/ItemController.js +++ b/controllers/item/ItemController.js @@ -25,7 +25,7 @@ const index = async (req, res) => { } } -const store = async (input, res) => { +const storeItem = async (input, res) => { try { const result = await Items.create(input) msg.successResponse(res, 'Create', result) @@ -34,7 +34,7 @@ const store = async (input, res) => { } } -const show = async (req, res) => { +const showItem = async (req, res) => { try { const result = await Items.findById(req.params.id) msg.successResponse(res, 'Get', result) @@ -43,7 +43,7 @@ const show = async (req, res) => { } } -const update = async (req, res) => { +const updateItem = async (req, res) => { try { const result = await Items.findByIdAndUpdate(req.params.id, { $set: req.body }, { new: true }) @@ -53,7 +53,7 @@ const update = async (req, res) => { } } -const destroy = async (req, res) => { +const destroyItem = async (req, res) => { try { const id = req.params.id const resultDestory = await Items.findByIdAndRemove(id) @@ -64,5 +64,5 @@ const destroy = async (req, res) => { } module.exports = { - index, store, show, update, destroy + index, storeItem, showItem, updateItem, destroyItem } diff --git a/routes/ItemRoute.js b/routes/ItemRoute.js index ff5ddfc..738e893 100644 --- a/routes/ItemRoute.js +++ b/routes/ItemRoute.js @@ -11,10 +11,10 @@ router.get(`${API_PATH}/item`, verifyToken, item.index) router.post(`${API_PATH}/item`, [itemValidation], (req, res) => { const errors = validationResult(req) if (!errors.isEmpty()) res.status(422).json(errors) - item.store(req.body, res) + item.storeItem(req.body, res) }) -router.get(`${API_PATH}/item/:id`, verifyToken, item.show) -router.put(`${API_PATH}/item/:id`, verifyToken, item.update) -router.delete(`${API_PATH}/item/:id`, verifyToken, item.destroy) +router.get(`${API_PATH}/item/:id`, verifyToken, item.showItem) +router.put(`${API_PATH}/item/:id`, verifyToken, item.updateItem) +router.delete(`${API_PATH}/item/:id`, verifyToken, item.destroyItem) module.exports = router From 5d25b4dc80afba973bb68d40f5203878b9edcee4 Mon Sep 17 00:00:00 2001 From: firmanjs Date: Tue, 4 Aug 2020 11:38:27 +0700 Subject: [PATCH 2/7] refactoring naming result --- controllers/item/ItemController.js | 27 +++++++++++++-------------- routes/ItemRoute.js | 8 ++++---- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/controllers/item/ItemController.js b/controllers/item/ItemController.js index 46f1d1f..6017ba6 100644 --- a/controllers/item/ItemController.js +++ b/controllers/item/ItemController.js @@ -7,7 +7,6 @@ const index = async (req, res) => { const paginate = _paging(req) try { const result = await Items.find(paginate.where) - .select('_id, name') .skip((paginate.limit * paginate.page) - paginate.limit) .limit(paginate.limit).sort(paginate.sort) const count = await Items.estimatedDocumentCount() @@ -25,44 +24,44 @@ const index = async (req, res) => { } } -const storeItem = async (input, res) => { +const store = async (input, res) => { try { - const result = await Items.create(input) - msg.successResponse(res, 'Create', result) + const storeItem = await Items.create(input) + msg.successResponse(res, 'Create', storeItem) } catch (error) { msg.errorResponse(res, error, 500) } } -const showItem = async (req, res) => { +const show = async (req, res) => { try { - const result = await Items.findById(req.params.id) - msg.successResponse(res, 'Get', result) + const showItem = await Items.findById(req.params.id) + msg.successResponse(res, 'Get', showItem) } catch (error) { msg.errorResponse(res, error, 500) } } -const updateItem = async (req, res) => { +const update = async (req, res) => { try { - const result = await Items.findByIdAndUpdate(req.params.id, + const updateItem = await Items.findByIdAndUpdate(req.params.id, { $set: req.body }, { new: true }) - msg.successResponse(res, 'Update', result) + msg.successResponse(res, 'Update', updateItem) } catch (error) { msg.errorResponse(res, error, 500) } } -const destroyItem = async (req, res) => { +const destroy = async (req, res) => { try { const id = req.params.id - const resultDestory = await Items.findByIdAndRemove(id) - msg.successResponse(res, 'Delete', resultDestory) + const destroyItem = await Items.findByIdAndRemove(id) + msg.successResponse(res, 'Delete', destroyItem) } catch (error) { msg.errorResponse(res, error, 500) } } module.exports = { - index, storeItem, showItem, updateItem, destroyItem + index, store, show, update, destroy } diff --git a/routes/ItemRoute.js b/routes/ItemRoute.js index 738e893..ff5ddfc 100644 --- a/routes/ItemRoute.js +++ b/routes/ItemRoute.js @@ -11,10 +11,10 @@ router.get(`${API_PATH}/item`, verifyToken, item.index) router.post(`${API_PATH}/item`, [itemValidation], (req, res) => { const errors = validationResult(req) if (!errors.isEmpty()) res.status(422).json(errors) - item.storeItem(req.body, res) + item.store(req.body, res) }) -router.get(`${API_PATH}/item/:id`, verifyToken, item.showItem) -router.put(`${API_PATH}/item/:id`, verifyToken, item.updateItem) -router.delete(`${API_PATH}/item/:id`, verifyToken, item.destroyItem) +router.get(`${API_PATH}/item/:id`, verifyToken, item.show) +router.put(`${API_PATH}/item/:id`, verifyToken, item.update) +router.delete(`${API_PATH}/item/:id`, verifyToken, item.destroy) module.exports = router From 26837ada8a7173ad83d99359b5d2061e9ac4e47b Mon Sep 17 00:00:00 2001 From: firmanjs Date: Wed, 5 Aug 2020 15:04:51 +0700 Subject: [PATCH 3/7] example rename function --- controllers/item/ItemController.js | 4 ++-- routes/ItemRoute.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/controllers/item/ItemController.js b/controllers/item/ItemController.js index 6017ba6..d03bc28 100644 --- a/controllers/item/ItemController.js +++ b/controllers/item/ItemController.js @@ -24,7 +24,7 @@ const index = async (req, res) => { } } -const store = async (input, res) => { +const storeItem = async (input, res) => { try { const storeItem = await Items.create(input) msg.successResponse(res, 'Create', storeItem) @@ -63,5 +63,5 @@ const destroy = async (req, res) => { } module.exports = { - index, store, show, update, destroy + index, storeItem, show, update, destroy } diff --git a/routes/ItemRoute.js b/routes/ItemRoute.js index ff5ddfc..cf6646c 100644 --- a/routes/ItemRoute.js +++ b/routes/ItemRoute.js @@ -9,9 +9,9 @@ const router = express.Router() router.get(`${API_PATH}/item`, verifyToken, item.index) router.post(`${API_PATH}/item`, [itemValidation], (req, res) => { - const errors = validationResult(req) - if (!errors.isEmpty()) res.status(422).json(errors) - item.store(req.body, res) + const errValidation = validationResult(req) + if (!errValidation.isEmpty()) res.status(422).json(errValidation) + item.storeItem(req.body, res) }) router.get(`${API_PATH}/item/:id`, verifyToken, item.show) router.put(`${API_PATH}/item/:id`, verifyToken, item.update) From fa819c932b977daacf4f98e0377f1b5cb325d62d Mon Sep 17 00:00:00 2001 From: firmanjs Date: Wed, 5 Aug 2020 16:17:17 +0700 Subject: [PATCH 4/7] refactoring naming result, and jwt --- controllers/item/ItemController.js | 14 ++++++++++---- helpers/exceptions.js | 20 +++++++++++++++----- helpers/token_validation.js | 2 +- helpers/validation.js | 15 +++++++++++++++ routes/ItemRoute.js | 7 ++----- 5 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 helpers/validation.js diff --git a/controllers/item/ItemController.js b/controllers/item/ItemController.js index d03bc28..6735d15 100644 --- a/controllers/item/ItemController.js +++ b/controllers/item/ItemController.js @@ -2,6 +2,7 @@ const Items = require('../../models/ItemModel') const msg = require('../../helpers/exceptions') const { _paging } = require('../../helpers/pagination') +const { resultValidation } = require('../../helpers/validation') const index = async (req, res) => { const paginate = _paging(req) @@ -24,12 +25,17 @@ const index = async (req, res) => { } } -const storeItem = async (input, res) => { +const store = async (req, res) => { try { - const storeItem = await Items.create(input) + const storeItem = await Items.create(req.body) msg.successResponse(res, 'Create', storeItem) } catch (error) { - msg.errorResponse(res, error, 500) + const cek = resultValidation(req) + if (cek === 0) { + msg.errorResponse(res, error, 500) + } else { + msg.errorResponse(res, cek, 500) + } } } @@ -63,5 +69,5 @@ const destroy = async (req, res) => { } module.exports = { - index, storeItem, show, update, destroy + index, store, show, update, destroy } diff --git a/helpers/exceptions.js b/helpers/exceptions.js index b8f8c8d..85b2a1c 100755 --- a/helpers/exceptions.js +++ b/helpers/exceptions.js @@ -62,11 +62,21 @@ const notFoundResponse = (res) => { } const errorResponse = (res, msg, code) => { - return res.status(code).json({ - message: `Error. ${msg}`, - status: 'bad request', - data: [] - }) + let message + if (typeof msg === 'object') { + message = { + msg, + status: 'bad request', + data: [] + } + } else { + message = { + message: `Error. ${msg}`, + status: 'bad request', + data: [] + } + } + return res.status(code).json(message) } module.exports = { diff --git a/helpers/token_validation.js b/helpers/token_validation.js index 666ad6e..be61d68 100755 --- a/helpers/token_validation.js +++ b/helpers/token_validation.js @@ -17,7 +17,7 @@ const verifyToken = async (req, res, next) => { if (err) { return res.status(500).send({ auth: false, - message: 'Failed to authenticate token not match !' + message: err }) } next() diff --git a/helpers/validation.js b/helpers/validation.js new file mode 100644 index 0000000..9b5c5d3 --- /dev/null +++ b/helpers/validation.js @@ -0,0 +1,15 @@ +'use strict' +const { validationResult } = require('express-validator') + +const resultValidation = (request) => { + const errValidation = validationResult(request) + if (!errValidation.isEmpty()) { + return errValidation + } else { + return 0 + } +} + +module.exports = { + resultValidation +} diff --git a/routes/ItemRoute.js b/routes/ItemRoute.js index cf6646c..fad2d8e 100644 --- a/routes/ItemRoute.js +++ b/routes/ItemRoute.js @@ -4,14 +4,11 @@ const item = require('../controllers/item/ItemController') const { itemValidation } = require('../controllers/item/validation') const { API_PATH } = require('../helpers/constant') const { verifyToken } = require('../helpers/token_validation') -const { validationResult } = require('express-validator') const router = express.Router() router.get(`${API_PATH}/item`, verifyToken, item.index) -router.post(`${API_PATH}/item`, [itemValidation], (req, res) => { - const errValidation = validationResult(req) - if (!errValidation.isEmpty()) res.status(422).json(errValidation) - item.storeItem(req.body, res) +router.post(`${API_PATH}/item`, verifyToken, [itemValidation], (req, res) => { + item.store(req, res) }) router.get(`${API_PATH}/item/:id`, verifyToken, item.show) router.put(`${API_PATH}/item/:id`, verifyToken, item.update) From ac6a37f7ec0ff1464b836f4b1ff814d35b2c2405 Mon Sep 17 00:00:00 2001 From: firmanjs Date: Thu, 6 Aug 2020 16:26:57 +0700 Subject: [PATCH 5/7] handling not found --- controllers/item/ItemController.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/controllers/item/ItemController.js b/controllers/item/ItemController.js index 6735d15..db35625 100644 --- a/controllers/item/ItemController.js +++ b/controllers/item/ItemController.js @@ -39,29 +39,33 @@ const store = async (req, res) => { } } -const show = async (req, res) => { +const show = async (req, res, next) => { try { - const showItem = await Items.findById(req.params.id) + const { id } = req.params + const showItem = await Items.findById(id) + if (!showItem) return next() msg.successResponse(res, 'Get', showItem) } catch (error) { msg.errorResponse(res, error, 500) } } -const update = async (req, res) => { +const update = async (req, res, next) => { try { const updateItem = await Items.findByIdAndUpdate(req.params.id, { $set: req.body }, { new: true }) + if (!updateItem) return next() msg.successResponse(res, 'Update', updateItem) } catch (error) { msg.errorResponse(res, error, 500) } } -const destroy = async (req, res) => { +const destroy = async (req, res, next) => { try { const id = req.params.id const destroyItem = await Items.findByIdAndRemove(id) + if (!destroyItem) return next() msg.successResponse(res, 'Delete', destroyItem) } catch (error) { msg.errorResponse(res, error, 500) From 1317fbee788aaa4e9bb28a5c7e4cea346b15c753 Mon Sep 17 00:00:00 2001 From: firmanjs Date: Thu, 6 Aug 2020 16:30:27 +0700 Subject: [PATCH 6/7] rename variable --- controllers/item/ItemController.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/controllers/item/ItemController.js b/controllers/item/ItemController.js index db35625..b6310ba 100644 --- a/controllers/item/ItemController.js +++ b/controllers/item/ItemController.js @@ -5,19 +5,19 @@ const { _paging } = require('../../helpers/pagination') const { resultValidation } = require('../../helpers/validation') const index = async (req, res) => { - const paginate = _paging(req) + const paginations = _paging(req) try { - const result = await Items.find(paginate.where) - .skip((paginate.limit * paginate.page) - paginate.limit) - .limit(paginate.limit).sort(paginate.sort) + const result = await Items.find(paginations.where) + .skip((paginations.limit * paginations.page) - paginations.limit) + .limit(paginations.limit).sort(paginations.sort) const count = await Items.estimatedDocumentCount() - const countPerPage = Math.ceil(count / paginate.limit) + const countPerPage = Math.ceil(count / paginations.limit) const dataMapping = { result: result, - page: paginate.page, + page: paginations.page, countPerPage: countPerPage, count: count, - limit: paginate.limit + limit: paginations.limit } msg.getResponse(req, res, dataMapping) } catch (error) { From 03f6e00555d1b4c57070e936d528e38f589e8626 Mon Sep 17 00:00:00 2001 From: firmanjs Date: Thu, 6 Aug 2020 16:32:07 +0700 Subject: [PATCH 7/7] rename variable --- controllers/item/ItemController.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/controllers/item/ItemController.js b/controllers/item/ItemController.js index b6310ba..c44f8ad 100644 --- a/controllers/item/ItemController.js +++ b/controllers/item/ItemController.js @@ -8,8 +8,9 @@ const index = async (req, res) => { const paginations = _paging(req) try { const result = await Items.find(paginations.where) + .sort(paginations.sort) + .limit(paginations.limit) .skip((paginations.limit * paginations.page) - paginations.limit) - .limit(paginations.limit).sort(paginations.sort) const count = await Items.estimatedDocumentCount() const countPerPage = Math.ceil(count / paginations.limit) const dataMapping = {