Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/patch user api #3

Merged
merged 12 commits into from
Dec 26, 2023
Merged
9 changes: 0 additions & 9 deletions config/config.local.yaml

This file was deleted.

54 changes: 54 additions & 0 deletions src/controller/user/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,57 @@ func (controller *UserController) PostCheckin(ctx *gin.Context) {
}
ctx.JSON(201, newCheckin)
}

func (controller *UserController) PatchUserName(ctx *gin.Context) {

id:= ctx.Param("id")
//convert request into obj
var requestDTO domain.CreateUserNameDTO
err := ctx.ShouldBindJSON(&requestDTO)
if err != nil {
ctx.AbortWithStatusJSON(400, gin.H{
"Message": "Invalid JSON format",
})
return
}
//patch user in db using id,DTO
patchedUser,err := controller.UserUsecase.PatchUserName(id,requestDTO)
if err != nil {
ctx.AbortWithStatusJSON(500, gin.H{
"Message": "Internal server error",
})
return
}
ctx.JSON(200,patchedUser)

}

func (controller *UserController) PatchUserComplete(ctx *gin.Context) {

id:= ctx.Param("id")
//convert request into obj
var requestDTO domain.CreateUserCompletedDTO
err := ctx.ShouldBindJSON(&requestDTO)
if err != nil {
ctx.AbortWithStatusJSON(400, gin.H{
"Message": "Invalid JSON format",
})
return
}
//patch user in db using id,DTO
patchedUser,err := controller.UserUsecase.PatchUserComplete(id,requestDTO)
if err != nil {
ctx.AbortWithStatusJSON(500, gin.H{
"Message": "Internal server error",
})
return
}
ctx.JSON(200,patchedUser)

}






10 changes: 9 additions & 1 deletion src/domain/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type User struct {
UserName string
IsUserCompleted bool
CocktailID uint
Checkins []Checkin
Checkins []Checkin
}

type Checkin struct {
Expand All @@ -18,3 +18,11 @@ type CreateCheckinDTO struct {
UserID string `json:"user_id" binding:"required"`
LocationID uint `json:"location_id" binding:"required"`
}

type CreateUserCompletedDTO struct {
IsUserCompleted bool `json:"is_user_completed" binding:"required"`
CocktailID uint `json:"cocktail_id" binding:"required"`
}
type CreateUserNameDTO struct {
UserName string `json:"user_name" binding:"required"`
}
14 changes: 12 additions & 2 deletions 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 @@ -33,9 +34,18 @@ func (repo *UserRepository) Checkin(checkin *domain.Checkin) error {
return nil
}

// Update column of user in database with column_name and value
// Update a 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
}
return nil
}

// Update some columns of user in database
func (repo *UserRepository) UpdateMultipleColumns(user *domain.User, columns map[string]interface{}) error {
result := repo.db.Model(user).Clauses(clause.Returning{}).Updates(columns)
if result.Error != nil {
return result.Error
}
Expand Down
2 changes: 2 additions & 0 deletions src/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ func loadUserRoutes(g *gin.Engine, db *gorm.DB) {
g.GET("/user/:id", user_controller.GetUser)
//post checkin route
g.POST("/user/checkin", user_controller.PostCheckin)
g.PATCH("user/complete/:id",user_controller.PatchUserComplete)
g.PATCH("user/name/:id",user_controller.PatchUserName)
}
24 changes: 24 additions & 0 deletions src/usecase/user_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type UserRepository interface {
CreateUser(*domain.User) error
Checkin(*domain.Checkin) error
UpdateColumn(*domain.User, string, interface{}) error
UpdateMultipleColumns(*domain.User, map[string]interface{}) error
GetById(*domain.User, string) error
GetWithCheckins(*domain.User, string) error
}
Expand All @@ -33,3 +34,26 @@ func (usecase *UserUsecase) Post(CheckinDTO domain.CreateCheckinDTO) (domain.Che
err := usecase.UserRepository.Checkin(&checkin)
return checkin, err
}

func (usecase *UserUsecase) PatchUserComplete(id string,userDTO domain.CreateUserCompletedDTO ) (domain.User, error) {
user := domain.User{
UserID: id,
}
updatingMap := map[string]interface{}{
"is_user_completed": userDTO.IsUserCompleted,
"cocktail_id": userDTO.CocktailID,
}
err := usecase.UserRepository.UpdateMultipleColumns(&user,updatingMap)
return user, err

}

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

}

Loading