Archived
2
This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
definma-api/src/routes/material.ts

105 lines
3.0 KiB
TypeScript
Raw Normal View History

2020-04-29 12:10:27 +02:00
import express from 'express';
import MaterialValidate from './validate/material';
import MaterialModel from '../models/material'
import IdValidate from './validate/id';
2020-05-07 21:55:29 +02:00
import res400 from './validate/res400';
2020-04-29 12:10:27 +02:00
const router = express.Router();
router.get('/materials', (req, res, next) => {
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
MaterialModel.find({}).lean().exec((err, data) => {
2020-05-06 14:39:04 +02:00
if (err) return next(err);
2020-04-29 12:10:27 +02:00
res.json(data.map(e => MaterialValidate.output(e)).filter(e => e !== null)); // validate all and filter null values from validation errors
});
});
router.get('/material/' + IdValidate.parameter(), (req, res, next) => {
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
MaterialModel.findById(req.params.id).lean().exec((err, data) => {
2020-05-06 14:39:04 +02:00
if (err) return next(err);
2020-04-29 12:10:27 +02:00
if (data) {
res.json(MaterialValidate.output(data));
}
else {
res.status(404).json({status: 'Not found'});
}
});
});
2020-04-29 16:09:31 +02:00
router.put('/material/' + IdValidate.parameter(), (req, res, next) => {
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
const {error, value: material} = MaterialValidate.input(req.body, 'change');
2020-05-07 21:55:29 +02:00
if (error) return res400(error, res);
2020-04-29 16:09:31 +02:00
if (material.hasOwnProperty('name')) {
MaterialModel.find({name: material.name}).lean().exec((err, data) => {
2020-05-06 14:39:04 +02:00
if (err) return next(err);
2020-04-29 16:09:31 +02:00
if (data.length > 0 && data[0]._id != req.params.id) {
res.status(400).json({status: 'Material name already taken'});
return;
}
else {
f();
}
});
}
else {
f();
}
function f() { // to resolve async
2020-05-04 15:48:07 +02:00
MaterialModel.findByIdAndUpdate(req.params.id, material, {new: true}).lean().exec((err, data) => {
2020-05-06 14:39:04 +02:00
if (err) return next(err);
2020-04-29 16:09:31 +02:00
if (data) {
res.json(MaterialValidate.output(data));
}
else {
res.status(404).json({status: 'Not found'});
}
});
}
});
router.delete('/material/' + IdValidate.parameter(), (req, res, next) => {
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
MaterialModel.findByIdAndDelete(req.params.id).lean().exec((err, data) => {
2020-05-06 14:39:04 +02:00
if (err) return next(err);
2020-04-29 16:09:31 +02:00
if (data) {
res.json({status: 'OK'})
}
else {
res.status(404).json({status: 'Not found'});
}
});
});
2020-04-29 12:10:27 +02:00
router.post('/material/new', (req, res, next) => {
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
// validate input
const {error, value: material} = MaterialValidate.input(req.body, 'new');
2020-05-07 21:55:29 +02:00
if (error) return res400(error, res);
2020-04-29 12:10:27 +02:00
MaterialModel.find({name: material.name}).lean().exec((err, data) => {
2020-05-06 14:39:04 +02:00
if (err) return next(err);
2020-04-29 12:10:27 +02:00
if (data.length > 0) {
res.status(400).json({status: 'Material name already taken'});
return;
}
new MaterialModel(material).save((err, data) => {
2020-05-06 14:39:04 +02:00
if (err) return next(err);
2020-04-29 12:10:27 +02:00
res.json(MaterialValidate.output(data.toObject()));
});
2020-04-29 16:09:31 +02:00
});
});
2020-04-29 12:10:27 +02:00
module.exports = router;