This commit is contained in:
2026-09-10 04:18:49 +07:00
parent 8e9289a621
commit b8a9545b52
3 changed files with 18 additions and 6 deletions
+15 -6
View File
@@ -2,10 +2,13 @@ package game_map_repo
import (
"context"
"errors"
"github.com/jackc/pgx/v5/pgxpool"
)
var ErrItemNotFound = errors.New("объект карты не найден")
type Item struct {
GameID int
Kind string
@@ -15,12 +18,12 @@ type Item struct {
Hidden bool
}
type Repo struct{
pool *pgxpool.Pool
type Repo struct {
pool *pgxpool.Pool
}
func New(pool *pgxpool.Pool) *Repo {
return &Repo{pool: pool}
func New(pool *pgxpool.Pool) *Repo {
return &Repo{pool: pool}
}
func (r *Repo) Ensure(ctx context.Context, gameID int, kind string, itemID int) error {
@@ -48,6 +51,12 @@ func (r *Repo) List(ctx context.Context, gameID int) ([]Item, error) {
}
func (r *Repo) Update(ctx context.Context, item Item) error {
_, err := r.pool.Exec(ctx, `UPDATE game_map_items SET x = $4, y = $5, hidden = $6 WHERE game_id = $1 AND kind = $2 AND item_id = $3`, item.GameID, item.Kind, item.ItemID, item.X, item.Y, item.Hidden)
return err
result, err := r.pool.Exec(ctx, `UPDATE game_map_items SET x = $4, y = $5, hidden = $6 WHERE game_id = $1 AND kind = $2 AND item_id = $3`, item.GameID, item.Kind, item.ItemID, item.X, item.Y, item.Hidden)
if err != nil {
return err
}
if result.RowsAffected() == 0 {
return ErrItemNotFound
}
return nil
}