add removing feature

This commit is contained in:
Thomas Ruoff
2018-02-02 23:57:14 +01:00
parent 558ae2e590
commit 5370571574
5 changed files with 72 additions and 24 deletions

View File

@@ -25,27 +25,50 @@ app.post('/api/pdf/generate/:template', (req, res) => {
.then(() => id);
})
.then(id => {
res.send({id: id});
res.end();
res
.status(200)
.json({id: id});
})
.catch((err) => {
console.error('Error:', err, 'for', req.url);
res.sendStatus(500).end(err);
res
.status(500)
.json({error: err.toString()});
});
});
app.get('/api/pdf/latest', (req, res) => {
store
.list()
.then(results => res.json(results))
.catch(err => res.sendStatus(500).end(err));
.then(results => res
.status(200)
.json(results)
)
.catch(err => res
.status(500)
.json({error: err.toString()})
);
});
app.delete('/api/pdf/latest/:id', (req, res) => {
const { id } = req.params;
console.log(`Deleting ${id}...`);
store
.remove(id)
.then(() => res.sendStatus(202))
.catch(err => res
.status(500)
.json({error: err.toString()})
);
});
app.options('/api/pdf/:id');
app.get('/api/pdf/:id', (req, res) => {
const {id} = req.params;
res.sendFile(getPdfPath(id));
res
.status(200)
.sendFile(getPdfPath(id));
});
app.use(express.static('../client/build'));

View File

@@ -9,12 +9,14 @@ const sortBy = require('lodash.sortby');
const list = promisify(store.list);
const load = promisify(store.load);
const add = promisify(store.add);
const remove = promisify(store.remove);
module.exports = {
list: () => list()
.then(result => sortBy(result, 'created').reverse()),
load: id => load(id),
add: item => add(item),
remove: id => remove(id),
}