package repo import ( "strconv" model "git.kapelle.org/niklas/beerpong-elo/internal/model" ) type InMemoryRepo struct { games []model.Game players []model.Player results []model.GameResult } func NewInMemoryRepo() Repo { return &InMemoryRepo{ games: []model.Game{}, players: []model.Player{}, results: []model.GameResult{}, } } func (r *InMemoryRepo) AddGame(game model.Game) (model.GameID, error) { id := len(r.games) parsedGame := model.Game{ ID: model.GameID(strconv.Itoa(id)), Added: game.Added, Author: game.Author, Team0Player0: game.Team0Player0, Team0Player1: game.Team0Player1, Team1Player0: game.Team1Player0, Team1Player1: game.Team1Player1, Score: game.Score, Overtime: game.Overtime, } r.games = append(r.games, parsedGame) return model.GameID(strconv.Itoa(id)), nil } func (r *InMemoryRepo) GetGame(id model.GameID) (*model.Game, error) { i, err := strconv.Atoi(string(id)) if err != nil { return nil, err } return &r.games[i], nil } func (r *InMemoryRepo) GetAllGames() []model.Game { return r.games } func (r *InMemoryRepo) GetOrCreatePlayerID(name string) (model.PlayerID, error) { id := model.PlayerID(name) for _, player := range r.players { if player.ID == id { return id, nil } } // No player found. Create one. r.players = append(r.players, model.NewPlayer(id)) return id, nil } func (r *InMemoryRepo) GetPlayer(id model.PlayerID) (*model.Player, error) { for _, player := range r.players { if player.ID == id { return &player, nil } } return nil, nil } func (r *InMemoryRepo) GetAllPlayers() ([]model.Player,error){ return r.players, nil } func (r *InMemoryRepo) AddGameResult(result model.GameResult) (model.GameResultID, error) { id := model.GameResultID(strconv.Itoa(len(r.results))) result.ID = id r.results = append(r.results, result) return id, nil } func (r *InMemoryRepo) GetGameResult(id model.GameResultID) (*model.GameResult, error) { i, err := strconv.Atoi(string(id)) if err != nil { return nil, nil } return &r.results[i], nil }