From e9566dcc6202b45c501e7fdde015082a91435c22 Mon Sep 17 00:00:00 2001 From: fede Date: Wed, 18 Mar 2026 16:33:48 -0300 Subject: [PATCH] dev (#1) Reviewed-on: https://fedesrv.ddns.net/git/fede/url-short/pulls/1 Co-authored-by: fede Co-committed-by: fede --- .gitignore | 2 ++ front/src/App.svelte | 12 ------- front/src/assets/hooks/getUrl.js | 2 +- front/src/assets/lib/AcortadorForm.svelte | 40 ++++++++++++++++------- repository/CrearUrl.go | 14 +++++++- 5 files changed, 44 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index f3fb968..c782be2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ front/node_modules shorturls.db +front/dist +url-short diff --git a/front/src/App.svelte b/front/src/App.svelte index 94cbe5b..1acc522 100644 --- a/front/src/App.svelte +++ b/front/src/App.svelte @@ -1,18 +1,6 @@
diff --git a/front/src/assets/hooks/getUrl.js b/front/src/assets/hooks/getUrl.js index 760a15c..e6c9db1 100644 --- a/front/src/assets/hooks/getUrl.js +++ b/front/src/assets/hooks/getUrl.js @@ -8,6 +8,6 @@ export const url = readable( export const redirect = readable( typeof window !== "undefined" && window.location.hostname === "localhost" - ? "http://localhost:5173/short" + ? "http://localhost:8080/" : "https://fedesrv.ddns.net/s", ); diff --git a/front/src/assets/lib/AcortadorForm.svelte b/front/src/assets/lib/AcortadorForm.svelte index d985e1e..289e0ac 100644 --- a/front/src/assets/lib/AcortadorForm.svelte +++ b/front/src/assets/lib/AcortadorForm.svelte @@ -13,16 +13,16 @@ onsubmit={async (e) => { e.preventDefault(); // console.log(e.target[0].value); - // console.log(input); + console.log($url); const req = { longUrl: input.value }; try { - const res = await fetch($url + "/url", { + const res = await fetch(`${$url}/url/`, { method: "POST", body: JSON.stringify({ ...req }), }); if (res.ok) { const data = await res.json(); - // console.log(data.shortUrl); + console.log(data.shortUrl); popup = data.shortUrl; } } catch { @@ -30,21 +30,33 @@ } }} > + {#if popup} {/if} @@ -82,4 +94,8 @@ .popup-dialog button:hover { background-color: #0056b3; } + button, + input { + padding: 0.25rem; + } diff --git a/repository/CrearUrl.go b/repository/CrearUrl.go index 3187137..c46a1f6 100644 --- a/repository/CrearUrl.go +++ b/repository/CrearUrl.go @@ -2,6 +2,7 @@ package repository import ( "fmt" + "math/rand/v2" "url-short/models" "gorm.io/gorm" @@ -38,7 +39,18 @@ func CrearUrl(longURL string, db *gorm.DB) (string, error) { // } var url models.Url - result = db.Create(&models.Url{Longurl: longURL}).Scan(&url) + for { + url.Shorturl = rand.Int64() + result = db.Model(&models.Url{}).Where("shorturl = ?", url.Shorturl).Count(&counturl) + if result.Error != nil { + return "", result.Error + } + if counturl == 0 { + break + } + } + + result = db.Create(&models.Url{Longurl: longURL, Shorturl: url.Shorturl}).Scan(&url) shortURL := fmt.Sprintf("%d", url.Shorturl) if result.Error != nil {