You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@answer.apache.org by li...@apache.org on 2024/04/26 02:26:14 UTC

(incubator-answer) branch feat/1.3.1/staff created (now 8b694e32)

This is an automated email from the ASF dual-hosted git repository.

linkinstar pushed a change to branch feat/1.3.1/staff
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git


      at 8b694e32 feat(user): add the API for get staff

This branch includes the following new commits:

     new 8b694e32 feat(user): add the API for get staff

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(incubator-answer) 01/01: feat(user): add the API for get staff

Posted by li...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

linkinstar pushed a commit to branch feat/1.3.1/staff
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git

commit 8b694e32c43ba2e7b06584c095e9bbbae4be8a5b
Author: LinkinStars <li...@foxmail.com>
AuthorDate: Fri Apr 26 10:25:59 2024 +0800

    feat(user): add the API for get staff
---
 internal/controller/user_controller.go   | 20 ++++++++++++++++++++
 internal/repo/user/user_repo.go          |  8 ++++++--
 internal/router/answer_api_router.go     |  1 +
 internal/schema/user_schema.go           | 16 ++++++++++++++++
 internal/service/content/user_service.go | 20 +++++++++++++++++++-
 internal/service/user_common/user.go     |  5 +++--
 6 files changed, 65 insertions(+), 5 deletions(-)

diff --git a/internal/controller/user_controller.go b/internal/controller/user_controller.go
index f0e70cfe..f6e14323 100644
--- a/internal/controller/user_controller.go
+++ b/internal/controller/user_controller.go
@@ -657,6 +657,26 @@ func (uc *UserController) UserRanking(ctx *gin.Context) {
 	handler.HandleResponse(ctx, err, resp)
 }
 
+// UserStaff get user staff
+// @Summary get user staff
+// @Description get user staff
+// @Tags User
+// @Accept json
+// @Produce json
+// @Security ApiKeyAuth
+// @Param data body schema.GetUserStaffReq true "GetUserStaffReq"
+// @Success 200 {object} handler.RespBody{data=schema.GetUserStaffResp}
+// @Router /answer/api/v1/user/staff [get]
+func (uc *UserController) UserStaff(ctx *gin.Context) {
+	req := &schema.GetUserStaffReq{}
+	if handler.BindAndCheck(ctx, req) {
+		return
+	}
+
+	resp, err := uc.userService.GetUserStaff(ctx, req)
+	handler.HandleResponse(ctx, err, resp)
+}
+
 // UserUnsubscribeNotification unsubscribe notification
 // @Summary unsubscribe notification
 // @Description unsubscribe notification
diff --git a/internal/repo/user/user_repo.go b/internal/repo/user/user_repo.go
index 628394cf..568ebf90 100644
--- a/internal/repo/user/user_repo.go
+++ b/internal/repo/user/user_repo.go
@@ -246,12 +246,16 @@ func (ur *userRepo) GetUserCount(ctx context.Context) (count int64, err error) {
 	return count, nil
 }
 
