clean up
This commit is contained in:
69
handlers_v2/cage_handler.go
Normal file
69
handlers_v2/cage_handler.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"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)
|
||||
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, cages)
|
||||
}
|
||||
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
cage, 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 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 cage == nil {
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Cage not found")
|
||||
return
|
||||
}
|
||||
|
||||
utils.JSON(c.Writer, http.StatusOK, cage)
|
||||
}
|
||||
Reference in New Issue
Block a user