hecha una implementacion que no puede tener coinsidencias

This commit is contained in:
2026-03-12 11:17:16 -03:00
parent 1b3ae43d61
commit 389f858369
2 changed files with 16 additions and 16 deletions

View File

@@ -3,8 +3,7 @@ package models
import "time" import "time"
type Url struct { type Url struct {
Shorturl string `gorm:"primaryKey"` Shorturl int64 `gorm:"primaryKey;autoIncrement:true"`
// Id uint64 `gorm: "primaryKey;autoIncrement"`
Longurl string `gorm:"uniqueIndex:idx_lurl"` Longurl string `gorm:"uniqueIndex:idx_lurl"`
Time time.Time `gorm:"autoCreateTime"` Time time.Time `gorm:"autoCreateTime"`
} }

View File

@@ -1,7 +1,6 @@
package repository package repository
import ( import (
"crypto/sha3"
"fmt" "fmt"
"url-short/models" "url-short/models"
@@ -16,29 +15,31 @@ func CrearUrl(longURL string, db *gorm.DB) (string, error) {
return "", result.Error return "", result.Error
} }
if counturl != 0 { if counturl != 0 {
println(1) // println(1)
var short models.Url var short models.Url
result = db.Model(&short).Where("longurl = ?", longURL).Select("shorturl").First(&short) result = db.Model(&short).Where("longurl = ?", longURL).Select("shorturl").First(&short)
if result.Error != nil { if result.Error != nil {
return "", result.Error return "", result.Error
} }
return short.Shorturl, nil return fmt.Sprintf("%d", short.Shorturl), nil
} }
// shortURL := base64.StdEncoding.EncodeToString([]byte(longURL))[:10] // shortURL := base64.StdEncoding.EncodeToString([]byte(longURL))[:10]
sha := sha3.New224() // sha := sha3.New224()
sha.Write([]byte(longURL)) // sha.Write([]byte(longURL))
shortURL := fmt.Sprintf("%x", sha.Sum(nil))[:10] // shortURL := fmt.Sprintf("%x", sha.Sum(nil))[:10]
result = db.Model(&models.Url{}).Where("shorturl = ?", shortURL).Count(&counturl) // result = db.Model(&models.Url{}).Where("shorturl = ?", shortURL).Count(&counturl)
if result.Error != nil { // if result.Error != nil {
return "", result.Error // return "", result.Error
} // }
if counturl != 0 { // if counturl != 0 {
return shortURL, nil // return shortURL, nil
} // }
result = db.Create(&models.Url{Longurl: longURL, Shorturl: shortURL}) var url models.Url
result = db.Create(&models.Url{Longurl: longURL}).Scan(&url)
shortURL := fmt.Sprintf("%d", url.Shorturl)
if result.Error != nil { if result.Error != nil {
return "", result.Error return "", result.Error