dev (#1)
All checks were successful
Build Urlshort / publish (push) Successful in 1m29s

Reviewed-on: #1
Co-authored-by: fede <federico.nicolas.polidoro@gmail.com>
Co-committed-by: fede <federico.nicolas.polidoro@gmail.com>
This commit was merged in pull request #1.
This commit is contained in:
2026-03-18 16:33:48 -03:00
committed by fedpo
parent b00ed96b9e
commit e9566dcc62
5 changed files with 44 additions and 26 deletions

View File

@@ -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 {