Skip to content

Commit

Permalink
Implement UpdateUserProfileHandler in handler/user.go and add Update …
Browse files Browse the repository at this point in the history
…feature to repository/user.go
  • Loading branch information
victorzhu30 committed Aug 7, 2024
1 parent 30be8cc commit 5261502
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
14 changes: 13 additions & 1 deletion handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,19 @@ func WatchUserHandler(c *gin.Context) {}

func UnWatchUserHandler(c *gin.Context) {}

func UpdateUserProfileHandler(c *gin.Context) {}
func UpdateUserProfileHandler(c *gin.Context) {
var request dto.UserProfileDTO
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, dto.BaseResponse{Message: "参数错误"})
return
}
err := service.UpdateUserProfileByID(c, &request)
if err != nil {
c.JSON(http.StatusInternalServerError, dto.BaseResponse{Message: "用户信息更新失败。"})
return
}
c.JSON(http.StatusOK, dto.BaseResponse{Message: "用户信息更新成功。"})
}

func GetUserReviewsHandler(c *gin.Context) {
userIDStr := c.Param("userID")
Expand Down
45 changes: 45 additions & 0 deletions model/converter/user.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package converter

import (
"gorm.io/gorm"
"jcourse_go/model/domain"
"jcourse_go/model/dto"
"jcourse_go/model/po"
"time"
)

func ConvertUserPOToDomain(userPO po.UserPO) domain.User {
Expand Down Expand Up @@ -75,3 +77,46 @@ func ConvertToUserProfileDTO(userPO *po.UserPO, userProfilePO *po.UserProfilePO)
Grade: userProfilePO.Grade,
}
}

func ConvertUpdateUserProfileDTOToUserPO(userProfileDTO *dto.UserProfileDTO, userPO *po.UserPO) *po.UserPO {
if userProfileDTO == nil {
return nil
}
return &po.UserPO{
Model: gorm.Model{
ID: userPO.ID,
CreatedAt: userPO.CreatedAt,
UpdatedAt: time.Now(),
DeletedAt: userPO.DeletedAt,
},
Username: userProfileDTO.Username,
Email: userPO.Email,
Password: userPO.Password,
UserRole: userPO.UserRole,
LastSeenAt: time.Now(),
}
}

func ConvertUpdateUserProfileDTOToUsrProfilePO(userProfileDTO *dto.UserProfileDTO, userProfilePO *po.UserProfilePO) *po.UserProfilePO {
if userProfileDTO == nil {
return nil
}

// 保留一些immutable的属性(
return &po.UserProfilePO{
Model: gorm.Model{
ID: userProfilePO.ID,
CreatedAt: userProfilePO.CreatedAt,
UpdatedAt: time.Now(),
DeletedAt: userProfilePO.DeletedAt,
},
UserID: userProfilePO.UserID,
Avatar: userProfileDTO.Avatar,
Department: userProfileDTO.Department,
Type: userProfilePO.Type,
Major: userProfileDTO.Major,
Degree: userProfilePO.Degree,
Grade: userProfileDTO.Grade,
Bio: userProfileDTO.Bio,
}
}
12 changes: 12 additions & 0 deletions repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type IUserQuery interface {
GetUserCount(ctx context.Context, opts ...DBOption) (int64, error)
GetUserByID(ctx context.Context, userID int64) (*po.UserPO, error)
GetUserByIDs(ctx context.Context, userIDs []int64) (map[int64]po.UserPO, error)
UpdateUserByID(ctx context.Context, user *po.UserPO) error
WithID(id int64) DBOption
WithEmail(email string) DBOption
WithPassword(password string) DBOption
Expand All @@ -33,6 +34,7 @@ type IUserProfileQuery interface {
GetUserProfileByID(ctx context.Context, userID int64) (*po.UserProfilePO, error)
GetUserProfileList(ctx context.Context, opts ...DBOption) ([]po.UserProfilePO, error)
GetUserProfileCount(ctx context.Context, opts ...DBOption) (int64, error)
UpdateUserProfileByID(ctx context.Context, userProfile *po.UserProfilePO) error
WithUserID(id int64) DBOption
WithLimit(limit int64) DBOption
WithOffset(offset int64) DBOption
Expand Down Expand Up @@ -127,6 +129,11 @@ func (q *UserProfileQuery) GetUserProfileCount(ctx context.Context, opts ...DBOp
return count, nil
}

func (q *UserProfileQuery) UpdateUserProfileByID(ctx context.Context, userProfile *po.UserProfilePO) error {
result := q.optionDB(ctx, q.WithUserID(userProfile.UserID)).Save(userProfile).Error
return result
}

type UserQuery struct {
db *gorm.DB
}
Expand Down Expand Up @@ -214,6 +221,11 @@ func (q *UserQuery) GetUserCount(ctx context.Context, opts ...DBOption) (int64,
return count, nil
}

func (q *UserQuery) UpdateUserByID(ctx context.Context, user *po.UserPO) error {
result := q.optionDB(ctx, q.WithID(int64(user.ID))).Save(user).Error
return result
}

func (q *UserQuery) CreateUser(ctx context.Context, email string, passwordStore string) (*po.UserPO, error) {
user := po.UserPO{
Username: email,
Expand Down
26 changes: 26 additions & 0 deletions service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,29 @@ func GetUserCount(ctx context.Context, filter domain.UserFilter) (int64, error)
opts := buildUserDBOptionFromFilter(userQuery, filter)
return userQuery.GetUserCount(ctx, opts...)
}

func UpdateUserProfileByID(ctx context.Context, userProfileDTO *dto.UserProfileDTO) error {
userQuery := repository.NewUserQuery()
oldUserPO, errQuery := userQuery.GetUserByID(ctx, userProfileDTO.UserID)
if errQuery != nil {
return errQuery
}
newUserPO := converter.ConvertUpdateUserProfileDTOToUserPO(userProfileDTO, oldUserPO)

errUpdate := userQuery.UpdateUserByID(ctx, newUserPO)
if errUpdate != nil {
return errUpdate
}

userProfileQuery := repository.NewUserProfileQuery()
oldUserProfilePO, errQuery2 := userProfileQuery.GetUserProfileByID(ctx, userProfileDTO.UserID)
if errQuery2 != nil {
return errQuery2
}
newUserProfilePO := converter.ConvertUpdateUserProfileDTOToUsrProfilePO(userProfileDTO, oldUserProfilePO)
errUpdate2 := userProfileQuery.UpdateUserProfileByID(ctx, newUserProfilePO)
if errUpdate2 != nil {
return errUpdate2
}
return nil
}

0 comments on commit 5261502

Please sign in to comment.