generated from VLADIMIR/template
add sound screen
This commit is contained in:
@@ -3,6 +3,7 @@ package screen_sessions_repo
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
@@ -11,8 +12,10 @@ import (
|
||||
var ErrScreenNotFound = errors.New("Экран не найден")
|
||||
|
||||
type ScreenSession struct {
|
||||
ID int
|
||||
GameID *int
|
||||
ID int
|
||||
GameID *int
|
||||
Sound bool
|
||||
LastSeen time.Time
|
||||
}
|
||||
|
||||
type ScreenSessionsRepo struct {
|
||||
@@ -35,19 +38,46 @@ func (r *ScreenSessionsRepo) Create(ctx context.Context, tokenHash string) (int,
|
||||
func (r *ScreenSessionsRepo) Get(ctx context.Context, id int, tokenHash string) (*ScreenSession, error) {
|
||||
session := &ScreenSession{ID: id}
|
||||
err := r.pool.QueryRow(ctx, `
|
||||
SELECT game_id
|
||||
SELECT game_id, sound_enabled, last_seen_at
|
||||
FROM screen_sessions
|
||||
WHERE id = $1 AND access_token_hash = $2`, id, tokenHash).
|
||||
Scan(&session.GameID)
|
||||
Scan(&session.GameID, &session.Sound, &session.LastSeen)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrScreenNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = r.pool.Exec(ctx, `UPDATE screen_sessions SET last_seen_at = NOW() WHERE id = $1`, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
session.LastSeen = time.Now()
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (r *ScreenSessionsRepo) ListByGame(ctx context.Context, gameID int) ([]*ScreenSession, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT id, game_id, sound_enabled, last_seen_at
|
||||
FROM screen_sessions
|
||||
WHERE game_id = $1
|
||||
ORDER BY id`, gameID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var sessions []*ScreenSession
|
||||
for rows.Next() {
|
||||
session := &ScreenSession{}
|
||||
if err := rows.Scan(&session.ID, &session.GameID, &session.Sound, &session.LastSeen); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sessions = append(sessions, session)
|
||||
}
|
||||
return sessions, rows.Err()
|
||||
}
|
||||
|
||||
func (r *ScreenSessionsRepo) Bind(ctx context.Context, id, gameID int, tokenHash string) error {
|
||||
result, err := r.pool.Exec(ctx, `
|
||||
UPDATE screen_sessions
|
||||
@@ -75,3 +105,21 @@ func (r *ScreenSessionsRepo) Unbind(ctx context.Context, id int) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ScreenSessionsRepo) SetSound(ctx context.Context, id int, enabled bool) error {
|
||||
query := `UPDATE screen_sessions SET sound_enabled = FALSE, updated_at = NOW() WHERE id = $1`
|
||||
args := []any{id}
|
||||
if enabled {
|
||||
query = `UPDATE screen_sessions
|
||||
SET sound_enabled = (id = $1), updated_at = NOW()
|
||||
WHERE game_id = (SELECT game_id FROM screen_sessions WHERE id = $1)`
|
||||
}
|
||||
result, err := r.pool.Exec(ctx, query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if result.RowsAffected() == 0 {
|
||||
return ErrScreenNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user