fix stuff
This commit is contained in:
parent
bef37ca96f
commit
5de4027d64
1 changed files with 61 additions and 66 deletions
|
|
@ -31,7 +31,7 @@ router.post('/urls', (req, res) => {
|
|||
|
||||
const urls = JSON.parse(data);
|
||||
const newUrl = {
|
||||
link: req.body.link,
|
||||
domain: req.body.domain,
|
||||
is_public: req.body.is_public
|
||||
};
|
||||
urls.push(newUrl);
|
||||
|
|
@ -48,78 +48,73 @@ router.post('/urls', (req, res) => {
|
|||
});
|
||||
|
||||
router.delete('/urls', (req, res) => {
|
||||
const linkToDelete = req.body.link;
|
||||
if (!linkToDelete) {
|
||||
res.status(400).send('Please provide a link to delete');
|
||||
const linkToDelete = req.body.domain;
|
||||
if (!linkToDelete) {
|
||||
res.status(400).send('Please provide a link to delete');
|
||||
return;
|
||||
}
|
||||
|
||||
fs.readFile('src/Assets/urls.json', 'utf8', (err, data) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.status(500).send('Internal Server Error');
|
||||
return;
|
||||
}
|
||||
|
||||
fs.readFile('src/Assets/urls.json', 'utf8', (err, data) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.status(500).send('Internal Server Error');
|
||||
return;
|
||||
}
|
||||
const urls = JSON.parse(data);
|
||||
const index = urls.findIndex(url => url.domain === linkToDelete);
|
||||
|
||||
const urls = JSON.parse(data);
|
||||
const index = urls.findIndex(url => url.link === linkToDelete);
|
||||
if (index !== -1) {
|
||||
urls.splice(index, 1);
|
||||
|
||||
if (index !== -1) {
|
||||
urls.splice(index, 1);
|
||||
|
||||
fs.writeFile('src/Assets/urls.json', JSON.stringify(urls, null, 2), (err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.status(500).send('Error deleting URL');
|
||||
return;
|
||||
}
|
||||
res.status(204).send();
|
||||
});
|
||||
} else {
|
||||
res.status(404).send('URL not found');
|
||||
}
|
||||
});
|
||||
fs.writeFile('src/Assets/urls.json', JSON.stringify(urls, null, 2), (err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.status(500).send('Error deleting URL');
|
||||
return;
|
||||
}
|
||||
res.status(204).send();
|
||||
});
|
||||
} else {
|
||||
res.status(404).send('URL not found');
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
router.patch('/urls', (req, res) => {
|
||||
const linkToModify = req.body.link;
|
||||
if (!linkToModify) {
|
||||
res.status(400).send('Please provide a link to modify');
|
||||
const linkToModify = req.body.domain;
|
||||
if (!linkToModify) {
|
||||
res.status(400).send('Please provide a link to modify');
|
||||
return;
|
||||
}
|
||||
|
||||
fs.readFile('src/Assets/urls.json', 'utf8', (err, data) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.status(500).send('Internal Server Error');
|
||||
return;
|
||||
}
|
||||
|
||||
fs.readFile('src/Assets/urls.json', 'utf8', (err, data) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.status(500).send('Internal Server Error');
|
||||
return;
|
||||
const urls = JSON.parse(data);
|
||||
const urlToUpdate = urls.find(url => url.domain === linkToModify);
|
||||
|
||||
if (urlToUpdate) {
|
||||
if (req.body.is_public !== undefined) {
|
||||
urlToUpdate.is_public = req.body.is_public;
|
||||
}
|
||||
|
||||
const urls = JSON.parse(data);
|
||||
const urlToUpdate = urls.find(url => url.link === linkToModify);
|
||||
|
||||
if (urlToUpdate) {
|
||||
if (req.body.is_public !== undefined) {
|
||||
urlToUpdate.is_public = req.body.is_public;
|
||||
saveUrls(urls, (err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.status(500).send('Error updating URL');
|
||||
return;
|
||||
}
|
||||
|
||||
saveUrls(urls, (err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.status(500).send('Error updating URL');
|
||||
return;
|
||||
}
|
||||
res.status(204).send();
|
||||
});
|
||||
} else {
|
||||
res.status(404).send('URL not found');
|
||||
}
|
||||
});
|
||||
res.status(204).send();
|
||||
});
|
||||
} else {
|
||||
res.status(404).send('URL not found');
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/urls', (req, res) => {
|
||||
res.status(400).send('Please provide an index to delete a specific URL');
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue