unique quiz constraint
This commit is contained in:
@@ -199,6 +199,9 @@ func (s *QuizService) Create(ctx context.Context, actorID string, d QuizDraft) (
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.ensureTargetFree(ctx, in, ""); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id, err := s.quizzes.Create(ctx, in, actorID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -223,12 +226,34 @@ func (s *QuizService) Update(ctx context.Context, id string, d QuizDraft) (*mode
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.ensureTargetFree(ctx, in, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.quizzes.Update(ctx, id, in, !locked); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.GetAdmin(ctx, id)
|
||||
}
|
||||
|
||||
// ensureTargetFree refuses a second quiz on the same chapter, lecture or
|
||||
// conversation, naming the one already there so the admin can edit it
|
||||
// instead. Stand-alone quizzes are never limited. The unique index backs
|
||||
// this up against a race between two saves.
|
||||
func (s *QuizService) ensureTargetFree(ctx context.Context, in repository.QuizInput, selfID string) error {
|
||||
if in.TargetPath == nil {
|
||||
return nil
|
||||
}
|
||||
_, title, err := s.quizzes.QuizOnTarget(ctx, *in.TargetPath, selfID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if title != "" {
|
||||
return fmt.Errorf("%w — %q is already linked to %s. Edit that quiz, or delete it first",
|
||||
repository.ErrQuizTargetTaken, title, *in.TargetPath)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *QuizService) SetPublished(ctx context.Context, id string, published bool) (*model.Quiz, error) {
|
||||
if published {
|
||||
q, err := s.quizzes.Get(ctx, id, false)
|
||||
|
||||
Reference in New Issue
Block a user