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