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"
type Url struct {
Shorturl string `gorm:"primaryKey"`
// Id uint64 `gorm: "primaryKey;autoIncrement"`
Shorturl int64 `gorm:"primaryKey;autoIncrement:true"`
Longurl string `gorm:"uniqueIndex:idx_lurl"`
Time time.Time `gorm:"autoCreateTime"`
}

View File

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