refactoring
This commit is contained in:
@@ -1,15 +1,48 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"GoMembership/pkg/logger"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Verification struct {
|
||||
UpdatedAt time.Time
|
||||
CreatedAt time.Time
|
||||
gorm.Model
|
||||
VerifiedAt *time.Time `json:"verified_at"`
|
||||
VerificationToken string `json:"token"`
|
||||
ID uint `gorm:"primaryKey"`
|
||||
UserID uint `json:"user_id"`
|
||||
Type string
|
||||
}
|
||||
|
||||
func (v *Verification) Create(db *gorm.DB) error {
|
||||
if err := db.Create(v).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
logger.Info.Printf("verification created: %#v", v)
|
||||
// Preload all associations to return the fully populated object
|
||||
return db.First(v, v.ID).Error // Refresh the verification object with all associations
|
||||
}
|
||||
|
||||
func (v *Verification) Update(db *gorm.DB) error {
|
||||
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
// Check if the user exists in the database
|
||||
var existingVerification Verification
|
||||
|
||||
logger.Info.Printf("updating verification: %#v", v)
|
||||
if err := tx.First(&existingVerification, v.ID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Model(&existingVerification).Updates(v).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.First(v, v.ID).Error
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func (v *Verification) Delete(db *gorm.DB) error {
|
||||
return db.Delete(&v).Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user