add last action delete

This commit is contained in:
2026-07-28 22:00:00 +07:00
parent 5e3584e31a
commit 7aa8a50e30
6 changed files with 80 additions and 15 deletions
+27
View File
@@ -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
}
+23
View File
@@ -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
}
+26 -1
View File
@@ -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)
}