switched to gin-gonic
This commit is contained in:
@@ -1,23 +1,27 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"GoMembership/internal/constants"
|
||||
"time"
|
||||
|
||||
"GoMembership/internal/constants"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"gorm.io/gorm/clause"
|
||||
|
||||
"GoMembership/internal/models"
|
||||
"GoMembership/pkg/errors"
|
||||
"gorm.io/gorm/clause"
|
||||
"GoMembership/pkg/logger"
|
||||
)
|
||||
|
||||
type UserRepository interface {
|
||||
CreateUser(user *models.User) (int64, error)
|
||||
UpdateUser(userID int64, user *models.User) error
|
||||
FindUserByID(id int64) (*models.User, error)
|
||||
FindUserByEmail(email string) (*models.User, error)
|
||||
SetVerificationToken(user *models.User, token *string) (int64, error)
|
||||
IsVerified(userID *int64) (bool, error)
|
||||
VerifyUserOfToken(token *string) (int64, error)
|
||||
VerifyUserOfToken(token *string) (*models.User, error)
|
||||
}
|
||||
|
||||
type userRepository struct {
|
||||
@@ -36,11 +40,12 @@ func (repo *userRepository) CreateUser(user *models.User) (int64, error) {
|
||||
return user.ID, nil
|
||||
}
|
||||
|
||||
func (repo *userRepository) UpdateUser(userID int64, updates map[string]interface{}) error {
|
||||
if len(updates) == 0 {
|
||||
func (repo *userRepository) UpdateUser(userID int64, user *models.User) error {
|
||||
logger.Info.Printf("Updating User: %#v\n", user)
|
||||
if user == nil {
|
||||
return errors.ErrNoData
|
||||
}
|
||||
result := repo.db.Session(&gorm.Session{FullSaveAssociations: true}).Model(&models.User{}).Where("id = ?", userID).Updates(&updates)
|
||||
result := repo.db.Session(&gorm.Session{FullSaveAssociations: true}).Updates(&user)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
@@ -54,7 +59,7 @@ func (repo *userRepository) UpdateUser(userID int64, updates map[string]interfac
|
||||
|
||||
func (repo *userRepository) FindUserByID(id int64) (*models.User, error) {
|
||||
var user models.User
|
||||
result := repo.db.First(&user, id)
|
||||
result := repo.db.Preload("Consents").Preload("BankAccount").Preload("Verification").Preload("Membership").First(&user, id)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
@@ -88,38 +93,40 @@ func (repo *userRepository) IsVerified(userID *int64) (bool, error) {
|
||||
return user.Status != constants.UnverifiedStatus, nil
|
||||
}
|
||||
|
||||
func (repo *userRepository) VerifyUserOfToken(token *string) (int64, error) {
|
||||
func (repo *userRepository) VerifyUserOfToken(token *string) (*models.User, error) {
|
||||
var emailVerification models.Verification
|
||||
result := repo.db.Where("verification_token = ?", token).First(&emailVerification)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return 0, gorm.ErrRecordNotFound
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
return 0, result.Error
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
// Check if the user is already verified
|
||||
verified, err := repo.IsVerified(&emailVerification.UserID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return nil, err
|
||||
}
|
||||
user, err := repo.FindUserByID(emailVerification.UserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if verified {
|
||||
return emailVerification.UserID, gorm.ErrRecordNotFound
|
||||
return user, errors.ErrAlreadyVerified
|
||||
}
|
||||
|
||||
// Update user status to active
|
||||
t := time.Now()
|
||||
emailVerification.EmailVerifiedAt = &t
|
||||
update := map[string]interface{}{
|
||||
"status": constants.VerifiedStatus,
|
||||
"verifications": emailVerification,
|
||||
}
|
||||
err = repo.UpdateUser(emailVerification.UserID, update)
|
||||
user.Status = constants.VerifiedStatus
|
||||
user.Verification = emailVerification
|
||||
|
||||
err = repo.UpdateUser(emailVerification.UserID, user)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return emailVerification.UserID, nil
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (repo *userRepository) SetVerificationToken(user *models.User, token *string) (int64, error) {
|
||||
|
||||
Reference in New Issue
Block a user