generated from VLADIMIR/template
add map
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package game_map_repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
GameID int
|
||||
Kind string
|
||||
ItemID int
|
||||
X int
|
||||
Y int
|
||||
Hidden bool
|
||||
}
|
||||
|
||||
type Repo struct{
|
||||
pool *pgxpool.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 {
|
||||
_, err := r.pool.Exec(ctx, `
|
||||
INSERT INTO game_map_items (game_id, kind, item_id)
|
||||
VALUES ($1, $2, $3) ON CONFLICT (game_id, kind, item_id) DO NOTHING`, gameID, kind, itemID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repo) List(ctx context.Context, gameID int) ([]Item, error) {
|
||||
rows, err := r.pool.Query(ctx, `SELECT game_id, kind, item_id, x, y, hidden FROM game_map_items WHERE game_id = $1 ORDER BY kind, item_id`, gameID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Item
|
||||
for rows.Next() {
|
||||
var item Item
|
||||
if err := rows.Scan(&item.GameID, &item.Kind, &item.ItemID, &item.X, &item.Y, &item.Hidden); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
return items, rows.Err()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user