generated from VLADIMIR/template
add last action delete
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user