feat: add get a game service and fake database
This commit is contained in:
@@ -1,13 +1,50 @@
|
||||
package handlers
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"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) {
|
||||
|
||||
}
|
||||
|
||||
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.DAOError); 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) {
|
||||
|
||||
@@ -27,9 +27,12 @@ type Config struct {
|
||||
}
|
||||
|
||||
type handlersContext struct {
|
||||
db dao.Database
|
||||
validator *validator.Validate
|
||||
userService *service.UserService
|
||||
db dao.Database
|
||||
validator *validator.Validate
|
||||
userService *service.UserService
|
||||
gameService *service.GameService
|
||||
cageService *service.CageService
|
||||
hamsterService *service.HamsterService
|
||||
}
|
||||
|
||||
func NewRouter(config *Config) *gin.Engine {
|
||||
@@ -58,6 +61,10 @@ func NewRouter(config *Config) *gin.Engine {
|
||||
}
|
||||
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)
|
||||
|
||||
public := router.Group("/")
|
||||
public.Handle(http.MethodGet, "/_health", hc.GetHealth)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user