From 30be8ccab667fd065b8df7f491ec4a7fb33b1e96 Mon Sep 17 00:00:00 2001 From: Victor Zhu <2364305645@qq.com> Date: Tue, 6 Aug 2024 22:45:31 +0800 Subject: [PATCH] Refined/Implemented several handlers in d6d1d1d --- handler/user.go | 33 ++++++++++++++++++------- service/user.go | 64 ++++++++++++++++++++++++++++++------------------- 2 files changed, 64 insertions(+), 33 deletions(-) diff --git a/handler/user.go b/handler/user.go index 002c1f8..f12148c 100644 --- a/handler/user.go +++ b/handler/user.go @@ -14,11 +14,11 @@ import ( func GetSuggestedUserHandler(c *gin.Context) {} func GetUserListHandler(c *gin.Context) { - /* - 管理员权限验证 - */ + + // 管理员权限验证 + var request dto.UserListRequest - if err := c.ShouldBind(&request); err != nil { + if err := c.ShouldBindQuery(&request); err != nil { c.JSON(http.StatusBadRequest, dto.BaseResponse{Message: "参数错误"}) return } @@ -41,7 +41,7 @@ func GetUserListHandler(c *gin.Context) { c.JSON(http.StatusOK, response) } -func GetCurrentUserHandler(c *gin.Context) { +func GetCurrentUserSummaryHandler(c *gin.Context) { userInterface, exists := c.Get(constant.CtxKeyUser) if !exists { c.JSON(http.StatusNotFound, dto.BaseResponse{Message: "用户未登录!"}) @@ -49,13 +49,12 @@ func GetCurrentUserHandler(c *gin.Context) { } user, _ := userInterface.(*domain.User) - me, err := service.GetUserDetailsByID(c, user.ID) + me, err := service.GetUserSummaryByID(c, user.ID) if err != nil { c.JSON(http.StatusNotFound, dto.BaseResponse{Message: "此用户不存在!"}) return } c.JSON(http.StatusOK, me) - return } func GetUserDetailHandler(c *gin.Context) { @@ -66,18 +65,34 @@ func GetUserDetailHandler(c *gin.Context) { return } - userDetail, errDetail := service.GetUserDetailsByID(c, int64(userID)) + userDetail, errDetail := service.GetUserDetailByID(c, int64(userID)) if errDetail != nil { c.JSON(http.StatusNotFound, dto.BaseResponse{Message: "此用户不存在!"}) } c.JSON(http.StatusOK, userDetail) } +func GetCurrentUserProfileHandler(c *gin.Context) { + userInterface, exists := c.Get(constant.CtxKeyUser) + if !exists { + c.JSON(http.StatusNotFound, dto.BaseResponse{Message: "用户未登录!"}) + return + } + + user, _ := userInterface.(*domain.User) + me, err := service.GetUserProfileByID(c, user.ID) + if err != nil { + c.JSON(http.StatusNotFound, dto.BaseResponse{Message: "此用户不存在!"}) + return + } + c.JSON(http.StatusOK, me) +} + func WatchUserHandler(c *gin.Context) {} func UnWatchUserHandler(c *gin.Context) {} -func UpdateUserInfoHandler(c *gin.Context) {} +func UpdateUserProfileHandler(c *gin.Context) {} func GetUserReviewsHandler(c *gin.Context) { userIDStr := c.Param("userID") diff --git a/service/user.go b/service/user.go index 6049701..de47127 100644 --- a/service/user.go +++ b/service/user.go @@ -3,6 +3,7 @@ package service import ( "context" "jcourse_go/model/dto" + "jcourse_go/model/po" "jcourse_go/util" "jcourse_go/model/converter" @@ -10,6 +11,27 @@ import ( "jcourse_go/repository" ) +// 该函数如果放在converter包中会报错import cycle is not allowed +// UserSummaryDTO的组装需要借助service/review.go中的GetReviewCount函数 +func ConvertToUserSummaryDTO(ctx context.Context, userPO *po.UserPO, userProfilePO *po.UserProfilePO) *dto.UserSummaryDTO { + if userPO == nil { + return nil + } + + filter := domain.ReviewFilter{ + UserID: int64(userPO.ID), + } + + total, _ := GetReviewCount(ctx, filter) + + return &dto.UserSummaryDTO{ + ID: int64(userPO.ID), + ReviewCount: total, + TipReceive: 0, + FollowingCourseCount: 0, + } +} + func GetUserByIDs(ctx context.Context, userIDs []int64) (map[int64]domain.User, error) { result := make(map[int64]domain.User) if len(userIDs) == 0 { @@ -51,11 +73,11 @@ func GetUserSummaryByID(ctx context.Context, userID int64) (*dto.UserSummaryDTO, if err != nil { return nil, err } - userSummary := converter.ConvertToUserSummaryDTO(userPO, userProfilePO) + userSummary := ConvertToUserSummaryDTO(ctx, userPO, userProfilePO) return userSummary, nil } -func GetUserDetailsByID(ctx context.Context, userID int64) (*dto.UserDetailsDTO, error) { +func GetUserDetailByID(ctx context.Context, userID int64) (*dto.UserDetailDTO, error) { userQuery := repository.NewUserQuery() userPO, err := userQuery.GetUserByID(ctx, userID) if err != nil { @@ -67,7 +89,7 @@ func GetUserDetailsByID(ctx context.Context, userID int64) (*dto.UserDetailsDTO, if err != nil { return nil, err } - userDetails := converter.ConvertToUserDetailsDTO(userPO, userProfilePO) + userDetails := converter.ConvertToUserDetailDTO(userPO, userProfilePO) return userDetails, nil } @@ -98,7 +120,7 @@ func buildUserDBOptionFromFilter(query repository.IUserQuery, filter domain.User return opts } -func GetUserList(ctx context.Context, filter domain.UserFilter) ([]dto.UserSummaryDTO, error) { +func GetUserList(ctx context.Context, filter domain.UserFilter) ([]dto.UserDetailDTO, error) { userQuery := repository.NewUserQuery() userProfileQuery := repository.NewUserProfileQuery() opts := buildUserDBOptionFromFilter(userQuery, filter) @@ -110,31 +132,25 @@ func GetUserList(ctx context.Context, filter domain.UserFilter) ([]dto.UserSumma if err != nil { return nil, err } - result := make([]dto.UserSummaryDTO, 0) + result := make([]dto.UserDetailDTO, 0) - userProfileMap := make(map[int]string) - for _, profile := range userProfilePOs { - userProfileMap[int(profile.UserID)] = profile.Avatar + userProfileMap := make(map[int]*po.UserProfilePO) + for _, userProfilePO := range userProfilePOs { + userProfileMap[int(userProfilePO.UserID)] = &userProfilePO } for _, userPO := range userPOs { - if avatar, exists := userProfileMap[int(userPO.ID)]; exists { - userSummaryDTO := dto.UserSummaryDTO{ - ID: int64(userPO.ID), - Username: userPO.Username, - Role: userPO.UserRole, - Avatar: avatar, - } - result = append(result, userSummaryDTO) - } else { - userSummaryDTO := dto.UserSummaryDTO{ - ID: int64(userPO.ID), - Username: userPO.Username, - Role: userPO.UserRole, - Avatar: "", - } - result = append(result, userSummaryDTO) + userDetailDTO := dto.UserDetailDTO{ + ID: int64(userPO.ID), + Username: userPO.Username, + Avatar: "", + Bio: "", + } + if userProfilePO, exists := userProfileMap[int(userPO.ID)]; exists { + userDetailDTO.Avatar = userProfilePO.Avatar + userDetailDTO.Bio = userProfilePO.Bio } + result = append(result, userDetailDTO) } return result, nil }