feat: add /users endpoint; update /units
This commit is contained in:
@@ -4,9 +4,45 @@ const router = require('express').Router();
|
||||
|
||||
module.exports = router;
|
||||
|
||||
const data = require('./data/units.json');
|
||||
const data = require('./units.json');
|
||||
const users = require('../users/users.json');
|
||||
router.get('/', (req, res) => {
|
||||
res.send(data);
|
||||
// for every data set author from users and save it to authoredData variable
|
||||
const authoredData = data.map((unit) => {
|
||||
const user = users.find((user) => user.public_id == unit.author);
|
||||
if (user) {
|
||||
unit.author = user;
|
||||
}
|
||||
return unit;
|
||||
});
|
||||
|
||||
res.send(authoredData);
|
||||
});
|
||||
|
||||
router.post('/:id', (req, res) => {
|
||||
const id = parseInt(req.params.id);
|
||||
const updatedUnit = req.body;
|
||||
|
||||
if (!updatedUnit) {
|
||||
return res.status(400).send('No unit to be added');
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
return res.status(500).send('No data to be updated');
|
||||
}
|
||||
|
||||
const index = data.findIndex((unit) => unit.id === id);
|
||||
|
||||
if (index < 0) {
|
||||
return res.status(404).send('Not found');
|
||||
}
|
||||
|
||||
data.splice(index, 1);
|
||||
|
||||
data.push(updatedUnit);
|
||||
|
||||
fs.writeFileSync(path.join(__dirname, 'units.json'), JSON.stringify(data));
|
||||
res.status(200).send(data);
|
||||
});
|
||||
|
||||
router.put('/', (req, res) => {
|
||||
@@ -16,17 +52,21 @@ router.put('/', (req, res) => {
|
||||
return res.status(400).send('No new unit to be added');
|
||||
}
|
||||
|
||||
if (!newUnit.author) {
|
||||
return res.status(400).send('User is not logged in!');
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
return res.status(500).send('No data to be updated');
|
||||
}
|
||||
|
||||
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);
|
||||
// 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({ id: newId, filename: filename, name: newUnit.name });
|
||||
data.push({ ...unit, id: newId });
|
||||
|
||||
fs.writeFileSync(path.join(__dirname, 'data', 'units.json'), JSON.stringify(data));
|
||||
fs.writeFileSync(path.join(__dirname, 'units.json'), JSON.stringify(data));
|
||||
res.status(200).send(data);
|
||||
});
|
||||
|
||||
@@ -39,7 +79,7 @@ router.delete('/:id', (req, res) => {
|
||||
}
|
||||
|
||||
data.splice(index, 1);
|
||||
fs.writeFileSync(path.join(__dirname, 'data', 'units.json'), JSON.stringify(data));
|
||||
fs.writeFileSync(path.join(__dirname, 'units.json'), JSON.stringify(data));
|
||||
res.send({ message: `Unit with ID ${id} deleted` });
|
||||
});
|
||||
|
||||
@@ -48,15 +88,17 @@ router.get('/:id', (req, res) => {
|
||||
const unit = data.find((unit) => unit.id === id);
|
||||
|
||||
if (!unit) {
|
||||
return res.status(404).send('Not found');
|
||||
return res.status(404).send('Unit not found');
|
||||
}
|
||||
|
||||
const unitFilepath = path.join(__dirname, 'data', `${unit.filename}.md`);
|
||||
const unitContent = fs.readFileSync(unitFilepath, 'utf-8');
|
||||
|
||||
if (!unitContent) {
|
||||
return res.status(404).send('Not found');
|
||||
const user = users.find((user) => {
|
||||
if (user.public_id == unit.author) {
|
||||
return user;
|
||||
}
|
||||
});
|
||||
if (!user) {
|
||||
return res.status(404).send('User not found');
|
||||
}
|
||||
|
||||
res.send({ ...unit, content: unitContent });
|
||||
res.send({...unit, author: user});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user