diff --git a/cmd/evening_detective_server/main.go b/cmd/evening_detective_server/main.go index 51e0a16..c450618 100644 --- a/cmd/evening_detective_server/main.go +++ b/cmd/evening_detective_server/main.go @@ -31,6 +31,7 @@ import ( "evening_detective_server/internal/services/game_review_service" "evening_detective_server/internal/services/game_service" "evening_detective_server/internal/services/gameplay_service" + "evening_detective_server/internal/services/map_service" "evening_detective_server/internal/services/mcp_service" "evening_detective_server/internal/services/participation_service" "evening_detective_server/internal/services/scenarios_service" @@ -139,7 +140,7 @@ func main() { answersRepo := team_answers_repo.NewTeamAnswersRepo(dbpool) screenRepo := screen_sessions_repo.NewScreenSessionsRepo(dbpool) screenService := screen_service.NewScreenService(screenRepo) - mapRepo := game_map_repo.New(dbpool) + mapService := map_service.NewMapService(game_map_repo.NewGameMapRepo(dbpool)) reviewsRepo := game_reviews_repo.New(dbpool) gameService := game_service.NewGameService( gameRepo, @@ -220,7 +221,7 @@ func main() { ticketService, participationService, screenService, - mapRepo, + mapService, ), ) diff --git a/internal/app/game_map.go b/internal/app/game_map.go index ea5e2cd..ac7cf2d 100644 --- a/internal/app/game_map.go +++ b/internal/app/game_map.go @@ -4,7 +4,7 @@ import ( "context" "errors" - "evening_detective_server/internal/repos/game_map_repo" + "evening_detective_server/internal/services/map_service" proto "evening_detective_server/proto" "google.golang.org/grpc/codes" @@ -22,14 +22,14 @@ func (s *server) GetGameMap(ctx context.Context, req *proto.GetGameMapReq) (*pro if err != nil { return nil, status.Error(codes.NotFound, "игра не найдена") } - if err := s.mapRepo.Ensure(ctx, int(req.GameID), "organizer_desk", 1); err != nil { + if err := s.mapService.Ensure(ctx, int(req.GameID), "organizer_desk", 1); err != nil { return nil, status.Error(codes.Internal, "не удалось сохранить карту") } for _, team := range game.Teams { if team.ParticipationStatus == "cancelled" { continue } - if err := s.mapRepo.Ensure(ctx, int(req.GameID), "team", team.ID); err != nil { + if err := s.mapService.Ensure(ctx, int(req.GameID), "team", team.ID); err != nil { return nil, status.Error(codes.Internal, "не удалось сохранить карту") } } @@ -38,11 +38,11 @@ func (s *server) GetGameMap(ctx context.Context, req *proto.GetGameMapReq) (*pro return nil, status.Error(codes.Internal, "не удалось получить экраны") } for _, screen := range screens { - if err := s.mapRepo.Ensure(ctx, int(req.GameID), "screen", screen.ID); err != nil { + if err := s.mapService.Ensure(ctx, int(req.GameID), "screen", screen.ID); err != nil { return nil, status.Error(codes.Internal, "не удалось сохранить карту") } } - items, err := s.mapRepo.List(ctx, int(req.GameID)) + items, err := s.mapService.List(ctx, int(req.GameID)) if err != nil { return nil, status.Error(codes.Internal, "не удалось получить карту") } @@ -61,7 +61,7 @@ func (s *server) GetGameMap(ctx context.Context, req *proto.GetGameMapReq) (*pro for _, item := range items { _, teamExists := teamNames[item.ItemID] if (item.Kind == "team" && !teamExists) || (item.Kind == "screen" && screenStates[item.ItemID] == nil) { - if err := s.mapRepo.Delete(ctx, item); err != nil { + if err := s.mapService.Delete(ctx, item); err != nil { return nil, status.Error(codes.Internal, "не удалось обновить карту") } continue @@ -90,8 +90,15 @@ func (s *server) UpdateGameMapItem(ctx context.Context, req *proto.UpdateGameMap if req == nil || req.GameID <= 0 || req.ItemID <= 0 || (req.Kind != "team" && req.Kind != "screen" && req.Kind != "organizer_desk") { return nil, status.Error(codes.InvalidArgument, "некорректный объект карты") } - if err := s.mapRepo.Update(ctx, game_map_repo.Item{GameID: int(req.GameID), Kind: req.Kind, ItemID: int(req.ItemID), X: int(req.X), Y: int(req.Y), Hidden: req.Hidden}); err != nil { - if errors.Is(err, game_map_repo.ErrItemNotFound) { + if err := s.mapService.Update(ctx, &map_service.MapItem{ + GameID: int(req.GameID), + Kind: req.Kind, + ItemID: int(req.ItemID), + X: int(req.X), + Y: int(req.Y), + Hidden: req.Hidden, + }); err != nil { + if errors.Is(err, map_service.ErrItemNotFound) { return nil, status.Error(codes.NotFound, "объект карты не найден") } return nil, status.Error(codes.Internal, "не удалось изменить карту") diff --git a/internal/app/server.go b/internal/app/server.go index bc411e0..5de3a78 100644 --- a/internal/app/server.go +++ b/internal/app/server.go @@ -1,12 +1,12 @@ package app import ( - "evening_detective_server/internal/repos/game_map_repo" "evening_detective_server/internal/services/answer_service" "evening_detective_server/internal/services/file_service" "evening_detective_server/internal/services/game_review_service" "evening_detective_server/internal/services/game_service" "evening_detective_server/internal/services/gameplay_service" + "evening_detective_server/internal/services/map_service" "evening_detective_server/internal/services/participation_service" "evening_detective_server/internal/services/scenarios_service" "evening_detective_server/internal/services/screen_service" @@ -32,7 +32,7 @@ type server struct { ticketService *ticket_service.Service participationService *participation_service.ParticipationService screenService *screen_service.ScreenService - mapRepo *game_map_repo.Repo + mapService *map_service.MapService } func NewServer( @@ -48,7 +48,7 @@ func NewServer( ticketService *ticket_service.Service, participationService *participation_service.ParticipationService, screenService *screen_service.ScreenService, - mapRepo *game_map_repo.Repo, + mapService *map_service.MapService, ) proto.EveningDetectiveServerServer { return &server{ usersService: usersService, @@ -63,6 +63,6 @@ func NewServer( ticketService: ticketService, participationService: participationService, screenService: screenService, - mapRepo: mapRepo, + mapService: mapService, } } diff --git a/internal/repos/game_map_repo/repo.go b/internal/repos/game_map_repo/repo.go index a1177aa..1d95c37 100644 --- a/internal/repos/game_map_repo/repo.go +++ b/internal/repos/game_map_repo/repo.go @@ -25,8 +25,8 @@ type Repo struct { pool *pgxpool.Pool } -// New создаёт repository объектов карты. -func New(pool *pgxpool.Pool) *Repo { +// NewGameMapRepo создаёт repository объектов карты. +func NewGameMapRepo(pool *pgxpool.Pool) *Repo { return &Repo{pool: pool} } diff --git a/internal/services/map_service/mapper.go b/internal/services/map_service/mapper.go new file mode 100644 index 0000000..15f55be --- /dev/null +++ b/internal/services/map_service/mapper.go @@ -0,0 +1,34 @@ +package map_service + +import "evening_detective_server/internal/repos/game_map_repo" + +type MapItem struct { + GameID int + Kind string + ItemID int + X int + Y int + Hidden bool +} + +func mapItem(item game_map_repo.Item) *MapItem { + return &MapItem{ + GameID: item.GameID, + Kind: item.Kind, + ItemID: item.ItemID, + X: item.X, + Y: item.Y, + Hidden: item.Hidden, + } +} + +func toRepoItem(item *MapItem) game_map_repo.Item { + return game_map_repo.Item{ + GameID: item.GameID, + Kind: item.Kind, + ItemID: item.ItemID, + X: item.X, + Y: item.Y, + Hidden: item.Hidden, + } +} diff --git a/internal/services/map_service/service.go b/internal/services/map_service/service.go new file mode 100644 index 0000000..b93ceb3 --- /dev/null +++ b/internal/services/map_service/service.go @@ -0,0 +1,48 @@ +package map_service + +import ( + "context" + "errors" + + "evening_detective_server/internal/repos/game_map_repo" +) + +var ErrItemNotFound = errors.New("объект карты не найден") + +type MapService struct { + repo *game_map_repo.Repo +} + +func NewMapService(repo *game_map_repo.Repo) *MapService { + return &MapService{repo: repo} +} + +func (s *MapService) Ensure(ctx context.Context, gameID int, kind string, itemID int) error { + return s.repo.Ensure(ctx, gameID, kind, itemID) +} + +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 +} + +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 +} + +func (s *MapService) Delete(ctx context.Context, item *MapItem) error { + return s.repo.Delete(ctx, toRepoItem(item)) +}