Skip to content

Commit

Permalink
fix: user_repo UpdateColumn to retrieve all fields from updated row
Browse files Browse the repository at this point in the history
  • Loading branch information
bblueberries committed Dec 25, 2023
1 parent 9052084 commit 7d331fe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/controller/user/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (controller *UserController) PatchUserName(ctx *gin.Context) {
return
}
//patch user in db using id,DTO
patchedUser,err := controller.UserUsecase.PatchUser(id,requestDTO)
patchedUser,err := controller.UserUsecase.PatchUserName(id,requestDTO)
if err != nil {
ctx.AbortWithStatusJSON(500, gin.H{
"Message": "Internal server error",
Expand All @@ -91,7 +91,7 @@ func (controller *UserController) PatchUserComplete(ctx *gin.Context) {
return
}
//patch user in db using id,DTO
patchedUser,err := controller.UserUsecase.PatchUser(id,requestDTO)
patchedUser,err := controller.UserUsecase.PatchUserComplete(id,requestDTO)
if err != nil {
ctx.AbortWithStatusJSON(500, gin.H{
"Message": "Internal server error",
Expand Down
3 changes: 2 additions & 1 deletion src/server/repository/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repository
import (
"github.com/esc-chula/gearfest-backend/src/domain"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type UserRepository struct {
Expand Down Expand Up @@ -35,7 +36,7 @@ func (repo *UserRepository) Checkin(checkin *domain.Checkin) error {

// Update column of user in database with column_name and value
func (repo *UserRepository) UpdateColumn(user *domain.User, column_name string, value interface{}) error {
result := repo.db.Model(user).Update(column_name, value)
result := repo.db.Model(user).Clauses(clause.Returning{}).Update(column_name, value)
if result.Error != nil {
return result.Error
}
Expand Down
31 changes: 17 additions & 14 deletions src/usecase/user_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,28 @@ func (usecase *UserUsecase) Post(CheckinDTO domain.CreateCheckinDTO) (domain.Che
return checkin, err
}

func (usecase *UserUsecase) PatchUser(id string,userDTO interface{} ) (domain.User, error) {

func (usecase *UserUsecase) PatchUserComplete(id string,userDTO domain.CreateUserCompletedDTO ) (domain.User, error) {
user := domain.User{
UserID: id,
UserName: "",

}

var err error
switch userDTO := userDTO.(type) {
case domain.CreateUserNameDTO :
{
err = usecase.UserRepository.UpdateColumn(&user,"user_name",userDTO.UserName)
}
case domain.CreateUserCompletedDTO :
{
err = usecase.UserRepository.UpdateColumn(&user,"is_user_completed",userDTO.IsUserCompleted)
}
}
err := usecase.UserRepository.UpdateColumn(&user,"is_user_completed",userDTO.IsUserCompleted)
// if(err != nil) {return user,err}
// err = usecase.UserRepository.GetById(&user, id)
return user, err

}

func (usecase *UserUsecase) PatchUserName(id string,userDTO domain.CreateUserNameDTO ) (domain.User, error) {
user := domain.User{
UserID: id,
IsUserCompleted : false,
}
err := usecase.UserRepository.UpdateColumn(&user,"user_name",userDTO.UserName)
// if(err != nil) {return user,err}
// err = usecase.UserRepository.GetById(&user, id)
return user, err

}

0 comments on commit 7d331fe

Please sign in to comment.