-func (ur *userRepo) SearchUserListByName(ctx context.Context, name string, limit int) (userList []*entity.User, err error) {
+func (ur *userRepo) SearchUserListByName(ctx context.Context, name string, limit int,
+	onlyStaff bool) (userList []*entity.User, err error) {
 	userList = make([]*entity.User, 0)
 	session := ur.data.DB.Context(ctx)
+	if onlyStaff {
+		session.Join("INNER", "user_role_rel", "`user`.id = `user_role_rel`.user_id AND `user_role_rel`.role_id > 1")
+	}
 	session.Where("status = ?", entity.UserStatusAvailable)
 	session.Where("username LIKE ? OR display_name LIKE ?", strings.ToLower(name)+"%", name+"%")
-	session.OrderBy("username ASC, id DESC")
+	session.OrderBy("username ASC, `user`.id DESC")
 	session.Limit(limit)
 	err = session.Find(&userList)
 	if err != nil {
diff --git a/internal/router/answer_api_router.go b/internal/router/answer_api_router.go
index 1ea2be89..6d50086b 100644
--- a/internal/router/answer_api_router.go
+++ b/internal/router/answer_api_router.go
@@ -146,6 +146,7 @@ func (a *AnswerAPIRouter) RegisterUnAuthAnswerAPIRouter(r *gin.RouterGroup) {
 	// user
 	r.GET("/personal/user/info", a.userController.GetOtherUserInfoByUsername)
 	r.GET("/user/ranking", a.userController.UserRanking)
+	r.GET("/user/staff", a.userController.UserStaff)
 
 	// answer
 	r.GET("/answer/info", a.answerController.Get)
diff --git a/internal/schema/user_schema.go b/internal/schema/user_schema.go
index a95acfc7..8079b904 100644
--- a/internal/schema/user_schema.go
+++ b/internal/schema/user_schema.go
@@ -412,3 +412,19 @@ type UserUnsubscribeNotificationReq struct {
 	Code    string `validate:"required,gt=0,lte=500" json:"code"`
 	Content string `json:"-"`
 }
+
+// GetUserStaffReq get user staff request
+type GetUserStaffReq struct {
+	Username string `validate:"omitempty,gt=0,lte=500" form:"username"`
+	PageSize int    `validate:"omitempty,min=1" form:"page_size"`
+}
+
+// GetUserStaffResp get user staff response
+type GetUserStaffResp struct {
+	// username
+	Username string `json:"username"`
+	// display name
+	DisplayName string `json:"display_name"`
+	// avatar
+	Avatar string `json:"avatar"`
+}
diff --git a/internal/service/content/user_service.go b/internal/service/content/user_service.go
index 4c9843a3..a2b33005 100644
--- a/internal/service/content/user_service.go
+++ b/internal/service/content/user_service.go
@@ -741,6 +741,24 @@ func (us *UserService) UserRanking(ctx context.Context) (resp *schema.UserRankin
 	return us.warpStatRankingResp(userInfoMapping, rankStat, voteStat, userRoleRels), nil
 }
 
+// GetUserStaff get user staff
+func (us *UserService) GetUserStaff(ctx context.Context, req *schema.GetUserStaffReq) (
+	resp []*schema.GetUserStaffResp, err error) {
+	userList, err := us.userRepo.SearchUserListByName(ctx, req.Username, req.PageSize, true)
+	if err != nil {
+		return nil, err
+	}
+	avatarMapping := us.siteInfoService.FormatListAvatar(ctx, userList)
+	for _, u := range userList {
+		resp = append(resp, &schema.GetUserStaffResp{
+			Username:    u.Username,
+			DisplayName: u.DisplayName,
+			Avatar:      avatarMapping[u.ID].GetURL(),
+		})
+	}
+	return resp, nil
+}
+
 // UserUnsubscribeNotification user unsubscribe email notification
 func (us *UserService) UserUnsubscribeNotification(
 	ctx context.Context, req *schema.UserUnsubscribeNotificationReq) (err error) {
@@ -858,7 +876,7 @@ func (us *UserService) SearchUserListByName(ctx context.Context, req *schema.Get
 	if len(req.Username) == 0 {
 		return resp, nil
 	}
-	userList, err := us.userRepo.SearchUserListByName(ctx, req.Username, 5)
+	userList, err := us.userRepo.SearchUserListByName(ctx, req.Username, 5, false)
 	if err != nil {
 		return resp, err
 	}
diff --git a/internal/service/user_common/user.go b/internal/service/user_common/user.go
index 334bcc4e..5e1a4830 100644
--- a/internal/service/user_common/user.go
+++ b/internal/service/user_common/user.go
@@ -21,9 +21,10 @@ package usercommon
 
 import (
 	"context"
+	"strings"
+
 	"github.com/apache/incubator-answer/internal/base/constant"
 	"github.com/apache/incubator-answer/pkg/converter"
-	"strings"
 
 	"github.com/Chain-Zhang/pinyin"
 	"github.com/apache/incubator-answer/internal/base/reason"
@@ -57,7 +58,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, limit int) (userList []*entity.User, err error)
+	SearchUserListByName(ctx context.Context, name string, limit int, onlyStaff bool) (userList []*entity.User, err error)
 }
 
 // UserCommon user service