Skip to content

Commit

Permalink
fix: add where for get user by name
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 17, 2022
1 parent 355db3a commit ca13678
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions internal/store/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/pkg/errors"
)

var userCache = cache.NewMemCache(cache.WithShards[*model.User](4))
var userCache = cache.NewMemCache(cache.WithShards[*model.User](2))
var userG singleflight.Group[*model.User]

func ExistAdmin() bool {
Expand All @@ -25,7 +25,7 @@ func GetUserByName(name string) (*model.User, error) {
}
user, err, _ := userG.Do(name, func() (*model.User, error) {
user := model.User{Name: name}
if err := db.First(&user).Error; err != nil {
if err := db.Where(user).First(&user).Error; err != nil {
return nil, errors.Wrapf(err, "failed select user")
}
userCache.Set(name, &user)
Expand Down Expand Up @@ -56,13 +56,13 @@ func UpdateUser(u *model.User) error {
}

func GetUsers(pageIndex, pageSize int) ([]model.User, int64, error) {
userDb := db.Model(&model.User{})
userDB := db.Model(&model.User{})
var count int64
if err := userDb.Count(&count).Error; err != nil {
if err := userDB.Count(&count).Error; err != nil {
return nil, 0, errors.Wrapf(err, "failed get users count")
}
var users []model.User
if err := userDb.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&users).Error; err != nil {
if err := userDB.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&users).Error; err != nil {
return nil, 0, errors.Wrapf(err, "failed get find users")
}
return users, count, nil
Expand Down

0 comments on commit ca13678

Please sign in to comment.