small fixes in kfu-m-24-1/eng-it-lean #88
@ -5,15 +5,16 @@ const router = require('express').Router();
|
||||
module.exports = router;
|
||||
|
||||
const data = require('./units.json');
|
||||
const users = require('../users/users.json');
|
||||
router.get('/', (req, res) => {
|
||||
// for every data set author from users and save it to authoredData variable
|
||||
const users = require('../users/users.json');
|
||||
const authoredData = data.map((unit) => {
|
||||
const user = users.find((user) => user.public_id == unit.author);
|
||||
let authoredUnit = undefined;
|
||||
if (user) {
|
||||
unit.author = user;
|
||||
authoredUnit = { ...unit, author: user };
|
||||
}
|
||||
return unit;
|
||||
return authoredUnit;
|
||||
});
|
||||
|
||||
res.send(authoredData);
|
||||
@ -39,9 +40,8 @@ router.post('/:id', (req, res) => {
|
||||
|
||||
data.splice(index, 1);
|
||||
|
||||
data.push(updatedUnit);
|
||||
data.push({...updatedUnit, author: updatedUnit.author.public_id});
|
||||
|
||||
fs.writeFileSync(path.join(__dirname, 'units.json'), JSON.stringify(data));
|
||||
res.status(200).send(data);
|
||||
});
|
||||
|
||||
@ -61,12 +61,8 @@ router.put('/', (req, res) => {
|
||||
}
|
||||
|
||||
const newId = data.length + 1;
|
||||
// const filename = newUnit.name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
||||
// fs.writeFileSync(path.join(__dirname, 'data', `${filename}.md`), newUnit.content);
|
||||
data.push({ ...newUnit, id: newId });
|
||||
|
||||
data.push({ ...unit, id: newId });
|
||||
|
||||
fs.writeFileSync(path.join(__dirname, 'units.json'), JSON.stringify(data));
|
||||
res.status(200).send(data);
|
||||
});
|
||||
|
||||
@ -79,11 +75,11 @@ router.delete('/:id', (req, res) => {
|
||||
}
|
||||
|
||||
data.splice(index, 1);
|
||||
fs.writeFileSync(path.join(__dirname, 'units.json'), JSON.stringify(data));
|
||||
res.send({ message: `Unit with ID ${id} deleted` });
|
||||
});
|
||||
|
||||
router.get('/:id', (req, res) => {
|
||||
const users = require('../users/users.json');
|
||||
const id = parseInt(req.params.id);
|
||||
const unit = data.find((unit) => unit.id === id);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user