diff --git a/handlers/cage_handler.go b/handlers/cage_handler.go index 5059597..5be7fb8 100644 --- a/handlers/cage_handler.go +++ b/handlers/cage_handler.go @@ -10,25 +10,25 @@ import ( ) func (hc *handlersContext) getAllCages(c *gin.Context) { - users, err := hc.db.GetAllCages() + cages, err := hc.db.GetAllCages() if err != nil { utils.GetLoggerFromCtx(c).WithError(err).Error("error while getting cages") utils.JSONErrorWithMessage(c.Writer, model.ErrInternalServer, "Error while getting cages") return } - utils.JSON(c.Writer, http.StatusOK, users) + utils.JSON(c.Writer, http.StatusOK, cages) } func (hc *handlersContext) getACage(c *gin.Context) { - userID := c.Param("cageId") + cageID := c.Param("cageId") - err := hc.validator.VarCtx(c, userID, "uuid4") + err := hc.validator.VarCtx(c, cageID, "uuid4") if err != nil { utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) return } - user, err := hc.db.GetCageById(userID) + cage, err := hc.db.GetCageById(cageID) if e, ok := err.(*dao.DAOError); ok { switch { case e.Type == dao.ErrTypeNotFound: @@ -45,12 +45,12 @@ func (hc *handlersContext) getACage(c *gin.Context) { return } - if user == nil { + if cage == nil { utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Cage not found") return } - utils.JSON(c.Writer, http.StatusOK, user) + utils.JSON(c.Writer, http.StatusOK, cage) } func (hc *handlersContext) createACage(c *gin.Context) { @@ -63,4 +63,48 @@ func (hc *handlersContext) updateACage(c *gin.Context) { func (hc *handlersContext) deleteACage(c *gin.Context) { + cageID := c.Param("cageId") + + err := hc.validator.VarCtx(c, cageID, "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 { + switch { + case e.Type == dao.ErrTypeNotFound: + utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Cage to delete not found") + return + default: + utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error DeleteCage: get cage error type not handled") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + } else if err != nil { + utils.GetLoggerFromCtx(c).WithError(err).Error("error while get cage to delete") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + + err = hc.db.DeleteCage(cageID) + if e, ok := err.(*dao.DAOError); ok { + switch { + case e.Type == dao.ErrTypeNotFound: + utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Cage to delete not found") + return + default: + utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error DeleteCage: Error type not handled") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + } else if err != nil { + utils.GetLoggerFromCtx(c).WithError(err).Error("error while deleting cage") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + + utils.JSON(c.Writer, http.StatusNoContent, nil) } diff --git a/handlers/game_handler.go b/handlers/game_handler.go index beea036..dc45ea8 100644 --- a/handlers/game_handler.go +++ b/handlers/game_handler.go @@ -63,6 +63,50 @@ func (hc *handlersContext) updateAGame(c *gin.Context) { func (hc *handlersContext) deleteAGame(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 + } + + // check game id given in URL exists + _, err = hc.db.GetGameById(gameID) + if e, ok := err.(*dao.DAOError); ok { + switch { + case e.Type == dao.ErrTypeNotFound: + utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Game to delete not found") + return + default: + utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error DeleteGame: get game error type not handled") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + } else if err != nil { + utils.GetLoggerFromCtx(c).WithError(err).Error("error while get game to delete") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + + err = hc.db.DeleteGame(gameID) + if e, ok := err.(*dao.DAOError); ok { + switch { + case e.Type == dao.ErrTypeNotFound: + utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Game to delete not found") + return + default: + utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error DeleteGame: Error type not handled") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + } else if err != nil { + utils.GetLoggerFromCtx(c).WithError(err).Error("error while deleting game") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + + utils.JSON(c.Writer, http.StatusNoContent, nil) } func (hc *handlersContext) getHamstersOfGame(c *gin.Context) { diff --git a/handlers/hamster_handler.go b/handlers/hamster_handler.go index c35cd38..0378668 100644 --- a/handlers/hamster_handler.go +++ b/handlers/hamster_handler.go @@ -63,4 +63,49 @@ func (hc *handlersContext) updateAHamster(c *gin.Context) { func (hc *handlersContext) deleteAHamster(c *gin.Context) { + hamsterID := c.Param("hamsterId") + + 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 { + switch { + case e.Type == dao.ErrTypeNotFound: + utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Hamster to delete not found") + return + default: + utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error DeleteHamster: get hamster error type not handled") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + } else if err != nil { + utils.GetLoggerFromCtx(c).WithError(err).Error("error while get hamster to delete") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + + err = hc.db.DeleteHamster(hamsterID) + if e, ok := err.(*dao.DAOError); ok { + switch { + case e.Type == dao.ErrTypeNotFound: + utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Hamster to delete not found") + return + default: + utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error DeleteHamster: Error type not handled") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + } else if err != nil { + utils.GetLoggerFromCtx(c).WithError(err).Error("error while deleting hamster") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + + utils.JSON(c.Writer, http.StatusNoContent, nil) + }