created API For Aplication Absensi
This commit is contained in:
28
app/modules/branch/controllers/branch.controller.js
Normal file
28
app/modules/branch/controllers/branch.controller.js
Normal file
@@ -0,0 +1,28 @@
|
||||
const services = require('../services/branch.service')
|
||||
|
||||
const getAll = async (req, res) => {
|
||||
const response = await services.getAll(req, res)
|
||||
return response
|
||||
}
|
||||
|
||||
const create = async (req, res) => {
|
||||
const response = await services.create(req, res)
|
||||
return response
|
||||
}
|
||||
|
||||
const update = async (req, res) => {
|
||||
const response = await services.update(req, res)
|
||||
return response
|
||||
}
|
||||
|
||||
const destroy = async (req, res) => {
|
||||
const response = await services.destroy(req, res)
|
||||
return response
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getAll,
|
||||
create,
|
||||
update,
|
||||
destroy
|
||||
}
|
||||
23
app/modules/branch/routes/branch.route.js
Normal file
23
app/modules/branch/routes/branch.route.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const express = require('express')
|
||||
const router = express.Router()
|
||||
const controller = require('../controllers/branch.controller')
|
||||
const apiKey = require('../../../middlewares/apiKey')
|
||||
const jwt = require('../../../middlewares/authentication')
|
||||
|
||||
router.get('/', apiKey, (req, res) => {
|
||||
controller.getAll(req, res)
|
||||
})
|
||||
|
||||
router.post('/', apiKey, jwt, (req, res) => {
|
||||
controller.create(req, res)
|
||||
} )
|
||||
|
||||
router.put('/:id', apiKey, jwt, (req, res) => {
|
||||
controller.update(req, res)
|
||||
})
|
||||
|
||||
router.put('/:id', apiKey, jwt, (req, res) => {
|
||||
controller.destroy(req, res)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
96
app/modules/branch/services/branch.service.js
Normal file
96
app/modules/branch/services/branch.service.js
Normal file
@@ -0,0 +1,96 @@
|
||||
const response = require('../../../helpers/responses')
|
||||
const db = require('../../../../models/migration')
|
||||
const errorHandler = require('../../../middlewares/errorHandler')
|
||||
const {sequelize} = require('../../../../models/migration')
|
||||
const { where } = require('sequelize')
|
||||
const Branch = db.Branch
|
||||
|
||||
const getAll = async (req, res) => {
|
||||
try {
|
||||
const branch = await Branch.findAll({
|
||||
order: [['created_at', 'DESC']]
|
||||
})
|
||||
return response.success(res, branch, 'successfully loaded')
|
||||
} catch (error) {
|
||||
errorHandler(error, req, res)
|
||||
return response.failed(res, 500, error.message)
|
||||
}
|
||||
}
|
||||
|
||||
const create = async (req, res) => {
|
||||
const t = await sequelize.transaction()
|
||||
try {
|
||||
const user_id = req.user.id
|
||||
const body = req.body
|
||||
|
||||
const branch = await Branch.create({
|
||||
...body,
|
||||
user_id
|
||||
})
|
||||
|
||||
await t.commit()
|
||||
return response.success(res, branch, 'create successfuly')
|
||||
} catch (error) {
|
||||
errorHandler(error, req, res)
|
||||
return response.failed(res, 500, error.message)
|
||||
}
|
||||
}
|
||||
|
||||
const update = async (req, res) => {
|
||||
const t = await sequelize.transaction()
|
||||
try {
|
||||
const id = req.params.id
|
||||
const body = req.body
|
||||
const user_id = req.user.id
|
||||
|
||||
const branch = await Branch.findOne({
|
||||
where: {id},
|
||||
transaction: t
|
||||
})
|
||||
|
||||
if (!branch) {
|
||||
await t.rollback()
|
||||
return response.failed(res, 404, 'Data Not Found')
|
||||
}
|
||||
|
||||
const branchUpdate = await branch.update({
|
||||
...body,
|
||||
user_id
|
||||
})
|
||||
|
||||
await t.commit()
|
||||
return response.success(res, branchUpdate, 'Updated Successfuly')
|
||||
|
||||
|
||||
} catch (error) {
|
||||
await t.rollback()
|
||||
errorHandler(error, req, res)
|
||||
return response.failed(res, 500, error.message)
|
||||
}
|
||||
}
|
||||
|
||||
const destroy = async (req, res) => {
|
||||
try {
|
||||
const id = req.params.id
|
||||
const branch = await Branch.findOne({
|
||||
where: { id },
|
||||
})
|
||||
|
||||
if (!branch) {
|
||||
return response.failed(res, 404, 'Data Not Found')
|
||||
}
|
||||
await branch.destroy();
|
||||
return response.success(res, null, 'Deleted Successfuly')
|
||||
|
||||
} catch (error) {
|
||||
errorHandler(error, req, res)
|
||||
return response.failed(res, 500, error.message)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getAll,
|
||||
create,
|
||||
update,
|
||||
destroy
|
||||
}
|
||||
Reference in New Issue
Block a user