From 7aa8a50e30d96376d0a48d2988057d94412de9d9 Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Tue, 28 Jul 2026 22:00:00 +0700 Subject: [PATCH] add last action delete --- api/main.proto | 3 --- .../main.swagger.json | 5 ---- internal/repos/actions_repo/repo.go | 27 +++++++++++++++++++ internal/repos/applications_repo/repo.go | 23 ++++++++++++++++ internal/services/game_service/service.go | 27 ++++++++++++++++++- proto/main.pb.go | 10 +++---- 6 files changed, 80 insertions(+), 15 deletions(-) diff --git a/api/main.proto b/api/main.proto index 72994ed..039fb0c 100644 --- a/api/main.proto +++ b/api/main.proto @@ -451,9 +451,6 @@ service EveningDetectiveServer { delete: "/api/teams/{id}/last-actions" }; option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = { - security: { - security_requirement: {} - } tags : "Ходы"; summary: "Удалить последний ход"; }; diff --git a/cmd/evening_detective_server/main.swagger.json b/cmd/evening_detective_server/main.swagger.json index 830611e..6f05216 100644 --- a/cmd/evening_detective_server/main.swagger.json +++ b/cmd/evening_detective_server/main.swagger.json @@ -1016,11 +1016,6 @@ ], "tags": [ "Ходы" - ], - "security": [ - { - "": [] - } ] } }, diff --git a/internal/repos/actions_repo/repo.go b/internal/repos/actions_repo/repo.go index a0234d5..39eb955 100644 --- a/internal/repos/actions_repo/repo.go +++ b/internal/repos/actions_repo/repo.go @@ -2,10 +2,15 @@ package actions_repo import ( "context" + "errors" "github.com/jackc/pgx/v5/pgxpool" ) +var ( + ErrActionsNotFound = errors.New("Ход не найден") +) + type ActionsRepo struct { pool *pgxpool.Pool } @@ -101,3 +106,25 @@ func (r *ActionsRepo) GetActionsCountByTeamIDs(ctx context.Context, teamIds []in return res, nil } + +func (r *ActionsRepo) DeleteLastActionByTeamId(ctx context.Context, teamId int) error { + tag, err := r.pool.Exec( + ctx, + `DELETE FROM actions + WHERE id IN ( + SELECT id + FROM actions + WHERE team_id = $1 + ORDER BY created_at DESC + LIMIT 1 + )`, + teamId, + ) + if err != nil { + return err + } + if tag.RowsAffected() == 0 { + return ErrActionsNotFound + } + return nil +} diff --git a/internal/repos/applications_repo/repo.go b/internal/repos/applications_repo/repo.go index 137d80e..b74a3a4 100644 --- a/internal/repos/applications_repo/repo.go +++ b/internal/repos/applications_repo/repo.go @@ -110,3 +110,26 @@ func (r *ApplicationsRepo) UpdateApplicationStatus( } return nil } + +func (r *ApplicationsRepo) DeleteApplicationByTeamIdAndName(ctx context.Context, teamId int, name string) error { + tag, err := r.pool.Exec( + ctx, + `DELETE FROM applications + WHERE id IN ( + SELECT id + FROM applications + WHERE team_id = $1 and name = $2 + ORDER BY created_at DESC + LIMIT 1 + )`, + teamId, + name, + ) + if err != nil { + return err + } + if tag.RowsAffected() == 0 { + return ErrApplicationNotFound + } + return nil +} diff --git a/internal/services/game_service/service.go b/internal/services/game_service/service.go index dfa5e33..b520470 100644 --- a/internal/services/game_service/service.go +++ b/internal/services/game_service/service.go @@ -233,5 +233,30 @@ func (s *GameService) AddTeamAction(ctx context.Context, teamId int, password st } func (s *GameService) DeleteLastTeamAction(ctx context.Context, teamId int) error { - panic("unimplemented") + team, err := s.teamsRepo.GetTeamByID(ctx, teamId) + if err != nil { + return err + } + + actions, err := s.actionsRepo.GetActionsByTeamID(ctx, teamId) + if err != nil { + return err + } + + scenario, err := s.scenarioService.GetFullScenarioByID(ctx, team.ScenarioId) + if err != nil { + return err + } + + teamStory := s.storyteller.GetStory(scenario.Story, actions) + + lastPlace := teamStory.Places[len(teamStory.Places)-1] + + for _, application := range lastPlace.Applications { + if err := s.applicationsRepo.DeleteApplicationByTeamIdAndName(ctx, teamId, application.Name); err != nil { + return err + } + } + + return s.actionsRepo.DeleteLastActionByTeamId(ctx, teamId) } diff --git a/proto/main.pb.go b/proto/main.pb.go index 25b01e4..538c82e 100644 --- a/proto/main.pb.go +++ b/proto/main.pb.go @@ -4440,7 +4440,7 @@ const file_main_proto_rawDesc = "" + "\x17DeleteLastTeamActionReq\x12\x0e\n" + "\x02id\x18\x01 \x01(\x03R\x02id\"/\n" + "\x17DeleteLastTeamActionRsp\x12\x14\n" + - "\x05error\x18\x01 \x01(\tR\x05error2\x80>\n" + + "\x05error\x18\x01 \x01(\tR\x05error2\xf8=\n" + "\x16EveningDetectiveServer\x12\xa6\x01\n" + "\x04Ping\x12'.crabs.evening_detective_server.PingReq\x1a'.crabs.evening_detective_server.PingRsp\"L\x92A3\x12)Проверить доступностьb\x06\n" + "\x04\n" + @@ -4540,11 +4540,9 @@ const file_main_proto_rawDesc = "" + "\rAddTeamAction\x120.crabs.evening_detective_server.AddTeamActionReq\x1a0.crabs.evening_detective_server.AddTeamActionRsp\"N\x92A)\n" + "\bХоды\x12\x15Сделать ходb\x06\n" + "\x04\n" + - "\x00\x12\x00\x82\xd3\xe4\x93\x02\x1c:\x01*\"\x17/api/teams/{id}/actions\x12\xed\x01\n" + - "\x14DeleteLastTeamAction\x127.crabs.evening_detective_server.DeleteLastTeamActionReq\x1a7.crabs.evening_detective_server.DeleteLastTeamActionRsp\"c\x92A<\n" + - "\bХоды\x12(Удалить последний ходb\x06\n" + - "\x04\n" + - "\x00\x12\x00\x82\xd3\xe4\x93\x02\x1e*\x1c/api/teams/{id}/last-actionsB\xd7\x01\x92A\xc8\x01\x12U\n" + + "\x00\x12\x00\x82\xd3\xe4\x93\x02\x1c:\x01*\"\x17/api/teams/{id}/actions\x12\xe5\x01\n" + + "\x14DeleteLastTeamAction\x127.crabs.evening_detective_server.DeleteLastTeamActionReq\x1a7.crabs.evening_detective_server.DeleteLastTeamActionRsp\"[\x92A4\n" + + "\bХоды\x12(Удалить последний ход\x82\xd3\xe4\x93\x02\x1e*\x1c/api/teams/{id}/last-actionsB\xd7\x01\x92A\xc8\x01\x12U\n" + "KДокументация сервиса \"Вечерний детектив\"2\x06v0.1.0Z]\n" + "[\n" + "\n" +