Add patch /:shortUrl

This commit is contained in:
Christopher Cookman 2024-11-15 12:09:10 -07:00
parent 6398baa64e
commit 1461221e57

View file

@ -116,6 +116,34 @@ app.post('/shorten', (req, res) => { // Shorten URL
});
});
app.patch('/:shortUrl', (req, req) => {
if (req.body.passcode !== passcode) {
return res.status(403).json({ error: 'Invalid passcode' });
}
if (!req.body.url) {
return res.status(400).json({ error: 'Please provide a URL' });
}
db.get('SELECT * FROM urls WHERE shortUrl = ?', [shortUrl], (err, row) => {
if (err) {
console.error(err);
return res.status(500).json({ error: 'Internal server error', fullError: err.stack });
}
if (row) {
db.run('UPDATE urls SET url = ? WHERE shortUrl = ?', [url, shortUrl], function (err) {
if (err) {
console.error(err);
return res.status(500).json({ error: 'Failed to update the URL', fullError: err.stack });
}
// Return success response with the updated data
res.status(200).json({ message: 'URL updated successfully', shortUrl: shortUrl, newUrl: url });
});
}
else {
res.status(404).json({ error: 'Short URL not found' });
}
});
})
port = process.env.SERVER_PORT || 3000;
passcode = process.env.API_KEY;