add questions

This commit is contained in:
2026-09-01 01:01:37 +07:00
parent 4431a4d491
commit 69666ee264
23 changed files with 4151 additions and 368 deletions
+86 -1
View File
@@ -295,6 +295,75 @@ func (s *ScenarioService) UpdateScenarioIntro(
return s.updateStory(ctx, id, story)
}
// AddScenarioQuestion добавляет вопрос дела в конец списка вопросов сценария.
func (s *ScenarioService) AddScenarioQuestion(
ctx context.Context,
id int,
question *storytelling.Question,
actorId int,
isAdmin bool,
) error {
if _, err := s.getScenarioForChange(ctx, id, actorId, isAdmin); err != nil {
return err
}
story, err := s.getStory(ctx, id)
if err != nil {
return err
}
story.Questions = append(story.Questions, question)
return s.updateStory(ctx, id, story)
}
// UpdateScenarioQuestion обновляет вопрос дела по его коду. Смена кода
// оставляет строки team_answers старого кода в БД
func (s *ScenarioService) UpdateScenarioQuestion(
ctx context.Context,
id int,
code string,
question *storytelling.Question,
actorId int,
isAdmin bool,
) error {
if _, err := s.getScenarioForChange(ctx, id, actorId, isAdmin); err != nil {
return err
}
story, err := s.getStory(ctx, id)
if err != nil {
return err
}
for i := range story.Questions {
if story.Questions[i].Code == code {
story.Questions[i] = question
break
}
}
return s.updateStory(ctx, id, story)
}
// DeleteScenarioQuestion удаляет вопрос дела по его коду.
func (s *ScenarioService) DeleteScenarioQuestion(
ctx context.Context,
id int,
code string,
actorId int,
isAdmin bool,
) error {
if _, err := s.getScenarioForChange(ctx, id, actorId, isAdmin); err != nil {
return err
}
story, err := s.getStory(ctx, id)
if err != nil {
return err
}
for i := range story.Questions {
if story.Questions[i].Code == code {
story.Questions = append(story.Questions[:i], story.Questions[i+1:]...)
break
}
}
return s.updateStory(ctx, id, story)
}
func (s *ScenarioService) getStory(ctx context.Context, id int) (*storytelling.Story, error) {
storyString, err := s.scenariosRepo.GetStoryByScenarioID(ctx, id)
if err != nil {
@@ -432,7 +501,6 @@ func (s *ScenarioService) UploadArchive(
return id, nil
}
func (s *ScenarioService) updateStory(ctx context.Context, id int, story *storytelling.Story) error {
for _, place := range story.Places {
place.Image = strings.TrimPrefix(place.Image, s.domain)
@@ -478,6 +546,23 @@ func normalizeStory(story *storytelling.Story) (string, error) {
}
story.Places = cleanPlaces
// Коды вопросов уникальны, пустые коды отбрасываются (как у точек).
questionCodes := map[string]struct{}{}
for _, question := range story.Questions {
questionCodes[question.Code] = struct{}{}
}
if len(questionCodes) != len(story.Questions) {
return "", errors.New("Такой код вопроса уже существует")
}
cleanQuestions := make([]*storytelling.Question, 0, len(story.Questions))
for _, question := range story.Questions {
if question.Code == "" {
continue
}
cleanQuestions = append(cleanQuestions, question)
}
story.Questions = cleanQuestions
return convertStory(story)
}