diff --git a/handlers/cage_handler.go b/handlers/cage_handler.go index 136dfcd..5059597 100644 --- a/handlers/cage_handler.go +++ b/handlers/cage_handler.go @@ -1,13 +1,56 @@ package handlers -import "github.com/gin-gonic/gin" +import ( + "github.com/gin-gonic/gin" + "hamster-tycoon/storage/dao" + "hamster-tycoon/storage/model" + "hamster-tycoon/storage/validators" + "hamster-tycoon/utils" + "net/http" +) func (hc *handlersContext) getAllCages(c *gin.Context) { - + users, 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) } func (hc *handlersContext) getACage(c *gin.Context) { + userID := c.Param("cageId") + err := hc.validator.VarCtx(c, userID, "uuid4") + if err != nil { + utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) + return + } + + user, err := hc.db.GetCageById(userID) + if e, ok := err.(*dao.DAOError); ok { + switch { + case e.Type == dao.ErrTypeNotFound: + utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Cage not found") + return + default: + utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error GetCage: 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") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + + if user == nil { + utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Cage not found") + return + } + + utils.JSON(c.Writer, http.StatusOK, user) } func (hc *handlersContext) createACage(c *gin.Context) { diff --git a/handlers/game_handler.go b/handlers/game_handler.go index 721e250..beea036 100644 --- a/handlers/game_handler.go +++ b/handlers/game_handler.go @@ -10,7 +10,13 @@ import ( ) func (hc *handlersContext) getAllGames(c *gin.Context) { - + users, err := hc.db.GetAllGames() + 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, users) } func (hc *handlersContext) getAGame(c *gin.Context) { diff --git a/handlers/hamster_handler.go b/handlers/hamster_handler.go index 9152015..c35cd38 100644 --- a/handlers/hamster_handler.go +++ b/handlers/hamster_handler.go @@ -1,13 +1,56 @@ package handlers -import "github.com/gin-gonic/gin" +import ( + "github.com/gin-gonic/gin" + "hamster-tycoon/storage/dao" + "hamster-tycoon/storage/model" + "hamster-tycoon/storage/validators" + "hamster-tycoon/utils" + "net/http" +) func (hc *handlersContext) getAllHamsters(c *gin.Context) { - + users, err := hc.db.GetAllHamsters() + if err != nil { + utils.GetLoggerFromCtx(c).WithError(err).Error("error while getting hamsters") + utils.JSONErrorWithMessage(c.Writer, model.ErrInternalServer, "Error while getting hamsters") + return + } + utils.JSON(c.Writer, http.StatusOK, users) } func (hc *handlersContext) getAHamster(c *gin.Context) { + userID := c.Param("hamsterId") + err := hc.validator.VarCtx(c, userID, "uuid4") + if err != nil { + utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) + return + } + + user, err := hc.db.GetHamsterById(userID) + if e, ok := err.(*dao.DAOError); ok { + switch { + case e.Type == dao.ErrTypeNotFound: + utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Hamster not found") + return + default: + utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error GetHamster: 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") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + + if user == nil { + utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Hamster not found") + return + } + + utils.JSON(c.Writer, http.StatusOK, user) } func (hc *handlersContext) createAHamster(c *gin.Context) {