RezinaLMS-BackEnd/internal/services/examprep/chapter_quizzes.go

61 lines
2.0 KiB
Go

package examprep
import (
"Rezina-Backend/internal/domain"
"context"
)
// ValidQuizParents deliberately excludes course and lesson attachments.
func ValidQuizParents(parents []domain.ExamPrepPracticeParent) bool {
return len(parents) == 1 && parents[0].Kind == domain.ExamPrepParentKindUnit && parents[0].ID > 0
}
func (s *Service) validateQuestionSetParents(ctx context.Context, id int64, parents []domain.ExamPrepPracticeParent) error {
set, err := s.store.GetQuestionSetByID(ctx, id)
if err != nil {
return s.validateQuestionSet(ctx, id)
}
if set.SetType == string(domain.QuestionSetTypeQuiz) {
if !ValidQuizParents(parents) {
return ErrInvalidExamPrepPracticeParent
}
return nil
}
return s.validateQuestionSet(ctx, id)
}
func (s *Service) CreateChapterQuiz(ctx context.Context, chapterID int64, input domain.CreateExamPrepPracticeInput) (domain.ExamPrepPractice, error) {
set, err := s.store.GetQuestionSetByID(ctx, input.QuestionSetID)
if err != nil {
return domain.ExamPrepPractice{}, ErrQuestionSetNotFound
}
if set.SetType != string(domain.QuestionSetTypeQuiz) {
return domain.ExamPrepPractice{}, ErrExamPrepQuizRequiresQuizSet
}
// A nested URL is authoritative; callers cannot redirect the quiz elsewhere.
parents := []domain.ExamPrepPracticeParent{{Kind: domain.ExamPrepParentKindUnit, ID: chapterID}}
if input.Parents != nil || input.ParentKind != "" || input.ParentID != 0 {
return domain.ExamPrepPractice{}, ErrInvalidExamPrepPracticeParent
}
if input.PublishStatus == "" {
input.PublishStatus = "DRAFT"
}
return s.createExamPrepPractice(ctx, parents, input)
}
func (s *Service) ListChapterQuizzes(ctx context.Context, chapterID int64, publishedOnly bool, limit, offset int32) ([]domain.ExamPrepPractice, int64, error) {
if err := s.ensureUnit(ctx, chapterID); err != nil {
return nil, 0, err
}
if limit <= 0 {
limit = 20
}
if limit > 200 {
limit = 200
}
if offset < 0 {
offset = 0
}
return s.store.ListChapterQuizzes(ctx, chapterID, publishedOnly, limit, offset)
}