This commit is contained in:
2021-08-24 00:11:00 +02:00
parent 57bde4ac3c
commit 0757262143
22 changed files with 716 additions and 632 deletions

View File

@@ -0,0 +1,207 @@
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) getAllHamsters(c *gin.Context) {
gameID := c.Param("gameID")
cageID := c.Param("cageID")
err := hc.validator.VarCtx(c, cageID, "uuid4")
if err != nil {
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
return
}
err = hc.validator.VarCtx(c, gameID, "uuid4")
if err != nil {
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
return
}
hamsters, err := hc.db.GetAllHamsters(gameID, cageID)
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, hamsters)
}
func (hc *handlersContext) getAHamster(c *gin.Context) {
gameID := c.Param("gameID")
cageID := c.Param("cageID")
hamsterID := c.Param("hamsterID")
err := hc.validator.VarCtx(c, cageID, "uuid4")
if err != nil {
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
return
}
err = hc.validator.VarCtx(c, gameID, "uuid4")
if err != nil {
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
return
}
err = hc.validator.VarCtx(c, hamsterID, "uuid4")
if err != nil {
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
return
}
hamster, err := hc.db.GetHamsterByID(hamsterID, gameID, cageID)
if e, ok := err.(*dao.Error); 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 hamster == nil {
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Hamster not found")
return
}
utils.JSON(c.Writer, http.StatusOK, hamster)
}
func (hc *handlersContext) createAHamster(c *gin.Context) {
gameID := c.Param("gameID")
cageID := c.Param("cageID")
err := hc.validator.VarCtx(c, cageID, "uuid4")
if err != nil {
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
return
}
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 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
}
hamsterToCreate.Cage = &model.Cage{ID: cageID, Game: model.Game{ID: gameID}}
err = hc.db.CreateHamster(&hamsterToCreate)
if e, ok := err.(*dao.Error); 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) {
panic("TO DO")
}
func (hc *handlersContext) deleteAHamster(c *gin.Context) {
hamsterID := c.Param("hamsterID")
gameID := c.Param("gameID")
cageID := c.Param("cageID")
err := hc.validator.VarCtx(c, cageID, "uuid4")
if err != nil {
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
return
}
err = hc.validator.VarCtx(c, gameID, "uuid4")
if err != nil {
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
return
}
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, gameID, cageID)
if e, ok := err.(*dao.Error); 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.Error); 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)
}