Skip to content

Commit

Permalink
feat: add OPT operation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibz-Dankan committed Sep 21, 2024
1 parent 5c68ec6 commit 50e0200
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
47 changes: 47 additions & 0 deletions internal/models/opt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package models

import (
"github.com/google/uuid"
"gorm.io/gorm"
)

func (u *OTP) BeforeCreate(tx *gorm.DB) error {

uuid := uuid.New().String()
OTP := "should be Random six digit number"

tx.Statement.SetColumn("ID", uuid)
tx.Statement.SetColumn("OPT", OTP)
return nil
}

func (u *OTP) Create(opt OTP) (string, error) {

// Expire all other existing user opts
result := db.Create(&opt)

if result.Error != nil {
return "", result.Error
}
return opt.ID, nil
}

func (u *OTP) FindOne(id string) (OTP, error) {
var opt OTP
db.First(&opt, "id = ?", id)

return opt, nil
}

func (u *OTP) FindByUser(userId string) (OTP, error) {
var otp OTP
db.First(&otp, "userId = ?", userId)

return otp, nil
}

func (u *OTP) ExpireUserOPT(userId string) error {

//TODO: to expire opt here
return nil
}
7 changes: 4 additions & 3 deletions internal/models/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type User struct {
Role string `gorm:"column:role;default:'user';not null" json:"role"`
App []App `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"apps"`
Feedback []Feedback `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"feedbacks"`
OPT []OPT `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"OPT"`
OPT []OTP `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"OPT"`
CreatedAt time.Time `gorm:"column:createdAt" json:"createdAt"`
UpdatedAt time.Time `gorm:"column:updatedAt" json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"column:deletedAt;index" json:"deletedAt"`
Expand Down Expand Up @@ -73,10 +73,11 @@ type Feedback struct {
UpdatedAt time.Time `gorm:"column:updatedAt;index" json:"updatedAt"`
}

type OPT struct {
type OTP struct {
ID string `gorm:"column:id;type:uuid;primaryKey" json:"id"`
UserID string `gorm:"column:userId;not null;index" json:"userId"`
OPT string `gorm:"column:OPT;index" json:"OPT"`
OTP int `gorm:"column:OTP;index" json:"OTP"`
IsUsed bool `gorm:"column:isUsed;default:false" json:"isUsed"`
ExpiresAt time.Time `gorm:"column:expiresAt;not null;index" json:"expiresAt"`
CreatedAt time.Time `gorm:"column:createdAt" json:"createdAt"`
UpdatedAt time.Time `gorm:"column:updatedAt" json:"updatedAt"`
Expand Down

0 comments on commit 50e0200

Please sign in to comment.