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