Fix patching part 2

This commit is contained in:
Christopher Cookman 2024-11-15 12:24:27 -07:00
parent 656700d1a2
commit e5c2c99165

View file

@ -94,6 +94,7 @@ app.post('/shorten', (req, res) => { // Shorten URL
if (req.body.passcode !== passcode) {
return res.status(403).json({ error: 'Invalid passcode' });
}
// Generate a 8 character long string, only if { shortUrl } doesnt exist in body
const shortUrl = req.body.shortUrl || Math.random().toString(36).substr(2, 8);
// Check if shortUrl is already in use
@ -116,6 +117,7 @@ app.post('/shorten', (req, res) => { // Shorten URL
});
});
app.patch('/:shortUrl', (req, res) => {
if (req.body.passcode !== passcode) {
return res.status(403).json({ error: 'Invalid passcode' });
@ -123,6 +125,7 @@ app.patch('/:shortUrl', (req, res) => {
if (!req.body.url) {
return res.status(400).json({ error: 'Please provide a URL' });
}
const shortUrl = req.params.shortUrl;
db.get('SELECT * FROM urls WHERE shortUrl = ?', [shortUrl], (err, row) => {
if (err) {
console.error(err);