Fix patching part 2
This commit is contained in:
parent
656700d1a2
commit
e5c2c99165
3
index.js
3
index.js
|
@ -94,6 +94,7 @@ app.post('/shorten', (req, res) => { // Shorten URL
|
||||||
if (req.body.passcode !== passcode) {
|
if (req.body.passcode !== passcode) {
|
||||||
return res.status(403).json({ error: 'Invalid passcode' });
|
return res.status(403).json({ error: 'Invalid passcode' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a 8 character long string, only if { shortUrl } doesnt exist in body
|
// 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);
|
const shortUrl = req.body.shortUrl || Math.random().toString(36).substr(2, 8);
|
||||||
// Check if shortUrl is already in use
|
// Check if shortUrl is already in use
|
||||||
|
@ -116,6 +117,7 @@ app.post('/shorten', (req, res) => { // Shorten URL
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
app.patch('/:shortUrl', (req, res) => {
|
app.patch('/:shortUrl', (req, res) => {
|
||||||
if (req.body.passcode !== passcode) {
|
if (req.body.passcode !== passcode) {
|
||||||
return res.status(403).json({ error: 'Invalid passcode' });
|
return res.status(403).json({ error: 'Invalid passcode' });
|
||||||
|
@ -123,6 +125,7 @@ app.patch('/:shortUrl', (req, res) => {
|
||||||
if (!req.body.url) {
|
if (!req.body.url) {
|
||||||
return res.status(400).json({ error: 'Please provide a 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) => {
|
db.get('SELECT * FROM urls WHERE shortUrl = ?', [shortUrl], (err, row) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|
Loading…
Reference in a new issue