Skip to content

Commit

Permalink
feat: auto lower email
Browse files Browse the repository at this point in the history
  • Loading branch information
taciturnaxolotl committed Nov 9, 2024
1 parent 2898f66 commit 7daae22
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions repositories/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repositories
import (
"errors"
"fmt"
"strings"
"time"

"github.com/duke-git/lancet/v2/condition"
Expand All @@ -21,6 +22,9 @@ func NewUserRepository(db *gorm.DB) *UserRepository {
}

func (r *UserRepository) FindOne(attributes models.User) (*models.User, error) {
if attributes.Email != "" {
attributes.Email = strings.ToLower(attributes.Email)
}
u := &models.User{}
if err := r.db.Where(&attributes).First(u).Error; err != nil {
return u, err
Expand Down Expand Up @@ -114,6 +118,9 @@ func (r *UserRepository) Count() (int64, error) {
}

func (r *UserRepository) InsertOrGet(user *models.User) (*models.User, bool, error) {
if user.Email != "" {
user.Email = strings.ToLower(user.Email)
}
if u, err := r.FindOne(models.User{ID: user.ID}); err == nil && u != nil && u.ID != "" {
return u, false, nil
}
Expand Down Expand Up @@ -164,6 +171,11 @@ func (r *UserRepository) Update(user *models.User) (*models.User, error) {
}

func (r *UserRepository) UpdateField(user *models.User, key string, value interface{}) (*models.User, error) {
// lower user params and if its an update to user or email then properly case
if key == "email" {
value = strings.ToLower(value.(string))
}

result := r.db.Model(user).Update(key, value)
if err := result.Error; err != nil {
return nil, err
Expand Down

0 comments on commit 7daae22

Please sign in to comment.