Skip to content

Commit

Permalink
Merge pull request #5 from firmanJS/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
firmanJS authored Aug 6, 2020
2 parents e4e2f8a + 03f6e00 commit 67adfbd
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 32 deletions.
52 changes: 31 additions & 21 deletions controllers/item/ItemController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,72 @@
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)
const paginations = _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 result = await Items.find(paginations.where)
.sort(paginations.sort)
.limit(paginations.limit)
.skip((paginations.limit * paginations.page) - paginations.limit)
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) {
msg.errorResponse(res, error, 500)
}
}

const store = async (input, res) => {
const store = async (req, res) => {
try {
const result = await Items.create(input)
msg.successResponse(res, 'Create', result)
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)
}
}
}

const show = async (req, res) => {
const show = async (req, res, next) => {
try {
const result = await Items.findById(req.params.id)
msg.successResponse(res, 'Get', result)
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 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)
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 resultDestory = await Items.findByIdAndRemove(id)
msg.successResponse(res, 'Delete', resultDestory)
const destroyItem = await Items.findByIdAndRemove(id)
if (!destroyItem) return next()
msg.successResponse(res, 'Delete', destroyItem)
} catch (error) {
msg.errorResponse(res, error, 500)
}
Expand Down
20 changes: 15 additions & 5 deletions helpers/exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion helpers/token_validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions helpers/validation.js
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 2 additions & 5 deletions routes/ItemRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 errors = validationResult(req)
if (!errors.isEmpty()) res.status(422).json(errors)
item.store(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)
Expand Down

0 comments on commit 67adfbd

Please sign in to comment.