feat: extract order handlers

This commit is contained in:
RustamRu 2025-01-19 11:49:29 +03:00
parent 98daf19853
commit 979bf24767

View File

@ -74,7 +74,7 @@ const VALIDATION_MESSAGES = {
},
}
router.post('/create', async (req, res) => {
const createOrder = async (req, res) => {
const bodyErrors = []
const { customer } = req.body
@ -134,9 +134,9 @@ router.post('/create', async (req, res) => {
})
res.status(200).send({ success: true, body: order })
})
}
router.get('/:id', async (req, res) => {
const getOrder = async (req, res) => {
const { id } = req.params
if (!mongoose.Types.ObjectId.isValid(id)) {
throw new Error(VALIDATION_MESSAGES.orderId.invalid)
@ -148,9 +148,9 @@ router.get('/:id', async (req, res) => {
}
res.status(200).send({ success: true, body: order })
})
}
router.patch('/:id', async (req, res) => {
const updateOrder = async (req, res) => {
const { id } = req.params
if (!mongoose.Types.ObjectId.isValid(id)) {
throw new Error(VALIDATION_MESSAGES.orderId.invalid)
@ -210,9 +210,9 @@ router.patch('/:id', async (req, res) => {
}
res.status(200).send({ success: true, body: order })
})
}
router.delete('/:id', async (req, res) => {
const deleteOrder = async (req, res) => {
const { id } = req.params
if (!mongoose.Types.ObjectId.isValid(id)) {
throw new Error(VALIDATION_MESSAGES.orderId.invalid)
@ -225,6 +225,11 @@ router.delete('/:id', async (req, res) => {
throw new Error(VALIDATION_MESSAGES.order.notFound)
}
res.status(200).send({ success: true, body: order })
})
}
router.post('/create', createOrder)
router.get('/:id', getOrder)
router.patch('/:id', updateOrder)
router.delete('/:id', deleteOrder)
module.exports = router