54 lines
1.7 KiB
JavaScript
54 lines
1.7 KiB
JavaScript
const Repository = require('../repository/repository');
|
|
const authenticationFilter = require('../filter/authenticationFilter');
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
const applicationRepository = new Repository('applications');
|
|
|
|
router.get('/', (request, response) => {
|
|
applicationRepository.find({}, results => {
|
|
response.json(results);
|
|
});
|
|
});
|
|
|
|
router.get('/:applicationId', (request, response) => {
|
|
applicationRepository.find({_id: request.params.applicationId}, (result) => {
|
|
response.json(result[0]);
|
|
});
|
|
});
|
|
|
|
router.post('/', (request, response) => {
|
|
applicationRepository.insert(request.body, (result) => {
|
|
response.json(result);
|
|
});
|
|
});
|
|
|
|
router.put('/:applicationId', (request, response) => {
|
|
const applicationId = 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', authenticationFilter, (request, response) => {
|
|
const applicationId = 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; |