2024-12-27 23:15:26 +03:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const router = require('express').Router();
|
|
|
|
|
|
|
|
module.exports = router;
|
|
|
|
|
2025-02-05 13:30:29 +03:00
|
|
|
const data = require('./units.json');
|
|
|
|
const users = require('../users/users.json');
|
2024-12-27 23:15:26 +03:00
|
|
|
router.get('/', (req, res) => {
|
2025-02-05 13:30:29 +03:00
|
|
|
// 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);
|
2024-12-27 23:15:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
router.put('/', (req, res) => {
|
2025-01-30 12:41:00 +03:00
|
|
|
const newUnit = req.body;
|
2024-12-27 23:15:26 +03:00
|
|
|
|
2025-01-30 12:41:00 +03:00
|
|
|
if (!newUnit) {
|
|
|
|
return res.status(400).send('No new unit to be added');
|
|
|
|
}
|
|
|
|
|
2025-02-05 13:30:29 +03:00
|
|
|
if (!newUnit.author) {
|
|
|
|
return res.status(400).send('User is not logged in!');
|
|
|
|
}
|
|
|
|
|
2025-01-30 12:41:00 +03:00
|
|
|
if (!data) {
|
|
|
|
return res.status(500).send('No data to be updated');
|
|
|
|
}
|
2024-12-27 23:15:26 +03:00
|
|
|
|
2025-01-30 12:41:00 +03:00
|
|
|
const newId = data.length + 1;
|
2025-02-05 13:30:29 +03:00
|
|
|
// const filename = newUnit.name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
|
|
|
// fs.writeFileSync(path.join(__dirname, 'data', `${filename}.md`), newUnit.content);
|
2024-12-27 23:15:26 +03:00
|
|
|
|
2025-02-05 13:30:29 +03:00
|
|
|
data.push({ ...unit, id: newId });
|
2024-12-27 23:15:26 +03:00
|
|
|
|
2025-02-05 13:30:29 +03:00
|
|
|
fs.writeFileSync(path.join(__dirname, 'units.json'), JSON.stringify(data));
|
2025-01-30 12:41:00 +03:00
|
|
|
res.status(200).send(data);
|
2024-12-27 23:15:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
router.delete('/:id', (req, res) => {
|
2025-01-30 12:41:00 +03:00
|
|
|
const id = parseInt(req.params.id);
|
|
|
|
const index = data.findIndex((unit) => unit.id === id);
|
2024-12-27 23:15:26 +03:00
|
|
|
|
2025-01-30 12:41:00 +03:00
|
|
|
if (index < 0) {
|
|
|
|
return res.status(404).send('Not found');
|
|
|
|
}
|
2024-12-27 23:15:26 +03:00
|
|
|
|
2025-01-30 12:41:00 +03:00
|
|
|
data.splice(index, 1);
|
2025-02-05 13:30:29 +03:00
|
|
|
fs.writeFileSync(path.join(__dirname, 'units.json'), JSON.stringify(data));
|
2025-01-30 12:41:00 +03:00
|
|
|
res.send({ message: `Unit with ID ${id} deleted` });
|
2024-12-27 23:15:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/:id', (req, res) => {
|
|
|
|
const id = parseInt(req.params.id);
|
|
|
|
const unit = data.find((unit) => unit.id === id);
|
|
|
|
|
|
|
|
if (!unit) {
|
2025-02-05 13:30:29 +03:00
|
|
|
return res.status(404).send('Unit not found');
|
2024-12-27 23:15:26 +03:00
|
|
|
}
|
|
|
|
|
2025-02-05 13:30:29 +03:00
|
|
|
const user = users.find((user) => {
|
|
|
|
if (user.public_id == unit.author) {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!user) {
|
|
|
|
return res.status(404).send('User not found');
|
2024-12-27 23:15:26 +03:00
|
|
|
}
|
|
|
|
|
2025-02-05 13:30:29 +03:00
|
|
|
res.send({...unit, author: user});
|
2024-12-27 23:15:26 +03:00
|
|
|
});
|