From e7b1241cfee5d9fc746725ce90ea54395d48ed7c Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Sun, 5 Jan 2025 02:57:26 +0100 Subject: [PATCH] moved repo and inMemoryRepo in 2 files --- internal/repo/inMemoryRepo.go | 84 +++++++++++++++++++++++++++++++++++ internal/repo/repo.go | 79 -------------------------------- 2 files changed, 84 insertions(+), 79 deletions(-) create mode 100644 internal/repo/inMemoryRepo.go diff --git a/internal/repo/inMemoryRepo.go b/internal/repo/inMemoryRepo.go new file mode 100644 index 0000000..44bb0ed --- /dev/null +++ b/internal/repo/inMemoryRepo.go @@ -0,0 +1,84 @@ +package repo + +import ( + "strconv" + + model "git.kapelle.org/niklas/beerpong-elo/internal/model" +) + +type InMemoryRepo struct { + games []model.Game + players []model.Player +} + +func NewInMemoryRepo() Repo { + return &InMemoryRepo{ + games: []model.Game{}, + players: []model.Player{}, + } +} + +func (r *InMemoryRepo) AddGame(game model.InputGame) model.GameID { + id := len(r.games) + + authID := r.GetOrCreatePlayerID(game.Author) + t0p0ID := r.GetOrCreatePlayerID(game.Team0Player0) + t0p1ID := r.GetOrCreatePlayerID(game.Team0Player1) + t1p0ID := r.GetOrCreatePlayerID(game.Team1Player0) + t1p1ID := r.GetOrCreatePlayerID(game.Team1Player1) + + parsedGame := model.Game{ + ID: model.GameID(rune(id)), + Added: game.Added, + Author: authID, + Team0Player0: t0p0ID, + Team0Player1: t0p1ID, + Team1Player0: t1p1ID, + Team1Player1: t1p0ID, + Score: game.Score, + Overtime: game.Overtime, + } + + r.games = append(r.games, parsedGame) + + return model.GameID((rune(id))) +} + +func (r *InMemoryRepo) GetGame(id model.GameID) model.Game { + i, err := strconv.Atoi(string(id)) + + if err != nil { + panic(err) + } + + return r.games[i] +} + +func (r *InMemoryRepo) GetAllGames() []model.Game { + return r.games +} + +func (r *InMemoryRepo) GetOrCreatePlayerID(name string) model.PlayerID { + id := model.PlayerID(name) + + for _, player := range r.players { + if player.ID == id { + return id + } + } + + // No player found. Create one. + r.players = append(r.players, model.NewPlayer(id)) + + return id +} + +func (r *InMemoryRepo) GetPlayer(id model.PlayerID) *model.Player { + for _, player := range r.players { + if player.ID == id { + return &player + } + } + + return nil +} diff --git a/internal/repo/repo.go b/internal/repo/repo.go index 7b1d6d9..814dc24 100644 --- a/internal/repo/repo.go +++ b/internal/repo/repo.go @@ -1,8 +1,6 @@ package repo import ( - "strconv" - model "git.kapelle.org/niklas/beerpong-elo/internal/model" ) @@ -14,80 +12,3 @@ type Repo interface { GetOrCreatePlayerID(name string) model.PlayerID GetPlayer(id model.PlayerID) *model.Player } - -type InMemoryRepo struct { - games []model.Game - players []model.Player -} - -func NewInMemoryRepo() Repo { - return &InMemoryRepo{ - games: []model.Game{}, - players: []model.Player{}, - } -} - -func (r *InMemoryRepo) AddGame(game model.InputGame) model.GameID { - id := len(r.games) - - authID := r.GetOrCreatePlayerID(game.Author) - t0p0ID := r.GetOrCreatePlayerID(game.Team0Player0) - t0p1ID := r.GetOrCreatePlayerID(game.Team0Player1) - t1p0ID := r.GetOrCreatePlayerID(game.Team1Player0) - t1p1ID := r.GetOrCreatePlayerID(game.Team1Player1) - - parsedGame := model.Game{ - ID: model.GameID(rune(id)), - Added: game.Added, - Author: authID, - Team0Player0: t0p0ID, - Team0Player1: t0p1ID, - Team1Player0: t1p1ID, - Team1Player1: t1p0ID, - Score: game.Score, - Overtime: game.Overtime, - } - - r.games = append(r.games, parsedGame) - - return model.GameID((rune(id))) -} - -func (r *InMemoryRepo) GetGame(id model.GameID) model.Game { - i, err := strconv.Atoi(string(id)) - - if err != nil { - panic(err) - } - - return r.games[i] -} - -func (r *InMemoryRepo) GetAllGames() []model.Game { - return r.games -} - -func (r *InMemoryRepo) GetOrCreatePlayerID(name string) model.PlayerID { - id := model.PlayerID(name) - - for _, player := range r.players { - if player.ID == id { - return id - } - } - - // No player found. Create one. - r.players = append(r.players, model.NewPlayer(id)) - - return id -} - -func (r *InMemoryRepo) GetPlayer(id model.PlayerID) *model.Player { - for _, player := range r.players { - if player.ID == id { - return &player - } - } - - return nil -}