feat: add '/units' endpoints; add 'put' and 'delete' requests to '/dictionaries' endpoint
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
[
|
||||
{
|
||||
"id": 0,
|
||||
"description": "1000 часто используемых",
|
||||
"description": "10 часто используемых",
|
||||
"imageFilename": "kart1.jpg"
|
||||
},
|
||||
{
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const router = require("express").Router();
|
||||
|
||||
module.exports = router;
|
||||
@@ -9,6 +11,59 @@ router.get("/", (req, res) => {
|
||||
res.send(data);
|
||||
});
|
||||
|
||||
// Put new dictionary to the array of dictionaries
|
||||
router.put('/new', (req, res) => {
|
||||
if (!data || !Array.isArray(data)) {
|
||||
return res.status(400).send('No array of dictionaries found`');
|
||||
}
|
||||
|
||||
const updatedData = req.body;
|
||||
|
||||
if (!updatedData) {
|
||||
return res.status(400).send('No data to update'); // Bad request
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
return res.status(500).send('No data to update'); // Internal server error
|
||||
}
|
||||
|
||||
indexedUpdatedData = { id: data.length, ...updatedData }; // Add the new dictionary to the array
|
||||
|
||||
data.push(indexedUpdatedData); // Add the new dictionary to the array
|
||||
|
||||
fs.writeFile(path.join(__dirname, 'data/dictionaries.json'), JSON.stringify(data), (err) => {
|
||||
if (err) {
|
||||
console.error(err); // Log the error
|
||||
return res.status(500).send('Error saving data');
|
||||
}
|
||||
res.status(200).json(data); // Send back the updated data
|
||||
});
|
||||
});
|
||||
|
||||
router.delete('/:id', (req, res) => {
|
||||
const id = parseInt(req.params.id); // Get the dictionary id from the URL
|
||||
|
||||
if (!id || isNaN(id)) {
|
||||
return res.status(400).send('Invalid ID'); // Bad request
|
||||
}
|
||||
|
||||
const index = data.findIndex((dictionary) => dictionary.id === id);
|
||||
|
||||
if (index < 0) {
|
||||
return res.status(404).send('Not found'); // Not found
|
||||
}
|
||||
|
||||
data.splice(index, 1); // Remove the dictionary from the array
|
||||
|
||||
fs.writeFile(path.join(__dirname, 'data/dictionaries.json'), JSON.stringify(data), (err) => {
|
||||
if (err) {
|
||||
console.error(err); // Log the error
|
||||
return res.status(500).send('Error saving data');
|
||||
}
|
||||
res.send({ message: `Dictionary with id ${id} deleted` });
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/:id", (req, res) => {
|
||||
const id = parseInt(req.params.id);
|
||||
const words = wordsData.find((word) => word.id === id);
|
||||
|
||||
Reference in New Issue
Block a user