clean up
This commit is contained in:
@@ -2,16 +2,16 @@ 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) getAllHamsters(c *gin.Context) {
|
||||
gameID := c.Param("gameID")
|
||||
cageID := c.Param("cageID")
|
||||
|
||||
err := hc.validator.VarCtx(c, cageID, "uuid4")
|
||||
@@ -20,13 +20,7 @@ func (hc *handlersContext) getAllHamsters(c *gin.Context) {
|
||||
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)
|
||||
hamsters, err := hc.db.GetAllHamsters(cageID)
|
||||
if err != nil {
|
||||
utils.GetLoggerFromCtx(c).WithError(err).Error("error while getting hamsters")
|
||||
utils.JSONErrorWithMessage(c.Writer, model.ErrInternalServer, "Error while getting hamsters")
|
||||
@@ -36,7 +30,6 @@ func (hc *handlersContext) getAllHamsters(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (hc *handlersContext) getAHamster(c *gin.Context) {
|
||||
gameID := c.Param("gameID")
|
||||
cageID := c.Param("cageID")
|
||||
hamsterID := c.Param("hamsterID")
|
||||
|
||||
@@ -46,19 +39,13 @@ func (hc *handlersContext) getAHamster(c *gin.Context) {
|
||||
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)
|
||||
hamster, err := hc.db.GetHamsterByID(hamsterID, cageID)
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
@@ -84,7 +71,6 @@ func (hc *handlersContext) getAHamster(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (hc *handlersContext) createAHamster(c *gin.Context) {
|
||||
gameID := c.Param("gameID")
|
||||
cageID := c.Param("cageID")
|
||||
|
||||
err := hc.validator.VarCtx(c, cageID, "uuid4")
|
||||
@@ -93,12 +79,6 @@ func (hc *handlersContext) createAHamster(c *gin.Context) {
|
||||
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")
|
||||
@@ -119,7 +99,7 @@ func (hc *handlersContext) createAHamster(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
hamsterToCreate.Cage = &model.Cage{ID: cageID, Game: model.Game{ID: gameID}}
|
||||
hamsterToCreate.Cage = &model.Cage{ID: cageID}
|
||||
err = hc.db.CreateHamster(&hamsterToCreate)
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
@@ -146,7 +126,6 @@ func (hc *handlersContext) updateAHamster(c *gin.Context) {
|
||||
|
||||
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")
|
||||
@@ -155,12 +134,6 @@ func (hc *handlersContext) deleteAHamster(c *gin.Context) {
|
||||
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))
|
||||
@@ -168,7 +141,7 @@ func (hc *handlersContext) deleteAHamster(c *gin.Context) {
|
||||
}
|
||||
|
||||
// check hamster id given in URL exists
|
||||
_, err = hc.db.GetHamsterByID(hamsterID, gameID, cageID)
|
||||
_, err = hc.db.GetHamsterByID(hamsterID, cageID)
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
|
||||
Reference in New Issue
Block a user