Files
ExpressJS-Mongo/src/js/controller/applicationCtrl.js
2020-09-06 19:36:58 +02:00

47 lines
1.5 KiB
JavaScript

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;