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
55 changes: 0 additions & 55 deletions src/controller/user/user_controller.go

This file was deleted.

115 changes: 115 additions & 0 deletions src/controllers/user_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package controllers

import (
"github.com/esc-chula/gearfest-backend/src/domains"
"github.com/esc-chula/gearfest-backend/src/usecases"
"github.com/gin-gonic/gin"
)

type UserController struct {
UserUsecases usecases.UserUsecases
}

func NewUserController(repository usecases.UserRepository) *UserController {
return &UserController{
UserUsecases: usecases.UserUsecases{
UserRepository: repository,
},
}
}

func (controller *UserController) GetUser(ctx *gin.Context) {
id := ctx.Param("id")
user, err := controller.UserUsecases.Get(id)
if err != nil {
ctx.AbortWithStatusJSON(400, gin.H{
"Message": "Bad request",
})
return
}
ctx.JSON(200, user)
}

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

//convert request into obj
var CheckinDTO domains.CreateCheckinDTO
err := ctx.ShouldBindJSON(&CheckinDTO)
if err != nil {
ctx.AbortWithStatusJSON(400, gin.H{
"Message": "Invalid JSON format",
})
return
}
//post the obj to db using userId,LocationId (checkInId auto gen)
newCheckin, err := controller.UserUsecases.Post(CheckinDTO)

if err != nil {
ctx.AbortWithStatusJSON(500, gin.H{
"Message": "Internal server error",
})

return
}
ctx.JSON(201, newCheckin)
}

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

id := ctx.Param("id")
//convert request into obj
var requestDTO domains.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.UserUsecases.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")
isUserCompleted, err := controller.UserUsecases.IsUserCompleted(id)
if err != nil {
ctx.AbortWithStatusJSON(404, gin.H{
"Message": "User not found",
})
return
} else if isUserCompleted {
ctx.AbortWithStatusJSON(403, gin.H{
"Message": "User has already completed",
})
return
}
//convert request into obj
var requestDTO domains.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.UserUsecases.PatchUserComplete(id, requestDTO)
if err != nil {
ctx.AbortWithStatusJSON(500, gin.H{
"Message": "Internal server error",
})
return
}
ctx.JSON(200, patchedUser)

}
9 changes: 8 additions & 1 deletion src/domain/user.go → src/domains/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package domain
package domains

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

type CreateUserCompletedDTO struct {
CocktailID uint `json:"cocktail_id" binding:"required"`
}
type CreateUserNameDTO struct {
UserName string `json:"user_name" binding:"required"`
}
File renamed without changes.
72 changes: 72 additions & 0 deletions src/server/repositories/user_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package repositories

import (
"github.com/esc-chula/gearfest-backend/src/domains"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type UserRepository struct {
db *gorm.DB
}

func NewUserRepository(db *gorm.DB) *UserRepository {
return &UserRepository{
db: db,
}
}

// Create user in database
func (repo *UserRepository) CreateUser(user *domains.User) error {
result := repo.db.Create(user)
if result.Error != nil {
return result.Error
}
return nil
}

// Create checkin
func (repo *UserRepository) Checkin(checkin *domains.Checkin) error {
result := repo.db.Create(checkin)
if result.Error != nil {
return result.Error
}
return nil
}

// Get the chosen field from user
func (repo *UserRepository) GetField(id string, field string) (bool, error) {
user := domains.User{}
result := repo.db.Select(field).First(&user, "user_id = ?", id)
if result.Error != nil {
return false, result.Error
}
return user.IsUserCompleted, nil
}

// Update a column of user in database with column_name and value
func (repo *UserRepository) UpdateField(user *domains.User, id string, field string, value interface{}) error {
result := repo.db.Model(&domains.User{}).Where("user_id = ?", id).Update(field, value)
if result.Error != nil {
return result.Error
}
return repo.GetById(user, id)
}

// Update some columns of user in database
func (repo *UserRepository) UpdateFields(user *domains.User, id string, fields map[string]interface{}) error {
result := repo.db.Model(&domains.User{}).Where("user_id = ? ", id).Updates(fields)
if result.Error != nil {
return result.Error
}
return repo.GetById(user, id)
}

// Get user by id
func (repo *UserRepository) GetById(user *domains.User, id string) error {
result := repo.db.Model(&domains.User{}).Preload(clause.Associations).First(user, "user_id = ?", id)
if result.Error != nil {
return result.Error
}
return nil
}
61 changes: 0 additions & 61 deletions src/server/repository/user_repository.go

This file was deleted.

20 changes: 11 additions & 9 deletions src/server/routes.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package server

import (
controller "github.com/esc-chula/gearfest-backend/src/controller/user"
"github.com/esc-chula/gearfest-backend/src/server/repository"
"github.com/esc-chula/gearfest-backend/src/controllers"
"github.com/esc-chula/gearfest-backend/src/server/repositories"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

func loadRoutes(db *gorm.DB) *gin.Engine {
g := gin.New()
loadUserRoutes(g, db)
userRoutes := g.Group("/user")
loadUserRoutes(userRoutes, db)
return g
}

func loadUserRoutes(g *gin.Engine, db *gorm.DB) {
UserRepository := repository.NewUserRepository(db)
user_controller := controller.NewUserController(UserRepository)
g.GET("/user/:id", user_controller.GetUser)
//post checkin route
g.POST("/user/checkin", user_controller.PostCheckin)
func loadUserRoutes(g *gin.RouterGroup, db *gorm.DB) {
UserRepository := repositories.NewUserRepository(db)
UserController := controllers.NewUserController(UserRepository)
g.GET("/:id", UserController.GetUser)
g.POST("/checkin", UserController.PostCheckin)
g.PATCH("/complete/:id", UserController.PatchUserComplete)
g.PATCH("/name/:id", UserController.PatchUserName)
}
35 changes: 0 additions & 35 deletions src/usecase/user_usecase.go

This file was deleted.

Loading
Loading