This commit is contained in:
2026-10-01 15:39:38 +07:00
parent bc45fc67b1
commit cd49b9164b
+8
View File
@@ -7,8 +7,10 @@ import (
"github.com/jackc/pgx/v5/pgxpool" "github.com/jackc/pgx/v5/pgxpool"
) )
// ErrItemNotFound означает, что объект карты не найден.
var ErrItemNotFound = errors.New("объект карты не найден") var ErrItemNotFound = errors.New("объект карты не найден")
// Item — объект на карте игры.
type Item struct { type Item struct {
GameID int GameID int
Kind string Kind string
@@ -18,14 +20,17 @@ type Item struct {
Hidden bool Hidden bool
} }
// Repo работает с объектами карт в PostgreSQL.
type Repo struct { type Repo struct {
pool *pgxpool.Pool pool *pgxpool.Pool
} }
// New создаёт repository объектов карты.
func New(pool *pgxpool.Pool) *Repo { func New(pool *pgxpool.Pool) *Repo {
return &Repo{pool: pool} return &Repo{pool: pool}
} }
// Ensure добавляет объект карты, если он ещё не существует.
func (r *Repo) Ensure(ctx context.Context, gameID int, kind string, itemID int) error { func (r *Repo) Ensure(ctx context.Context, gameID int, kind string, itemID int) error {
_, err := r.pool.Exec(ctx, ` _, err := r.pool.Exec(ctx, `
INSERT INTO game_map_items (game_id, kind, item_id) INSERT INTO game_map_items (game_id, kind, item_id)
@@ -38,6 +43,7 @@ func (r *Repo) Ensure(ctx context.Context, gameID int, kind string, itemID int)
return err return err
} }
// List возвращает объекты карты игры.
func (r *Repo) List(ctx context.Context, gameID int) ([]Item, error) { func (r *Repo) List(ctx context.Context, gameID int) ([]Item, error) {
rows, err := r.pool.Query( rows, err := r.pool.Query(
ctx, ctx,
@@ -68,6 +74,7 @@ func (r *Repo) List(ctx context.Context, gameID int) ([]Item, error) {
return items, rows.Err() return items, rows.Err()
} }
// Update изменяет координаты и видимость объекта карты.
func (r *Repo) Update(ctx context.Context, item Item) error { func (r *Repo) Update(ctx context.Context, item Item) error {
result, err := r.pool.Exec( result, err := r.pool.Exec(
ctx, ctx,
@@ -95,6 +102,7 @@ func (r *Repo) Update(ctx context.Context, item Item) error {
return nil return nil
} }
// Delete удаляет объект карты.
func (r *Repo) Delete(ctx context.Context, item Item) error { func (r *Repo) Delete(ctx context.Context, item Item) error {
_, err := r.pool.Exec( _, err := r.pool.Exec(
ctx, ctx,