diff --git a/index.js b/index.js index 16bf3cb..0010991 100644 --- a/index.js +++ b/index.js @@ -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;