A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
refactor(user): refactor search user by name
LinkinStarsb01d965f8003
diff --git a/internal/repo/question/question_repo.go b/internal/repo/question/question_repo.goindex 6623ae90..7f1c8b28 100644--- a/internal/repo/question/question_repo.go+++ b/internal/repo/question/question_repo.go@@ -225,7 +225,7 @@ func (qr *questionRepo) GetQuestionList(ctx context.Context, question *entity.Qufunc (qr *questionRepo) GetQuestionCount(ctx context.Context) (count int64, err error) {session := qr.data.DB.Context(ctx)- session.Where("status = ? OR status = ?", entity.QuestionStatusAvailable, entity.QuestionStatusClosed)+ session.In("status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed})count, err = session.Count(&entity.Question{Show: entity.QuestionShow})if err != nil {return 0, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()@@ -234,17 +234,9 @@ func (qr *questionRepo) GetQuestionCount(ctx context.Context) (count int64, err}func (qr *questionRepo) GetUserQuestionCount(ctx context.Context, userID string) (count int64, err error) {- questionList := make([]*entity.Question, 0)- count, err = qr.data.DB.In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}).And("question.user_id = ?", userID).FindAndCount(&questionList)- if err != nil {- return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()- }- return-}--func (qr *questionRepo) GetQuestionCountByIDs(ctx context.Context, ids []string) (count int64, err error) {- questionList := make([]*entity.Question, 0)- count, err = qr.data.DB.In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}).In("id = ?", ids).Count(&questionList)+ session := qr.data.DB.Context(ctx)+ session.In("status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed})+ count, err = session.Count(&entity.Question{UserID: userID})if err != nil {return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()}diff --git a/internal/repo/user/user_repo.go b/internal/repo/user/user_repo.goindex 9bf2f2ea..e3b35c15 100644--- a/internal/repo/user/user_repo.go+++ b/internal/repo/user/user_repo.go@@ -2,6 +2,7 @@ package userimport ("context"+ "strings""time""github.com/answerdev/answer/internal/base/data"@@ -224,18 +225,16 @@ func (ur *userRepo) GetUserCount(ctx context.Context) (count int64, err error) {return}-func (ur *userRepo) SearchUserListByName(ctx context.Context, name string) (userList []*entity.User, err error) {+func (ur *userRepo) SearchUserListByName(ctx context.Context, name string, limit int) (userList []*entity.User, err error) {userList = make([]*entity.User, 0)- if name == "" {- return userList, nil- }- session := ur.data.DB.Where("")- session.Where("username LIKE LOWER(?) or display_name LIKE ?", name+"%", name+"%").And("status =?", entity.UserStatusAvailable)- session.Asc("username")- session = session.Limit(5, 0)- err = session.OrderBy("id desc").Find(&userList)+ session := ur.data.DB.Context(ctx)+ session.Where("status = ?", entity.UserStatusAvailable)+ session.Where("username LIKE ? OR display_name LIKE ?", strings.ToLower(name)+"%", name+"%")+ session.OrderBy("username ASC, id DESC")+ session.Limit(limit)+ err = session.Find(&userList)if err != nil {- err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()+ return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()}tryToDecorateUserListFromUserCenter(ctx, ur.data, userList)returndiff --git a/internal/service/question_common/question.go b/internal/service/question_common/question.goindex dee28b5b..84a6c880 100644--- a/internal/service/question_common/question.go+++ b/internal/service/question_common/question.go@@ -50,7 +50,6 @@ type QuestionRepo interface {AdminQuestionPage(ctx context.Context, search *schema.AdminQuestionPageReq) ([]*entity.Question, int64, error)GetQuestionCount(ctx context.Context) (count int64, err error)GetUserQuestionCount(ctx context.Context, userID string) (count int64, err error)- GetQuestionCountByIDs(ctx context.Context, ids []string) (count int64, err error)SitemapQuestions(ctx context.Context, page, pageSize int) (questionIDList []*schema.SiteMapQuestionInfo, err error)}diff --git a/internal/service/user_common/user.go b/internal/service/user_common/user.goindex 84a13f3e..4a452584 100644--- a/internal/service/user_common/user.go+++ b/internal/service/user_common/user.go@@ -36,7 +36,7 @@ type UserRepo interface {GetByUsernames(ctx context.Context, usernames []string) ([]*entity.User, error)GetByEmail(ctx context.Context, email string) (userInfo *entity.User, exist bool, err error)GetUserCount(ctx context.Context) (count int64, err error)- SearchUserListByName(ctx context.Context, name string) (userList []*entity.User, err error)+ SearchUserListByName(ctx context.Context, name string, limit int) (userList []*entity.User, err error)}// UserCommon user servicediff --git a/internal/service/user_service.go b/internal/service/user_service.goindex 582a4cb1..17d52da3 100644--- a/internal/service/user_service.go+++ b/internal/service/user_service.go@@ -817,22 +817,26 @@ func (us *UserService) getUserInfoMapping(ctx context.Context, userIDs []string)return userInfoMapping, nil}-func (us *UserService) SearchUserListByName(ctx context.Context, input *schema.GetOtherUserInfoByUsernameReq) ([]*schema.UserBasicInfo, error) {- userinfolist := make([]*schema.UserBasicInfo, 0)- list, err := us.userRepo.SearchUserListByName(ctx, input.Username)- if err != nil {- return userinfolist, err- }- avatarMapping := us.siteInfoService.FormatListAvatar(ctx, list)- for _, user := range list {- if input.UserID != user.ID {- userinfo := us.userCommonService.FormatUserBasicInfo(ctx, user)- userinfo.Avatar = avatarMapping[user.ID].GetURL()- userinfolist = append(userinfolist, userinfo)+func (us *UserService) SearchUserListByName(ctx context.Context, req *schema.GetOtherUserInfoByUsernameReq) (+ resp []*schema.UserBasicInfo, err error) {+ resp = make([]*schema.UserBasicInfo, 0)+ if len(req.Username) == 0 {+ return resp, nil+ }+ userList, err := us.userRepo.SearchUserListByName(ctx, req.Username, 5)+ if err != nil {+ return resp, err+ }+ avatarMapping := us.siteInfoService.FormatListAvatar(ctx, userList)+ for _, u := range userList {+ if req.UserID == u.ID {+ continue}-+ basicInfo := us.userCommonService.FormatUserBasicInfo(ctx, u)+ basicInfo.Avatar = avatarMapping[u.ID].GetURL()+ resp = append(resp, basicInfo)}- return userinfolist, nil+ return resp, nil}func (us *UserService) warpStatRankingResp(