43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
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 string) error {
|
|
return r.db.Delete(&models.EmailVerificationToken{}, id).Error
|
|
}
|
|
|
|
func (r *EmailVerificationTokenRepo) DeleteByUserID(userID string) error {
|
|
return r.db.Where("user_id = ?", userID).Delete(&models.EmailVerificationToken{}).Error
|
|
}
|