add missing path property handling

This commit is contained in:
Jeffrey Duroyon
2020-05-18 00:56:43 +02:00
parent ca8e42388b
commit f6e0a826a1
21 changed files with 292 additions and 114 deletions

View File

@@ -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")