dev #1
@@ -1,18 +1,6 @@
|
||||
<script>
|
||||
import { url } from "./assets/hooks/getUrl";
|
||||
import AcortadorForm from "./assets/lib/AcortadorForm.svelte";
|
||||
$effect(
|
||||
(async () => {
|
||||
if (window.location.pathname.match(/^\/short\/url\/\d+$/)) {
|
||||
const numero = window.location.pathname.split("/").pop();
|
||||
// console.log(numero);
|
||||
const res = await fetch(`${$url}/url/${numero}`, {
|
||||
method: "GET",
|
||||
});
|
||||
console.log(res);
|
||||
}
|
||||
})(),
|
||||
);
|
||||
</script>
|
||||
|
||||
<section id="center">
|
||||
|
||||
@@ -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",
|
||||
);
|
||||
|
||||
@@ -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 {
|
||||
@@ -36,15 +36,26 @@
|
||||
|
||||
{#if popup}
|
||||
<div transition:fade class="popup-dialog">
|
||||
<p>Short URL creada</p>
|
||||
<button
|
||||
onclick={() => {
|
||||
navigator.clipboard.writeText(`${$redirect}/url/${popup}`);
|
||||
popup = "";
|
||||
}}
|
||||
>
|
||||
Copy</button
|
||||
>
|
||||
{#if popup == "Error"}
|
||||
<p>Fallo al crear la url</p>
|
||||
<button
|
||||
onclick={() => {
|
||||
popup = "";
|
||||
}}
|
||||
>
|
||||
Cerrar</button
|
||||
>
|
||||
{:else}
|
||||
<p>Short URL creada</p>
|
||||
<button
|
||||
onclick={() => {
|
||||
navigator.clipboard.writeText(`${$redirect}/url/${popup}`);
|
||||
popup = "";
|
||||
}}
|
||||
>
|
||||
Copiar</button
|
||||
>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user