From 3f4c835bd19312ee2ad756d67393b695f4e20d72 Mon Sep 17 00:00:00 2001 From: unurled Date: Mon, 5 Sep 2022 18:41:06 +0200 Subject: [PATCH] can now use custom path --- public/index.html | 12 ++++++++ public/js/app.js | 13 +++++++++ src/service/url.js | 70 +++++++++++++++++++++++++++++++++++----------- 3 files changed, 78 insertions(+), 17 deletions(-) diff --git a/public/index.html b/public/index.html index 7903e2f..6b9a4d0 100644 --- a/public/index.html +++ b/public/index.html @@ -16,6 +16,10 @@ +
+ + +
+
+
+
+

New Url for the site

+ +
+ +
\ No newline at end of file diff --git a/public/js/app.js b/public/js/app.js index 1d35f11..1c1da06 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -24,6 +24,16 @@ async function submitForm(e, form) { document.getElementById("shortUrl").innerHTML = response.shortUrl document.getElementById("shortUrl").href = response.shortUrl document.getElementById("longUrl").innerHTML = "Long Url: " + response.longUrl + if (response.edit) { + console.log("Already have this path, editing.") + } + if (response.old) { + document.getElementById("edit").style.visibility = "visible" + document.getElementById("oldLongUrl").innerHTML = response.old.longUrl + document.getElementById("oldLongUrl").href = response.old.longUrl + document.getElementById("oldShortUrl").innerHTML = response.old.shortUrl + document.getElementById("oldShortUrl").href = response.old.shortUrl + } } else alert(`An error occured.`); @@ -44,8 +54,11 @@ function buildJsonFormData(form) { } return jsonFormData; } + /*--/Functions--*/ +document.getElementById("edit").style.visibility = 'hidden' + /*--Event Listeners--*/ const sampleForm = document.querySelector("#form"); if(sampleForm) { diff --git a/src/service/url.js b/src/service/url.js index fd9519a..dcbb23a 100644 --- a/src/service/url.js +++ b/src/service/url.js @@ -37,10 +37,11 @@ function validUrl(url) { router.post('/shorten', async (req, res) => { const { - longUrl + longUrl, customPath } = req.body // destructure the longUrl from req.body.longUrl - // check base url if valid using the validUrl.isUri method + console.log(longUrl, customPath) + // check base url if valid using the validUrl method if (!validUrl(baseUrl)) { console.log("Invalid url " + baseUrl) @@ -50,7 +51,7 @@ router.post('/shorten', async (req, res) => { // if valid, we create the url code const urlCode = shortid.generate() - // check long url if valid using the validUrl.isUri method + // check long url if valid using the validUrl method if (validUrl(longUrl)) { try { /* The findOne() provides a match to only the subset of the documents @@ -63,22 +64,57 @@ router.post('/shorten', async (req, res) => { // url exist and return the response if (url) { - console.log("Return " + url) - res.json(url) + let json = url.toJSON() + console.log("Return " + JSON.stringify(json)) + res.json(json) } else { - // join the generated short code the the base url - const shortUrl = baseUrl + '/' + urlCode + try { + let doc = await Url.findOne({ + 'urlCode': customPath + }) + if (doc) { + let json + //json["edit"] = true + console.log("Found custom path") - // invoking the Url model and saving to the DB - url = new Url({ - longUrl, - shortUrl, - urlCode, - date: new Date() - }) - await url.save() - console.log("Return " + url) - res.json(url) + const shortUrl = baseUrl + '/' + customPath + + url = new Url({ + longUrl, + shortUrl, + urlCode: customPath, + date: new Date() + }) + json = url.toJSON() + await url.save() + + let old = await Url.findOneAndUpdate({ urlCode: customPath }, + { urlCode: urlCode, shortUrl: baseUrl + '/' + urlCode }, + { returnDocument: 'after' }) + + json["old"] = old.toJSON() + + res.json(json) + } else { + console.log("Not found custom path") + // join the generated short code the the base url + const shortUrl = baseUrl + '/' + customPath + + // invoking the Url model and saving to the DB + url = new Url({ + longUrl, + shortUrl, + urlCode: customPath, + date: new Date() + }) + await url.save() + console.log("Return " + url) + res.json(url) + } + } catch (err) { + console.log(err) + res.status(500).json('Server Error') + } } } // exception handler