Feat: add player interface
This commit is contained in:
@ -50,12 +50,24 @@ type Team struct {
|
||||
}
|
||||
|
||||
type Action struct {
|
||||
Actor string
|
||||
ActorColor string
|
||||
Action string // clue | guess
|
||||
Word string
|
||||
WordColor string
|
||||
Number string // for clue
|
||||
ID int `json:"id" db:"id"`
|
||||
RoomID string `json:"room_id" db:"room_id"`
|
||||
Actor string `json:"actor" db:"actor"`
|
||||
ActorColor string `json:"actor_color" db:"actor_color"`
|
||||
Action string `json:"action_type" db:"action_type"`
|
||||
Word string `json:"word" db:"word"`
|
||||
WordColor string `json:"word_color" db:"word_color"`
|
||||
Number string `json:"number_associated" db:"number_associated"`
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
}
|
||||
|
||||
type Player struct {
|
||||
ID int `json:"id" db:"id"`
|
||||
RoomID string `json:"room_id" db:"room_id"`
|
||||
Username string `json:"username" db:"username"`
|
||||
Team UserTeam `json:"team" db:"team"`
|
||||
Role UserRole `json:"role" db:"role"`
|
||||
IsBot bool `json:"is_bot" db:"is_bot"`
|
||||
}
|
||||
|
||||
type BotPlayer struct {
|
||||
|
@ -1,11 +1,31 @@
|
||||
package repos
|
||||
|
||||
import "github.com/jackc/pgx/v5/pgxpool"
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type AllRepos interface {
|
||||
RoomsRepo
|
||||
ActionsRepo
|
||||
PlayersRepo
|
||||
}
|
||||
|
||||
type RepoProvider struct {
|
||||
DB *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewRepoProvider(pathToDB string) *RepoProvider {
|
||||
dbpool, err := pgxpool.New(context.Background(), pathToDB)
|
||||
if err != nil {
|
||||
slog.Error("Unable to connect to database", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
slog.Info("Successfully connected to database")
|
||||
return &RepoProvider{
|
||||
DB: dbpool,
|
||||
}
|
||||
}
|
||||
|
31
repos/players.go
Normal file
31
repos/players.go
Normal file
@ -0,0 +1,31 @@
|
||||
package repos
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gralias/models"
|
||||
)
|
||||
|
||||
type PlayersRepo interface {
|
||||
GetPlayer(roomID, username string) (*models.Player, error)
|
||||
AddPlayer(player *models.Player) error
|
||||
DeletePlayer(roomID, username string) error
|
||||
}
|
||||
|
||||
func (p *RepoProvider) GetPlayer(roomID, username string) (*models.Player, error) {
|
||||
var player models.Player
|
||||
err := p.DB.QueryRow(context.Background(), "SELECT id, room_id, username, team, role, is_bot FROM players WHERE room_id = $1 AND username = $2", roomID, username).Scan(&player.ID, &player.RoomID, &player.Username, &player.Team, &player.Role, &player.IsBot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &player, nil
|
||||
}
|
||||
|
||||
func (p *RepoProvider) AddPlayer(player *models.Player) error {
|
||||
_, err := p.DB.Exec(context.Background(), "INSERT INTO players (room_id, username, team, role, is_bot) VALUES ($1, $2, $3, $4, $5)", player.RoomID, player.Username, player.Team, player.Role, player.IsBot)
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *RepoProvider) DeletePlayer(roomID, username string) error {
|
||||
_, err := p.DB.Exec(context.Background(), "DELETE FROM players WHERE room_id = $1 AND username = $2", roomID, username)
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user