diff --git a/handlers/cage_handler.go b/handlers/cage_handler.go index a0e8c06..3a9f60a 100644 --- a/handlers/cage_handler.go +++ b/handlers/cage_handler.go @@ -1,6 +1,7 @@ package handlers import ( + "encoding/json" "github.com/gin-gonic/gin" "hamster-tycoon/storage/dao" "hamster-tycoon/storage/model" @@ -54,7 +55,44 @@ func (hc *handlersContext) getACage(c *gin.Context) { } func (hc *handlersContext) createACage(c *gin.Context) { + b, err := c.GetRawData() + if err != nil { + utils.GetLoggerFromCtx(c).WithError(err).Error("error while creating cage, read data fail") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + cageToCreate := model.Cage{} + err = json.Unmarshal(b, &cageToCreate) + if err != nil { + utils.JSONError(c.Writer, model.ErrBadRequestFormat) + return + } + + err = hc.validator.StructCtx(c, cageToCreate) + if err != nil { + utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) + return + } + + err = hc.db.CreateCage(&cage) + if e, ok := err.(*dao.DAOError); ok { + switch { + case e.Type == dao.ErrTypeDuplicate: + utils.JSONErrorWithMessage(c.Writer, model.ErrAlreadyExists, "Cage already exists") + return + default: + utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error CreateCage: Error type not handled") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + } else if err != nil { + utils.GetLoggerFromCtx(c).WithError(err).Error("error while creating cage") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + + utils.JSON(c.Writer, http.StatusCreated, cageToCreate) } func (hc *handlersContext) updateACage(c *gin.Context) { diff --git a/handlers/game_handler.go b/handlers/game_handler.go index 77c4446..bf7058a 100644 --- a/handlers/game_handler.go +++ b/handlers/game_handler.go @@ -1,6 +1,7 @@ package handlers import ( + "encoding/json" "github.com/gin-gonic/gin" "hamster-tycoon/storage/dao" "hamster-tycoon/storage/model" @@ -10,13 +11,13 @@ import ( ) func (hc *handlersContext) getAllGames(c *gin.Context) { - users, err := hc.db.GetAllGames() + games, 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) + utils.JSON(c.Writer, http.StatusOK, games) } func (hc *handlersContext) getAGame(c *gin.Context) { @@ -54,7 +55,44 @@ func (hc *handlersContext) getAGame(c *gin.Context) { } func (hc *handlersContext) createAGame(c *gin.Context) { + b, err := c.GetRawData() + if err != nil { + utils.GetLoggerFromCtx(c).WithError(err).Error("error while creating game, read data fail") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + gameToCreate := model.Game{} + err = json.Unmarshal(b, &gameToCreate) + if err != nil { + utils.JSONError(c.Writer, model.ErrBadRequestFormat) + return + } + + err = hc.validator.StructCtx(c, gameToCreate) + if err != nil { + utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) + return + } + + err = hc.db.CreateGame(&gameToCreate) + if e, ok := err.(*dao.DAOError); ok { + switch { + case e.Type == dao.ErrTypeDuplicate: + utils.JSONErrorWithMessage(c.Writer, model.ErrAlreadyExists, "Game already exists") + return + default: + utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error CreateGame: Error type not handled") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + } else if err != nil { + utils.GetLoggerFromCtx(c).WithError(err).Error("error while creating game") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + + utils.JSON(c.Writer, http.StatusCreated, gameToCreate) } func (hc *handlersContext) updateAGame(c *gin.Context) { diff --git a/handlers/hamster_handler.go b/handlers/hamster_handler.go index bedab46..789ce23 100644 --- a/handlers/hamster_handler.go +++ b/handlers/hamster_handler.go @@ -1,6 +1,7 @@ package handlers import ( + "encoding/json" "github.com/gin-gonic/gin" "hamster-tycoon/storage/dao" "hamster-tycoon/storage/model" @@ -10,25 +11,25 @@ import ( ) func (hc *handlersContext) getAllHamsters(c *gin.Context) { - users, err := hc.db.GetAllHamsters() + hamsters, 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) + utils.JSON(c.Writer, http.StatusOK, hamsters) } func (hc *handlersContext) getAHamster(c *gin.Context) { - userID := c.Param("hamsterId") + hamsterID := c.Param("hamsterId") - err := hc.validator.VarCtx(c, userID, "uuid4") + err := hc.validator.VarCtx(c, hamsterID, "uuid4") if err != nil { utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) return } - user, err := hc.db.GetHamsterById(userID) + hamster, err := hc.db.GetHamsterById(hamsterID) if e, ok := err.(*dao.DAOError); ok { switch { case e.Type == dao.ErrTypeNotFound: @@ -45,16 +46,53 @@ func (hc *handlersContext) getAHamster(c *gin.Context) { return } - if user == nil { + if hamster == nil { utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Hamster not found") return } - utils.JSON(c.Writer, http.StatusOK, user) + utils.JSON(c.Writer, http.StatusOK, hamster) } func (hc *handlersContext) createAHamster(c *gin.Context) { + b, err := c.GetRawData() + if err != nil { + utils.GetLoggerFromCtx(c).WithError(err).Error("error while creating hamster, read data fail") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + hamsterToCreate := model.Hamster{} + err = json.Unmarshal(b, &hamsterToCreate) + if err != nil { + utils.JSONError(c.Writer, model.ErrBadRequestFormat) + return + } + + err = hc.validator.StructCtx(c, hamsterToCreate) + if err != nil { + utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) + return + } + + err = hc.db.CreateHamster(&hamsterToCreate) + if e, ok := err.(*dao.DAOError); ok { + switch { + case e.Type == dao.ErrTypeDuplicate: + utils.JSONErrorWithMessage(c.Writer, model.ErrAlreadyExists, "Hamster already exists") + return + default: + utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error CreateHamster: Error type not handled") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + } else if err != nil { + utils.GetLoggerFromCtx(c).WithError(err).Error("error while creating hamster") + utils.JSONError(c.Writer, model.ErrInternalServer) + return + } + + utils.JSON(c.Writer, http.StatusCreated, hamsterToCreate) } func (hc *handlersContext) updateAHamster(c *gin.Context) {