-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/patch user api
- Loading branch information
Showing
10 changed files
with
265 additions
and
162 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.