generated from VLADIMIR/template
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package map_service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"evening_detective_server/internal/repos/game_map_repo"
|
|
)
|
|
|
|
// ErrItemNotFound indicates that a map item was not found.
|
|
var ErrItemNotFound = errors.New("объект карты не найден")
|
|
|
|
// MapService handles game map items.
|
|
type MapService struct {
|
|
repo *game_map_repo.Repo
|
|
}
|
|
|
|
// NewMapService creates a map service.
|
|
func NewMapService(repo *game_map_repo.Repo) *MapService {
|
|
return &MapService{repo: repo}
|
|
}
|
|
|
|
// Ensure creates a map item if it does not exist.
|
|
func (s *MapService) Ensure(ctx context.Context, gameID int, kind string, itemID int) error {
|
|
return s.repo.Ensure(ctx, gameID, kind, itemID)
|
|
}
|
|
|
|
// List returns map items for a game.
|
|
func (s *MapService) List(ctx context.Context, gameID int) ([]*MapItem, error) {
|
|
items, err := s.repo.List(ctx, gameID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]*MapItem, 0, len(items))
|
|
for _, item := range items {
|
|
result = append(result, mapItem(item))
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// Update changes a map item's position and visibility.
|
|
func (s *MapService) Update(ctx context.Context, item *MapItem) error {
|
|
if err := s.repo.Update(ctx, toRepoItem(item)); err != nil {
|
|
if errors.Is(err, game_map_repo.ErrItemNotFound) {
|
|
return ErrItemNotFound
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Delete removes a map item.
|
|
func (s *MapService) Delete(ctx context.Context, item *MapItem) error {
|
|
return s.repo.Delete(ctx, toRepoItem(item))
|
|
}
|