Skip to content

Commit

Permalink
Merge pull request #176 from Tibz-Dankan/feat/admin
Browse files Browse the repository at this point in the history
Feat/admin
  • Loading branch information
Tibz-Dankan authored Sep 21, 2024
2 parents e853924 + 50e0200 commit 30a1320
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 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
}
11 changes: 11 additions & 0 deletions internal/models/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +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 []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 @@ -71,3 +72,13 @@ type Feedback struct {
CreatedAt time.Time `gorm:"column:createdAt;index" json:"createdAt"`
UpdatedAt time.Time `gorm:"column:updatedAt;index" json:"updatedAt"`
}

type OTP struct {
ID string `gorm:"column:id;type:uuid;primaryKey" json:"id"`
UserID string `gorm:"column:userId;not null;index" json:"userId"`
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"`
}

0 comments on commit 30a1320

Please sign in to comment.