Clean code.
This commit is contained in:
47
src/js/controller/applicationCtrl.js
Normal file
47
src/js/controller/applicationCtrl.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const ObjectId = require('mongodb').ObjectId;
|
||||
const Repository = require('../repository/repository');
|
||||
const router = require('express').Router();
|
||||
|
||||
const applicationRepository = new Repository('applications');
|
||||
|
||||
router.get('/', (request, response) => {
|
||||
applicationRepository.find({}, results => {
|
||||
response.json(results);
|
||||
});
|
||||
});
|
||||
|
||||
router.post('/', (request, response) => {
|
||||
applicationRepository.insert(request.body, (result) => {
|
||||
response.json(result);
|
||||
});
|
||||
});
|
||||
|
||||
router.put('/:applicationId', (request, response) => {
|
||||
const applicationId = ObjectId(request.params.applicationId);
|
||||
applicationRepository.find({_id: applicationId}, entity => {
|
||||
if (entity.length === 0) {
|
||||
response.status(404).send();
|
||||
} else {
|
||||
const applicationToUpdate = request.body;
|
||||
applicationToUpdate._id = applicationId;
|
||||
applicationRepository.update(applicationToUpdate, () => {
|
||||
response.status(204).send();
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
router.delete('/:applicationId', (request, response) => {
|
||||
const applicationId = ObjectId(request.params.applicationId);
|
||||
applicationRepository.find({_id: applicationId}, entity => {
|
||||
if (entity.length === 0) {
|
||||
response.status(404).send();
|
||||
} else {
|
||||
applicationRepository.delete(applicationId, () => {
|
||||
response.status(204).send();
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user