This commit is contained in:
2021-08-24 00:15:13 +02:00
parent 0757262143
commit 0b3872ab25
4 changed files with 16 additions and 345 deletions

View File

@@ -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)
}

View File

@@ -1,167 +0,0 @@
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.Error); 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.Error); 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.Error); 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.Error); 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) {
gameID := c.Param("gameID")
err := hc.validator.VarCtx(c, gameID, "uuid4")
if err != nil {
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
return
}
games, err := hc.db.GetHamstersOfGame(gameID)
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)
}

View File

@@ -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:

View File

@@ -4,7 +4,6 @@ import (
"hamster-tycoon/middlewares"
"hamster-tycoon/service"
"hamster-tycoon/storage/dao"
"hamster-tycoon/storage/dao/fake"
"hamster-tycoon/storage/dao/postgresql"
"hamster-tycoon/storage/validators"
"net/http"
@@ -30,7 +29,6 @@ type handlersContext struct {
db dao.Database
validator *validator.Validate
userService *service.UserService
gameService *service.GameService
cageService *service.CageService
hamsterService *service.HamsterService
}
@@ -54,14 +52,10 @@ func NewRouter(config *Config) *gin.Engine {
router.Use(middlewares.GetHTTPLoggerMiddleware())
hc := &handlersContext{}
if config.Mock {
hc.db = fake.NewDatabaseFake()
} else {
hc.db = postgresql.NewDatabasePostgreSQL(config.DBConnectionURI)
}
hc.db = postgresql.NewDatabasePostgreSQL(config.DBConnectionURI)
hc.validator = newValidator()
hc.userService = service.NewUserService(hc.db)
hc.gameService = service.NewGameService(hc.db)
hc.cageService = service.NewCageService(hc.db)
hc.hamsterService = service.NewHamsterService(hc.db)
@@ -80,20 +74,12 @@ func NewRouter(config *Config) *gin.Engine {
securedUserRoute.Handle(http.MethodDelete, "/:userID", hc.deleteUser)
// end: user routes
gameRoute := securedUserRoute.Group("/:userID/games")
gameRoute.Handle(http.MethodGet, "", hc.getAllGames)
gameRoute.Handle(http.MethodGet, "/:gameID", hc.getAGame)
gameRoute.Handle(http.MethodPost, "/:gameID", hc.createAGame)
gameRoute.Handle(http.MethodPut, "/:gameID", hc.updateAGame)
gameRoute.Handle(http.MethodGet, "/:gameID/hamsters", hc.getHamstersOfGame)
gameRoute.Handle(http.MethodDelete, "/:gameID", hc.deleteAGame)
cageRoute := gameRoute.Group("/:gameID/cages")
cageRoute := securedUserRoute.Group("/cages")
cageRoute.Handle(http.MethodGet, "", hc.getAllCages)
cageRoute.Handle(http.MethodGet, "/:cageID", hc.getACage)
cageRoute.Handle(http.MethodPost, "/:cageID", hc.createACage)
cageRoute.Handle(http.MethodPut, "/:cageID", hc.updateACage)
cageRoute.Handle(http.MethodDelete, "/:cageID", hc.deleteACage)
//cageRoute.Handle(http.MethodPost, "/:cageID", hc.createACage)
//cageRoute.Handle(http.MethodPut, "/:cageID", hc.updateACage)
//cageRoute.Handle(http.MethodDelete, "/:cageID", hc.deleteACage)
hamsterRoute := cageRoute.Group("/:cageID/hamsters")
hamsterRoute.Handle(http.MethodGet, "", hc.getAllHamsters)