Add count increment and save in GetURL method

This commit is contained in:
Aykhan Shahsuvarov 2024-02-28 17:51:27 +04:00
parent 7a771dba79
commit 248d1cf117

View File

@ -1,7 +1,6 @@
package db package db
import ( import (
// "github.com/aykhans/oh-my-url/app/config"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -13,6 +12,7 @@ type url struct {
ID uint `gorm:"primaryKey"` ID uint `gorm:"primaryKey"`
Key string `gorm:"unique;not null;size:15;default:null"` Key string `gorm:"unique;not null;size:15;default:null"`
URL string `gorm:"not null;default:null"` URL string `gorm:"not null;default:null"`
Count int `gorm:"not null;default:0"`
} }
func (p *Postgres) Init() { func (p *Postgres) Init() {
@ -41,6 +41,8 @@ func (p *Postgres) GetURL(key string) (string, error) {
if tx.Error != nil { if tx.Error != nil {
return "", tx.Error return "", tx.Error
} }
result.Count++
p.gormDB.Save(&result)
return result.URL, nil return result.URL, nil
} }