Files
hamster-tycoon/handlers/game_handler.go
Jeffrey Duroyon 65bba00863 add create handler
2020-05-17 01:30:15 +02:00

153 lines
4.3 KiB
Go

package handlers
import (
"encoding/json"
"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) getAllGames(c *gin.Context) {
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, games)
}
func (hc *handlersContext) getAGame(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
}
game, err := hc.gameService.GetAGameById(gameId)
if e, ok := err.(*dao.DAOError); ok {
switch {
case e.Type == dao.ErrTypeNotFound:
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Game not found")
return
default:
utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error GetGame: 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")
utils.JSONError(c.Writer, model.ErrInternalServer)
return
}
if game == nil {
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Game not found")
return
}
utils.JSON(c.Writer, http.StatusOK, game)
}
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) {
panic("TO DO")
}
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) {
}