Skip to content

Commit

Permalink
Refactor: remove direct gorm.Model dependency in business logic
Browse files Browse the repository at this point in the history
  • Loading branch information
victorzhu30 committed Aug 11, 2024
1 parent 5db3e8f commit 59ff2ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
38 changes: 13 additions & 25 deletions model/converter/user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package converter

import (
"gorm.io/gorm"
"jcourse_go/model/domain"
"jcourse_go/model/dto"
"jcourse_go/model/po"
Expand Down Expand Up @@ -78,38 +77,23 @@ func ConvertToUserProfileDTO(userPO *po.UserPO, userProfilePO *po.UserProfilePO)
}
}

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,
},
func ConvertUpdateUserProfileDTOToUserPO(userProfileDTO *dto.UserProfileDTO, userPO *po.UserPO) po.UserPO {
updatedUserPo := po.UserPO{
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
if userProfileDTO.ID != 0 {
updatedUserPo.ID = userPO.ID
}
return updatedUserPo
}

// 保留一些immutable的属性(
return &po.UserProfilePO{
Model: gorm.Model{
ID: userProfilePO.ID,
CreatedAt: userProfilePO.CreatedAt,
UpdatedAt: time.Now(),
DeletedAt: userProfilePO.DeletedAt,
},
func ConvertUpdateUserProfileDTOToUsrProfilePO(userProfileDTO *dto.UserProfileDTO, userProfilePO *po.UserProfilePO) po.UserProfilePO {
// 保留一些immutable的属性
updatedUserProfilePO := po.UserProfilePO{
UserID: userProfilePO.UserID,
Avatar: userProfileDTO.Avatar,
Department: userProfileDTO.Department,
Expand All @@ -119,4 +103,8 @@ func ConvertUpdateUserProfileDTOToUsrProfilePO(userProfileDTO *dto.UserProfileDT
Grade: userProfileDTO.Grade,
Bio: userProfileDTO.Bio,
}
if updatedUserProfilePO.ID != 0 {
updatedUserProfilePO.ID = userProfilePO.ID
}
return updatedUserProfilePO
}
4 changes: 2 additions & 2 deletions service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func UpdateUserProfileByID(ctx context.Context, userProfileDTO *dto.UserProfileD
}
newUserPO := converter.ConvertUpdateUserProfileDTOToUserPO(userProfileDTO, oldUserPO)

errUpdate := userQuery.UpdateUserByID(ctx, newUserPO)
errUpdate := userQuery.UpdateUserByID(ctx, &newUserPO)
if errUpdate != nil {
return errUpdate
}
Expand All @@ -181,7 +181,7 @@ func UpdateUserProfileByID(ctx context.Context, userProfileDTO *dto.UserProfileD
return errQuery2
}
newUserProfilePO := converter.ConvertUpdateUserProfileDTOToUsrProfilePO(userProfileDTO, oldUserProfilePO)
errUpdate2 := userProfileQuery.UpdateUserProfileByID(ctx, newUserProfilePO)
errUpdate2 := userProfileQuery.UpdateUserProfileByID(ctx, &newUserProfilePO)
if errUpdate2 != nil {
return errUpdate2
}
Expand Down

0 comments on commit 59ff2ba

Please sign in to comment.