From 59ff2bae5117af0f770381a1bfab61a95ac27a80 Mon Sep 17 00:00:00 2001 From: Victor Zhu <2364305645@qq.com> Date: Sun, 11 Aug 2024 09:09:37 +0800 Subject: [PATCH] Refactor: remove direct gorm.Model dependency in business logic --- model/converter/user.go | 38 +++++++++++++------------------------- service/user.go | 4 ++-- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/model/converter/user.go b/model/converter/user.go index 44a87f4..99cb9ef 100644 --- a/model/converter/user.go +++ b/model/converter/user.go @@ -1,7 +1,6 @@ package converter import ( - "gorm.io/gorm" "jcourse_go/model/domain" "jcourse_go/model/dto" "jcourse_go/model/po" @@ -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, @@ -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 } diff --git a/service/user.go b/service/user.go index 451e941..105b7ad 100644 --- a/service/user.go +++ b/service/user.go @@ -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 } @@ -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 }