generated from VLADIMIR/template
add screen mode
This commit is contained in:
@@ -16,6 +16,9 @@ type ScreenSession struct {
|
||||
GameID *int
|
||||
Sound bool
|
||||
LastSeen time.Time
|
||||
Mode int32
|
||||
Version int64
|
||||
Position int64
|
||||
}
|
||||
|
||||
type ScreenSessionsRepo struct {
|
||||
@@ -38,10 +41,10 @@ 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, sound_enabled, last_seen_at
|
||||
SELECT game_id, sound_enabled, last_seen_at, mode, state_version, position_ms
|
||||
FROM screen_sessions
|
||||
WHERE id = $1 AND access_token_hash = $2`, id, tokenHash).
|
||||
Scan(&session.GameID, &session.Sound, &session.LastSeen)
|
||||
Scan(&session.GameID, &session.Sound, &session.LastSeen, &session.Mode, &session.Version, &session.Position)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrScreenNotFound
|
||||
}
|
||||
@@ -58,7 +61,7 @@ func (r *ScreenSessionsRepo) Get(ctx context.Context, id int, tokenHash string)
|
||||
|
||||
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
|
||||
SELECT id, game_id, sound_enabled, last_seen_at, mode, state_version, position_ms
|
||||
FROM screen_sessions
|
||||
WHERE game_id = $1
|
||||
ORDER BY id`, gameID)
|
||||
@@ -70,7 +73,7 @@ func (r *ScreenSessionsRepo) ListByGame(ctx context.Context, gameID int) ([]*Scr
|
||||
var sessions []*ScreenSession
|
||||
for rows.Next() {
|
||||
session := &ScreenSession{}
|
||||
if err := rows.Scan(&session.ID, &session.GameID, &session.Sound, &session.LastSeen); err != nil {
|
||||
if err := rows.Scan(&session.ID, &session.GameID, &session.Sound, &session.LastSeen, &session.Mode, &session.Version, &session.Position); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sessions = append(sessions, session)
|
||||
@@ -78,6 +81,16 @@ func (r *ScreenSessionsRepo) ListByGame(ctx context.Context, gameID int) ([]*Scr
|
||||
return sessions, rows.Err()
|
||||
}
|
||||
|
||||
func (r *ScreenSessionsRepo) SetMode(ctx context.Context, gameID int, mode int32) error {
|
||||
_, err := r.pool.Exec(ctx, `UPDATE screen_sessions SET mode = $2, state_version = state_version + 1, updated_at = NOW() WHERE game_id = $1`, gameID, mode)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *ScreenSessionsRepo) SetPosition(ctx context.Context, gameID int, position int64) error {
|
||||
_, err := r.pool.Exec(ctx, `UPDATE screen_sessions SET position_ms = $2, state_version = state_version + 1, updated_at = NOW() WHERE game_id = $1`, gameID, position)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *ScreenSessionsRepo) Bind(ctx context.Context, id, gameID int, tokenHash string) error {
|
||||
result, err := r.pool.Exec(ctx, `
|
||||
UPDATE screen_sessions
|
||||
|
||||
Reference in New Issue
Block a user