clean up
This commit is contained in:
@@ -1,24 +1,17 @@
|
||||
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"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (hc *handlersContext) getAllCages(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
|
||||
}
|
||||
|
||||
cages, err := hc.db.GetAllCages(gameID)
|
||||
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")
|
||||
@@ -29,7 +22,6 @@ func (hc *handlersContext) getAllCages(c *gin.Context) {
|
||||
|
||||
func (hc *handlersContext) getACage(c *gin.Context) {
|
||||
cageID := c.Param("cageID")
|
||||
gameID := c.Param("gameID")
|
||||
|
||||
err := hc.validator.VarCtx(c, cageID, "uuid4")
|
||||
if err != nil {
|
||||
@@ -37,13 +29,7 @@ func (hc *handlersContext) getACage(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
err = hc.validator.VarCtx(c, gameID, "uuid4")
|
||||
if err != nil {
|
||||
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
cage, err := hc.db.GetCageByID(gameID, cageID)
|
||||
cage, err := hc.db.GetCageByID(cageID)
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
@@ -67,110 +53,3 @@ func (hc *handlersContext) getACage(c *gin.Context) {
|
||||
|
||||
utils.JSON(c.Writer, http.StatusOK, cage)
|
||||
}
|
||||
|
||||
func (hc *handlersContext) createACage(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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
cageToCreate.Game = model.Game{ID: gameID}
|
||||
err = hc.db.CreateCage(&cageToCreate)
|
||||
if e, ok := err.(*dao.Error); 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) {
|
||||
panic("TO DO")
|
||||
}
|
||||
|
||||
func (hc *handlersContext) deleteACage(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
|
||||
}
|
||||
|
||||
// check cage id given in URL exists
|
||||
_, err = hc.db.GetCageByID(gameID, cageID)
|
||||
if e, ok := err.(*dao.Error); 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.Error); 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user