diff --git a/handler/user.go b/handler/user.go index f12148c..9eae4a8 100644 --- a/handler/user.go +++ b/handler/user.go @@ -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") diff --git a/model/converter/user.go b/model/converter/user.go index 2f46936..44a87f4 100644 --- a/model/converter/user.go +++ b/model/converter/user.go @@ -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 { @@ -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, + } +} diff --git a/repository/user.go b/repository/user.go index 169958d..2333922 100644 --- a/repository/user.go +++ b/repository/user.go @@ -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 @@ -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 @@ -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 } @@ -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, diff --git a/service/user.go b/service/user.go index de47127..451e941 100644 --- a/service/user.go +++ b/service/user.go @@ -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 +}