From ca13678105ed346951f3f3bf60b6e6f0e752f96b Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Thu, 16 Jun 2022 21:59:17 +0800 Subject: [PATCH] fix: add where for get user by name --- internal/store/user.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/store/user.go b/internal/store/user.go index 148b80e4b7d..01efc00282b 100644 --- a/internal/store/user.go +++ b/internal/store/user.go @@ -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 { @@ -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) @@ -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