Skip to content

Commit

Permalink
Refined/Implemented several handlers in d6d1d1d
Browse files Browse the repository at this point in the history
  • Loading branch information
victorzhu30 committed Aug 6, 2024
1 parent 797e488 commit 30be8cc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 33 deletions.
33 changes: 24 additions & 9 deletions handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -41,21 +41,20 @@ 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: "用户未登录!"})
return
}
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) {
Expand All @@ -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")
Expand Down
64 changes: 40 additions & 24 deletions service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,35 @@ package service
import (
"context"
"jcourse_go/model/dto"
"jcourse_go/model/po"
"jcourse_go/util"

"jcourse_go/model/converter"
"jcourse_go/model/domain"
"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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down

0 comments on commit 30be8cc

Please sign in to comment.