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)
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package service
|
||||
|
||||
import "hamster-tycoon/storage/dao"
|
||||
import (
|
||||
"hamster-tycoon/storage/dao"
|
||||
"hamster-tycoon/storage/model"
|
||||
)
|
||||
|
||||
type GameService struct {
|
||||
serviceContext
|
||||
@@ -8,4 +11,20 @@ type GameService struct {
|
||||
|
||||
func NewGameService(database dao.Database) *GameService {
|
||||
return &GameService{serviceContext{db: database}}
|
||||
}
|
||||
}
|
||||
|
||||
func (gs *GameService) GetAGameById(id string) (*model.Game, error) {
|
||||
game, err := gs.db.GetGameById(id)
|
||||
if err != nil {
|
||||
if castedError, ok := err.(*dao.DAOError); ok {
|
||||
switch castedError.Type {
|
||||
case dao.ErrTypeNotFound:
|
||||
return nil, &model.ErrNotFound
|
||||
default:
|
||||
return nil, &model.ErrInternalServer
|
||||
}
|
||||
}
|
||||
return nil, &model.ErrInternalServer
|
||||
}
|
||||
return game, nil
|
||||
}
|
||||
|
||||
@@ -1,16 +1,59 @@
|
||||
package fake
|
||||
|
||||
import "hamster-tycoon/storage/model"
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"hamster-tycoon/storage/dao"
|
||||
"hamster-tycoon/storage/model"
|
||||
"hamster-tycoon/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
cacheKeyGames = "games"
|
||||
)
|
||||
|
||||
func (db *DatabaseFake) saveGames(games []*model.Game) {
|
||||
data := make([]interface{}, 0)
|
||||
for _, v := range games {
|
||||
data = append(data, v)
|
||||
}
|
||||
db.save(cacheKeyGames, data)
|
||||
}
|
||||
|
||||
func (db *DatabaseFake) loadGames() []*model.Game {
|
||||
games := make([]*model.Game, 0)
|
||||
b, err := db.Cache.Get(cacheKeyGames)
|
||||
if err != nil {
|
||||
return games
|
||||
}
|
||||
err = json.Unmarshal(b, &games)
|
||||
if err != nil {
|
||||
utils.GetLogger().WithError(err).Error("Error while unmarshal fake users")
|
||||
}
|
||||
return games
|
||||
}
|
||||
|
||||
func (db *DatabaseFake) GetAllGames() ([]*model.Game, error) {
|
||||
return nil, nil
|
||||
return db.loadGames(), nil
|
||||
}
|
||||
|
||||
func (db *DatabaseFake) GetGameById(string) (*model.Game, error) {
|
||||
return nil, nil
|
||||
func (db *DatabaseFake) GetGameById(gameId string) (*model.Game, error) {
|
||||
games := db.loadGames()
|
||||
for _, g := range games {
|
||||
if g.ID == gameId {
|
||||
return g, nil
|
||||
}
|
||||
}
|
||||
return nil, dao.NewDAOError(dao.ErrTypeNotFound, errors.New("game not found"))
|
||||
}
|
||||
|
||||
func (db *DatabaseFake) CreateGame(*model.Game) error {
|
||||
func (db *DatabaseFake) CreateGame(game *model.Game) error {
|
||||
game.ID = uuid.NewV4().String()
|
||||
|
||||
games := db.loadGames()
|
||||
games = append(games, game)
|
||||
db.saveGames(games)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user