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');
|
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
|
2025-02-07 00:30:46 +03:00
|
|
|
const users = require('../users/users.json');
|
2025-02-05 13:30:29 +03:00
|
|
|
const authoredData = data.map((unit) => {
|
|
|
|
const user = users.find((user) => user.public_id == unit.author);
|
2025-02-07 00:30:46 +03:00
|
|
|
let authoredUnit = undefined;
|
2025-02-05 13:30:29 +03:00
|
|
|
if (user) {
|
2025-02-07 00:30:46 +03:00
|
|
|
authoredUnit = { ...unit, author: user };
|
2025-02-05 13:30:29 +03:00
|
|
|
}
|
2025-02-07 00:30:46 +03:00
|
|
|
return authoredUnit;
|
2025-02-05 13:30:29 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2025-02-07 00:30:46 +03:00
|
|
|
data.push({...updatedUnit, author: updatedUnit.author.public_id});
|
2025-02-05 13:30:29 +03:00
|
|
|
|
|
|
|
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-07 00:30:46 +03:00
|
|
|
data.push({ ...newUnit, id: newId });
|
2024-12-27 23:15:26 +03:00
|
|
|
|
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);
|
|
|
|
res.send({ message: `Unit with ID ${id} deleted` });
|
2024-12-27 23:15:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/:id', (req, res) => {
|
2025-02-07 00:30:46 +03:00
|
|
|
const users = require('../users/users.json');
|
2024-12-27 23:15:26 +03:00
|
|
|
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 18:55:22 +03:00
|
|
|
const user = users.find((user) => user.public_id == unit.author);
|
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
|
|
|
});
|