add missing path property handling
This commit is contained in:
@@ -11,7 +11,14 @@ import (
|
||||
)
|
||||
|
||||
func (hc *handlersContext) getAllCages(c *gin.Context) {
|
||||
cages, err := hc.db.GetAllCages()
|
||||
gameID := c.Param("gameID")
|
||||
err := hc.validator.VarCtx(c, gameID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
cages, err := hc.db.GetAllCages(gameID)
|
||||
if err != nil {
|
||||
utils.GetLoggerFromCtx(c).WithError(err).Error("error while getting cages")
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrInternalServer, "Error while getting cages")
|
||||
@@ -21,7 +28,8 @@ func (hc *handlersContext) getAllCages(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (hc *handlersContext) getACage(c *gin.Context) {
|
||||
cageID := c.Param("cageId")
|
||||
cageID := c.Param("cageID")
|
||||
gameID := c.Param("gameID")
|
||||
|
||||
err := hc.validator.VarCtx(c, cageID, "uuid4")
|
||||
if err != nil {
|
||||
@@ -29,8 +37,14 @@ func (hc *handlersContext) getACage(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
cage, err := hc.db.GetCageById(cageID)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
err = hc.validator.VarCtx(c, gameID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
cage, err := hc.db.GetCageByID(gameID, cageID)
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Cage not found")
|
||||
@@ -55,6 +69,13 @@ func (hc *handlersContext) getACage(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (hc *handlersContext) createACage(c *gin.Context) {
|
||||
gameID := c.Param("gameID")
|
||||
err := hc.validator.VarCtx(c, gameID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
b, err := c.GetRawData()
|
||||
if err != nil {
|
||||
utils.GetLoggerFromCtx(c).WithError(err).Error("error while creating cage, read data fail")
|
||||
@@ -75,8 +96,9 @@ func (hc *handlersContext) createACage(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
cageToCreate.Game = model.Game{ID: gameID}
|
||||
err = hc.db.CreateCage(&cageToCreate)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeDuplicate:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrAlreadyExists, "Cage already exists")
|
||||
@@ -100,8 +122,8 @@ func (hc *handlersContext) updateACage(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (hc *handlersContext) deleteACage(c *gin.Context) {
|
||||
|
||||
cageID := c.Param("cageId")
|
||||
gameID := c.Param("gameID")
|
||||
cageID := c.Param("cageID")
|
||||
|
||||
err := hc.validator.VarCtx(c, cageID, "uuid4")
|
||||
if err != nil {
|
||||
@@ -109,9 +131,15 @@ func (hc *handlersContext) deleteACage(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
err = hc.validator.VarCtx(c, gameID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
// check cage id given in URL exists
|
||||
_, err = hc.db.GetCageById(cageID)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
_, err = hc.db.GetCageByID(gameID, cageID)
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Cage to delete not found")
|
||||
@@ -128,7 +156,7 @@ func (hc *handlersContext) deleteACage(c *gin.Context) {
|
||||
}
|
||||
|
||||
err = hc.db.DeleteCage(cageID)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Cage to delete not found")
|
||||
|
||||
@@ -21,16 +21,16 @@ func (hc *handlersContext) getAllGames(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (hc *handlersContext) getAGame(c *gin.Context) {
|
||||
gameId := c.Param("gameId")
|
||||
gameID := c.Param("gameID")
|
||||
|
||||
err := hc.validator.VarCtx(c, gameId, "uuid4")
|
||||
err := hc.validator.VarCtx(c, gameID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
game, err := hc.gameService.GetAGameById(gameId)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
game, err := hc.gameService.GetAGameByID(gameID)
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Game not found")
|
||||
@@ -76,7 +76,7 @@ func (hc *handlersContext) createAGame(c *gin.Context) {
|
||||
}
|
||||
|
||||
err = hc.db.CreateGame(&gameToCreate)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeDuplicate:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrAlreadyExists, "Game already exists")
|
||||
@@ -101,7 +101,7 @@ func (hc *handlersContext) updateAGame(c *gin.Context) {
|
||||
|
||||
func (hc *handlersContext) deleteAGame(c *gin.Context) {
|
||||
|
||||
gameID := c.Param("gameId")
|
||||
gameID := c.Param("gameID")
|
||||
|
||||
err := hc.validator.VarCtx(c, gameID, "uuid4")
|
||||
if err != nil {
|
||||
@@ -110,8 +110,8 @@ func (hc *handlersContext) deleteAGame(c *gin.Context) {
|
||||
}
|
||||
|
||||
// check game id given in URL exists
|
||||
_, err = hc.db.GetGameById(gameID)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
_, err = hc.db.GetGameByID(gameID)
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Game to delete not found")
|
||||
@@ -128,7 +128,7 @@ func (hc *handlersContext) deleteAGame(c *gin.Context) {
|
||||
}
|
||||
|
||||
err = hc.db.DeleteGame(gameID)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Game to delete not found")
|
||||
@@ -148,5 +148,20 @@ func (hc *handlersContext) deleteAGame(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (hc *handlersContext) getHamstersOfGame(c *gin.Context) {
|
||||
gameID := c.Param("gameID")
|
||||
|
||||
err := hc.validator.VarCtx(c, gameID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
games, err := hc.db.GetHamstersOfGame(gameID)
|
||||
if err != nil {
|
||||
utils.GetLoggerFromCtx(c).WithError(err).Error("error while getting games")
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrInternalServer, "Error while getting games")
|
||||
return
|
||||
}
|
||||
utils.JSON(c.Writer, http.StatusOK, games)
|
||||
|
||||
}
|
||||
|
||||
@@ -11,7 +11,22 @@ import (
|
||||
)
|
||||
|
||||
func (hc *handlersContext) getAllHamsters(c *gin.Context) {
|
||||
hamsters, err := hc.db.GetAllHamsters()
|
||||
gameID := c.Param("gameID")
|
||||
cageID := c.Param("cageID")
|
||||
|
||||
err := hc.validator.VarCtx(c, cageID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
err = hc.validator.VarCtx(c, gameID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
hamsters, err := hc.db.GetAllHamsters(gameID, cageID)
|
||||
if err != nil {
|
||||
utils.GetLoggerFromCtx(c).WithError(err).Error("error while getting hamsters")
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrInternalServer, "Error while getting hamsters")
|
||||
@@ -21,16 +36,30 @@ func (hc *handlersContext) getAllHamsters(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (hc *handlersContext) getAHamster(c *gin.Context) {
|
||||
hamsterID := c.Param("hamsterId")
|
||||
gameID := c.Param("gameID")
|
||||
cageID := c.Param("cageID")
|
||||
hamsterID := c.Param("hamsterID")
|
||||
|
||||
err := hc.validator.VarCtx(c, hamsterID, "uuid4")
|
||||
err := hc.validator.VarCtx(c, cageID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
hamster, err := hc.db.GetHamsterById(hamsterID)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
err = hc.validator.VarCtx(c, gameID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
err = hc.validator.VarCtx(c, hamsterID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
hamster, err := hc.db.GetHamsterByID(hamsterID, gameID, cageID)
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Hamster not found")
|
||||
@@ -55,6 +84,21 @@ func (hc *handlersContext) getAHamster(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (hc *handlersContext) createAHamster(c *gin.Context) {
|
||||
gameID := c.Param("gameID")
|
||||
cageID := c.Param("cageID")
|
||||
|
||||
err := hc.validator.VarCtx(c, cageID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
err = hc.validator.VarCtx(c, gameID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
b, err := c.GetRawData()
|
||||
if err != nil {
|
||||
utils.GetLoggerFromCtx(c).WithError(err).Error("error while creating hamster, read data fail")
|
||||
@@ -75,8 +119,9 @@ func (hc *handlersContext) createAHamster(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
hamsterToCreate.Cage = &model.Cage{ID: cageID, Game: model.Game{ID: gameID}}
|
||||
err = hc.db.CreateHamster(&hamsterToCreate)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeDuplicate:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrAlreadyExists, "Hamster already exists")
|
||||
@@ -100,18 +145,31 @@ func (hc *handlersContext) updateAHamster(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (hc *handlersContext) deleteAHamster(c *gin.Context) {
|
||||
hamsterID := c.Param("hamsterID")
|
||||
gameID := c.Param("gameID")
|
||||
cageID := c.Param("cageID")
|
||||
|
||||
hamsterID := c.Param("hamsterId")
|
||||
err := hc.validator.VarCtx(c, cageID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
err := hc.validator.VarCtx(c, hamsterID, "uuid4")
|
||||
err = hc.validator.VarCtx(c, gameID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
err = hc.validator.VarCtx(c, hamsterID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
// check hamster id given in URL exists
|
||||
_, err = hc.db.GetHamsterById(hamsterID)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
_, err = hc.db.GetHamsterByID(hamsterID, gameID, cageID)
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Hamster to delete not found")
|
||||
@@ -128,7 +186,7 @@ func (hc *handlersContext) deleteAHamster(c *gin.Context) {
|
||||
}
|
||||
|
||||
err = hc.db.DeleteHamster(hamsterID)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Hamster to delete not found")
|
||||
|
||||
@@ -75,32 +75,32 @@ func NewRouter(config *Config) *gin.Engine {
|
||||
securedUserRoute := userRoute.Group("")
|
||||
//TODO add secure auth
|
||||
securedUserRoute.Handle(http.MethodGet, "", hc.getAllUsers)
|
||||
securedUserRoute.Handle(http.MethodGet, "/:userId", hc.getUser)
|
||||
securedUserRoute.Handle(http.MethodPut, "/:userId", hc.updateUser)
|
||||
securedUserRoute.Handle(http.MethodDelete, "/:userId", hc.deleteUser)
|
||||
securedUserRoute.Handle(http.MethodGet, "/:userID", hc.getUser)
|
||||
securedUserRoute.Handle(http.MethodPut, "/:userID", hc.updateUser)
|
||||
securedUserRoute.Handle(http.MethodDelete, "/:userID", hc.deleteUser)
|
||||
// end: user routes
|
||||
|
||||
gameRoute := securedUserRoute.Group("/:userId/games")
|
||||
gameRoute := securedUserRoute.Group("/:userID/games")
|
||||
gameRoute.Handle(http.MethodGet, "", hc.getAllGames)
|
||||
gameRoute.Handle(http.MethodGet, "/:gameId", hc.getAGame)
|
||||
gameRoute.Handle(http.MethodPost, "/:gameId", hc.createAGame)
|
||||
gameRoute.Handle(http.MethodPut, "/:gameId", hc.updateAGame)
|
||||
gameRoute.Handle(http.MethodGet, "/:gameId/hamsters", hc.getHamstersOfGame)
|
||||
gameRoute.Handle(http.MethodDelete, "/:gameId", hc.deleteAGame)
|
||||
gameRoute.Handle(http.MethodGet, "/:gameID", hc.getAGame)
|
||||
gameRoute.Handle(http.MethodPost, "/:gameID", hc.createAGame)
|
||||
gameRoute.Handle(http.MethodPut, "/:gameID", hc.updateAGame)
|
||||
gameRoute.Handle(http.MethodGet, "/:gameID/hamsters", hc.getHamstersOfGame)
|
||||
gameRoute.Handle(http.MethodDelete, "/:gameID", hc.deleteAGame)
|
||||
|
||||
cageRoute := gameRoute.Group("/:gameId/cages")
|
||||
cageRoute := gameRoute.Group("/:gameID/cages")
|
||||
cageRoute.Handle(http.MethodGet, "", hc.getAllCages)
|
||||
cageRoute.Handle(http.MethodGet, "/:cageId", hc.getACage)
|
||||
cageRoute.Handle(http.MethodPost, "/:cageId", hc.createACage)
|
||||
cageRoute.Handle(http.MethodPut, "/:cageId", hc.updateACage)
|
||||
cageRoute.Handle(http.MethodDelete, "/:cageId", hc.deleteACage)
|
||||
cageRoute.Handle(http.MethodGet, "/:cageID", hc.getACage)
|
||||
cageRoute.Handle(http.MethodPost, "/:cageID", hc.createACage)
|
||||
cageRoute.Handle(http.MethodPut, "/:cageID", hc.updateACage)
|
||||
cageRoute.Handle(http.MethodDelete, "/:cageID", hc.deleteACage)
|
||||
|
||||
hamsterRoute := cageRoute.Group("/:cageId/hamsters")
|
||||
hamsterRoute := cageRoute.Group("/:cageID/hamsters")
|
||||
hamsterRoute.Handle(http.MethodGet, "", hc.getAllHamsters)
|
||||
hamsterRoute.Handle(http.MethodGet, "/:hamsterId", hc.getAHamster)
|
||||
hamsterRoute.Handle(http.MethodPost, "/:hamsterId", hc.createAHamster)
|
||||
hamsterRoute.Handle(http.MethodPut, "/:hamsterId", hc.updateAHamster)
|
||||
hamsterRoute.Handle(http.MethodDelete, "/:hamsterId", hc.deleteAHamster)
|
||||
hamsterRoute.Handle(http.MethodGet, "/:hamsterID", hc.getAHamster)
|
||||
hamsterRoute.Handle(http.MethodPost, "/:hamsterID", hc.createAHamster)
|
||||
hamsterRoute.Handle(http.MethodPut, "/:hamsterID", hc.updateAHamster)
|
||||
hamsterRoute.Handle(http.MethodDelete, "/:hamsterID", hc.deleteAHamster)
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ func (hc *handlersContext) connectUser(c *gin.Context) {
|
||||
utils.JSONError(c.Writer, model.ErrBadRequestFormat)
|
||||
return
|
||||
}
|
||||
user, err := hc.userService.GetUserFromGoogleId(tokenInfo.UserId)
|
||||
user, err := hc.userService.GetUserFromGoogleID(tokenInfo.UserId)
|
||||
if err != nil {
|
||||
utils.GetLogger().WithError(err).Error(err)
|
||||
if castedError, ok := err.(*model.APIError); ok {
|
||||
@@ -92,7 +92,7 @@ func (hc *handlersContext) createUser(c *gin.Context) {
|
||||
}
|
||||
|
||||
err = hc.db.CreateUser(&user)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeDuplicate:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrAlreadyExists, "User already exists")
|
||||
@@ -121,7 +121,7 @@ func (hc *handlersContext) getUser(c *gin.Context) {
|
||||
}
|
||||
|
||||
user, err := hc.db.GetUsersByID(userID)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "User not found")
|
||||
@@ -156,7 +156,7 @@ func (hc *handlersContext) deleteUser(c *gin.Context) {
|
||||
|
||||
// check user id given in URL exists
|
||||
_, err = hc.db.GetUsersByID(userID)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "User to delete not found")
|
||||
@@ -173,7 +173,7 @@ func (hc *handlersContext) deleteUser(c *gin.Context) {
|
||||
}
|
||||
|
||||
err = hc.db.DeleteUser(userID)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "User to delete not found")
|
||||
@@ -203,7 +203,7 @@ func (hc *handlersContext) updateUser(c *gin.Context) {
|
||||
|
||||
// check user id given in URL exists
|
||||
user, err := hc.db.GetUsersByID(userID)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "User to update not found")
|
||||
@@ -244,7 +244,7 @@ func (hc *handlersContext) updateUser(c *gin.Context) {
|
||||
|
||||
// make the update
|
||||
err = hc.db.UpdateUser(user)
|
||||
if e, ok := err.(*dao.DAOError); ok {
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "User to update not found")
|
||||
|
||||
Reference in New Issue
Block a user