moved repo and inMemoryRepo in 2 files

This commit is contained in:
Niklas Kapelle 2025-01-05 02:57:26 +01:00
parent 9997ed4fd2
commit e7b1241cfe
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
2 changed files with 84 additions and 79 deletions

View File

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

View File

@ -1,8 +1,6 @@
package repo package repo
import ( import (
"strconv"
model "git.kapelle.org/niklas/beerpong-elo/internal/model" model "git.kapelle.org/niklas/beerpong-elo/internal/model"
) )
@ -14,80 +12,3 @@ type Repo interface {
GetOrCreatePlayerID(name string) model.PlayerID GetOrCreatePlayerID(name string) model.PlayerID
GetPlayer(id model.PlayerID) *model.Player 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
}