Feat: settings repo

This commit is contained in:
Grail Finder
2025-07-04 13:32:59 +03:00
parent 058d501774
commit 6be365473c
9 changed files with 72 additions and 19 deletions

View File

@ -50,9 +50,10 @@ func TestActionsRepo_CreateAction(t *testing.T) {
WordColor: "red",
Number: "3",
CreatedAt: time.Now(),
RoomID: roomID,
}
err := repo.CreateAction(context.Background(), roomID, action)
err := repo.CreateAction(context.Background(), action)
assert.NoError(t, err)
var retrievedAction models.Action
@ -75,6 +76,7 @@ func TestActionsRepo_ListActions(t *testing.T) {
Word: "apple",
WordColor: "red",
Number: "3",
RoomID: roomID,
CreatedAt: time.Now().Add(-2 * time.Second),
}
action2 := &models.Action{
@ -84,6 +86,7 @@ func TestActionsRepo_ListActions(t *testing.T) {
Word: "banana",
WordColor: "blue",
Number: "0",
RoomID: roomID,
CreatedAt: time.Now().Add(-1 * time.Second),
}

View File

@ -17,6 +17,7 @@ type AllRepos interface {
PlayersRepo
SessionsRepo
WordCardsRepo
SettingsRepo
InitTx(ctx context.Context) (context.Context, *sqlx.Tx, error)
}
@ -32,6 +33,11 @@ func NewRepoProvider(pathToDB string) *RepoProvider {
slog.Error("Unable to connect to database", "error", err)
os.Exit(1)
}
_, err = db.Exec("PRAGMA foreign_keys = ON;")
if err != nil {
slog.Error("Unable to enable foreign keys", "error", err)
os.Exit(1)
}
slog.Info("Successfully connected to database")
rp := &RepoProvider{
DB: db,

View File

@ -60,10 +60,6 @@ func (p *RepoProvider) RoomDeleteByID(ctx context.Context, id string) error {
func (p *RepoProvider) RoomUpdate(ctx context.Context, r *models.Room) error {
db := getDB(ctx, p.DB)
_, err := db.ExecContext(ctx, `UPDATE rooms SET team_turn = ?, this_turn_limit = ?, opened_this_turn = ?, blue_counter = ?, red_counter = ?, red_turn = ?, mime_done = ?, is_running = ?, is_over = ?, team_won = ?, room_link = ? WHERE id = ?`, r.TeamTurn, r.ThisTurnLimit, r.OpenedThisTurn, r.BlueCounter, r.RedCounter, r.RedTurn, r.MimeDone, r.IsRunning, r.IsOver, r.TeamWon, r.RoomLink, r.ID)
if err != nil {
return err
}
_, err = db.ExecContext(ctx, `UPDATE settings SET language = ?, room_pass = ?, turn_time = ? WHERE room_id = ?`, r.Settings.Language, r.Settings.RoomPass, r.Settings.RoundTime, r.ID)
return err
}

35
repos/settings.go Normal file
View File

@ -0,0 +1,35 @@
package repos
import (
"context"
"gralias/models"
"github.com/jmoiron/sqlx"
)
type SettingsRepo interface {
SettingsGetByRoomID(ctx context.Context, roomID string) (*models.GameSettings, error)
SettingsUpdate(ctx context.Context, settings *models.GameSettings) error
SettingsDeleteByRoomID(ctx context.Context, roomID string) error
}
func (p *RepoProvider) SettingsGetByRoomID(ctx context.Context, roomID string) (*models.GameSettings, error) {
settings := &models.GameSettings{}
err := sqlx.GetContext(ctx, p.DB, settings, `SELECT * FROM settings WHERE room_id = ?`, roomID)
if err != nil {
return nil, err
}
return settings, nil
}
func (p *RepoProvider) SettingsUpdate(ctx context.Context, s *models.GameSettings) error {
db := getDB(ctx, p.DB)
_, err := db.ExecContext(ctx, `UPDATE settings SET language = ?, room_pass = ?, turn_time = ?, turn_seconds_left = ? WHERE room_id = ?`, s.Language, s.RoomPass, s.RoundTime, s.TurnSecondsLeft, s.RoomID)
return err
}
func (p *RepoProvider) SettingsDeleteByRoomID(ctx context.Context, roomID string) error {
db := getDB(ctx, p.DB)
_, err := db.ExecContext(ctx, `DELETE FROM settings WHERE room_id = ?`, roomID)
return err
}