prompt 6
This commit is contained in:
42
internal/repo/email_verification_token_repo.go
Normal file
42
internal/repo/email_verification_token_repo.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"trustcontact/internal/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type EmailVerificationTokenRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewEmailVerificationTokenRepo(db *gorm.DB) *EmailVerificationTokenRepo {
|
||||
return &EmailVerificationTokenRepo{db: db}
|
||||
}
|
||||
|
||||
func (r *EmailVerificationTokenRepo) Create(token *models.EmailVerificationToken) error {
|
||||
return r.db.Create(token).Error
|
||||
}
|
||||
|
||||
func (r *EmailVerificationTokenRepo) FindValidByHash(tokenHash string, now time.Time) (*models.EmailVerificationToken, error) {
|
||||
var token models.EmailVerificationToken
|
||||
err := r.db.Where("token_hash = ? AND expires_at > ?", tokenHash, now).First(&token).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &token, nil
|
||||
}
|
||||
|
||||
func (r *EmailVerificationTokenRepo) DeleteByID(id uint) error {
|
||||
return r.db.Delete(&models.EmailVerificationToken{}, id).Error
|
||||
}
|
||||
|
||||
func (r *EmailVerificationTokenRepo) DeleteByUserID(userID uint) error {
|
||||
return r.db.Where("user_id = ?", userID).Delete(&models.EmailVerificationToken{}).Error
|
||||
}
|
||||
Reference in New Issue
Block a user