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

718 lines
24 KiB
Go

package examprep
import (
"Rezina-Backend/internal/domain"
"Rezina-Backend/internal/ports"
"context"
"errors"
"github.com/jackc/pgx/v5"
)
var ErrCatalogCourseNotFound = errors.New("exam prep catalog course not found")
var ErrUnitNotFound = errors.New("exam prep unit not found")
var ErrModuleNotFound = errors.New("exam prep module not found")
var ErrLessonNotFound = errors.New("exam prep lesson not found")
var ErrPracticeNotFound = errors.New("exam prep practice not found")
var ErrQuestionSetNotFound = errors.New("question set not found")
var ErrExamPrepQuizRequiresQuizSet = errors.New("chapter quizzes require a QUIZ question set")
var ErrExamPrepPracticeRequiresPracticeSet = errors.New("exam-prep practice must use a PRACTICE question set")
var ErrExamPrepAssessmentRequiresExamSet = errors.New("exam-prep assessment must use an EXAM question set")
var ErrQuestionSetNotExamPrepAssessment = errors.New("question set is not linked as an exam-prep assessment")
var ErrInvalidExamPrepAssessmentParent = errors.New("parent_kind and parent_id do not match an allowed exam prep parent")
var ErrInvalidExamPrepPracticeParent = errors.New("parent_kind and parent_id do not match an allowed exam prep parent")
var ErrExamPrepPracticeParentLinkNotFound = errors.New("practice is not linked to that parent")
// examPrepStore is implemented by *repository.Store (catalog courses, units, modules, lessons, practices, personas).
type examPrepStore interface {
PublicCatalogCourses(context.Context, string, int32, int32) (domain.PublicCatalogPage, error)
PublicCatalogCourse(context.Context, int64) (domain.PublicCourseOutline, error)
ListChapterQuizzes(ctx context.Context, chapterID int64, publishedOnly bool, limit, offset int32) ([]domain.ExamPrepPractice, int64, error)
GetChapterLessonContainer(ctx context.Context, chapterID int64) (int64, error)
ports.ExamPrepCatalogCourseStore
ports.ExamPrepUnitStore
ports.ExamPrepModuleStore
ports.ExamPrepLessonStore
ports.ExamPrepPracticeStore
ports.ExamPrepAssessmentStore
ports.LmsPersonaReader
ports.QuestionSetByID
}
type Service struct {
store examPrepStore
}
func NewService(store examPrepStore) *Service {
return &Service{store: store}
}
func (s *Service) ensurePersonaRef(ctx context.Context, id int64) error {
if id <= 0 {
return domain.ErrPersonaNotFound
}
_, err := s.store.GetLmsPersonaByID(ctx, id)
if errors.Is(err, pgx.ErrNoRows) {
return domain.ErrPersonaNotFound
}
return err
}
func (s *Service) CreateCatalogCourse(ctx context.Context, input domain.CreateExamPrepCatalogCourseInput) (domain.ExamPrepCatalogCourse, error) {
input.Category = "EDUCATION"
return s.store.CreateExamPrepCatalogCourse(ctx, input)
}
func (s *Service) GetCatalogCourseByID(ctx context.Context, id int64) (domain.ExamPrepCatalogCourse, error) {
c, err := s.store.GetExamPrepCatalogCourseByID(ctx, id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return domain.ExamPrepCatalogCourse{}, ErrCatalogCourseNotFound
}
return domain.ExamPrepCatalogCourse{}, err
}
return c, nil
}
func (s *Service) ListCatalogCourses(ctx context.Context, publishedOnly bool, limit, offset int32) ([]domain.ExamPrepCatalogCourse, int64, error) {
if limit <= 0 {
limit = 20
}
if limit > 200 {
limit = 200
}
if offset < 0 {
offset = 0
}
return s.store.ListExamPrepCatalogCourses(ctx, publishedOnly, limit, offset)
}
func (s *Service) UpdateCatalogCourse(ctx context.Context, id int64, input domain.UpdateExamPrepCatalogCourseInput) (domain.ExamPrepCatalogCourse, error) {
c, err := s.store.UpdateExamPrepCatalogCourse(ctx, id, input)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return domain.ExamPrepCatalogCourse{}, ErrCatalogCourseNotFound
}
return domain.ExamPrepCatalogCourse{}, err
}
return c, nil
}
func (s *Service) DeleteCatalogCourse(ctx context.Context, id int64) error {
if _, err := s.store.GetExamPrepCatalogCourseByID(ctx, id); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrCatalogCourseNotFound
}
return err
}
return s.store.DeleteExamPrepCatalogCourse(ctx, id)
}
func (s *Service) ReorderCatalogCourses(ctx context.Context, ordered []int64) error {
expected, err := s.store.ListAllExamPrepCatalogCourseIDs(ctx)
if err != nil {
return err
}
if err := domain.ValidateReorderPermutation(ordered, expected); err != nil {
return err
}
if len(ordered) == 0 {
return nil
}
return s.store.ReorderExamPrepCatalogCourses(ctx, ordered)
}
func (s *Service) ensureCatalogCourse(ctx context.Context, catalogCourseID int64) error {
if _, err := s.store.GetExamPrepCatalogCourseByID(ctx, catalogCourseID); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrCatalogCourseNotFound
}
return err
}
return nil
}
func (s *Service) CreateUnit(ctx context.Context, catalogCourseID int64, input domain.CreateExamPrepUnitInput) (domain.ExamPrepUnit, error) {
if err := s.ensureCatalogCourse(ctx, catalogCourseID); err != nil {
return domain.ExamPrepUnit{}, err
}
return s.store.CreateExamPrepUnit(ctx, catalogCourseID, input)
}
func (s *Service) ListUnitsByCatalogCourse(ctx context.Context, catalogCourseID int64, publishedOnly bool, limit, offset int32) ([]domain.ExamPrepUnit, int64, error) {
if err := s.ensureCatalogCourse(ctx, catalogCourseID); err != nil {
return nil, 0, err
}
if limit <= 0 {
limit = 20
}
if limit > 200 {
limit = 200
}
if offset < 0 {
offset = 0
}
return s.store.ListExamPrepUnitsByCatalogCourse(ctx, catalogCourseID, publishedOnly, limit, offset)
}
func (s *Service) GetUnitByID(ctx context.Context, id int64) (domain.ExamPrepUnit, error) {
u, err := s.store.GetExamPrepUnitByID(ctx, id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return domain.ExamPrepUnit{}, ErrUnitNotFound
}
return domain.ExamPrepUnit{}, err
}
return u, nil
}
func (s *Service) UpdateUnit(ctx context.Context, id int64, input domain.UpdateExamPrepUnitInput) (domain.ExamPrepUnit, error) {
u, err := s.store.UpdateExamPrepUnit(ctx, id, input)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return domain.ExamPrepUnit{}, ErrUnitNotFound
}
return domain.ExamPrepUnit{}, err
}
return u, nil
}
func (s *Service) DeleteUnit(ctx context.Context, id int64) error {
if _, err := s.store.GetExamPrepUnitByID(ctx, id); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrUnitNotFound
}
return err
}
return s.store.DeleteExamPrepUnit(ctx, id)
}
func (s *Service) ReorderUnitsInCatalogCourse(ctx context.Context, catalogCourseID int64, ordered []int64) error {
if err := s.ensureCatalogCourse(ctx, catalogCourseID); err != nil {
return err
}
expected, err := s.store.ListExamPrepUnitIDsByCatalogCourse(ctx, catalogCourseID)
if err != nil {
return err
}
if err := domain.ValidateReorderPermutation(ordered, expected); err != nil {
return err
}
if len(ordered) == 0 {
return nil
}
return s.store.ReorderExamPrepUnitsInCatalogCourse(ctx, catalogCourseID, ordered)
}
func (s *Service) ensureUnit(ctx context.Context, unitID int64) error {
if _, err := s.store.GetExamPrepUnitByID(ctx, unitID); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrUnitNotFound
}
return err
}
return nil
}
func (s *Service) CreateModule(ctx context.Context, unitID int64, input domain.CreateExamPrepModuleInput) (domain.ExamPrepModule, error) {
if err := s.ensureUnit(ctx, unitID); err != nil {
return domain.ExamPrepModule{}, err
}
return s.store.CreateExamPrepUnitModule(ctx, unitID, input)
}
func (s *Service) ListModulesByUnit(ctx context.Context, unitID int64, publishedOnly bool, limit, offset int32) ([]domain.ExamPrepModule, int64, error) {
if err := s.ensureUnit(ctx, unitID); err != nil {
return nil, 0, err
}
if limit <= 0 {
limit = 20
}
if limit > 200 {
limit = 200
}
if offset < 0 {
offset = 0
}
return s.store.ListExamPrepUnitModulesByUnit(ctx, unitID, publishedOnly, limit, offset)
}
func (s *Service) GetModuleByID(ctx context.Context, id int64) (domain.ExamPrepModule, error) {
m, err := s.store.GetExamPrepUnitModuleByID(ctx, id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return domain.ExamPrepModule{}, ErrModuleNotFound
}
return domain.ExamPrepModule{}, err
}
return m, nil
}
func (s *Service) UpdateModule(ctx context.Context, id int64, input domain.UpdateExamPrepModuleInput) (domain.ExamPrepModule, error) {
m, err := s.store.UpdateExamPrepUnitModule(ctx, id, input)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return domain.ExamPrepModule{}, ErrModuleNotFound
}
return domain.ExamPrepModule{}, err
}
return m, nil
}
func (s *Service) DeleteModule(ctx context.Context, id int64) error {
if _, err := s.store.GetExamPrepUnitModuleByID(ctx, id); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrModuleNotFound
}
return err
}
return s.store.DeleteExamPrepUnitModule(ctx, id)
}
func (s *Service) ReorderModulesInUnit(ctx context.Context, unitID int64, ordered []int64) error {
if err := s.ensureUnit(ctx, unitID); err != nil {
return err
}
expected, err := s.store.ListExamPrepUnitModuleIDsByUnit(ctx, unitID)
if err != nil {
return err
}
if err := domain.ValidateReorderPermutation(ordered, expected); err != nil {
return err
}
if len(ordered) == 0 {
return nil
}
return s.store.ReorderExamPrepUnitModulesInUnit(ctx, unitID, ordered)
}
func (s *Service) ensureModule(ctx context.Context, unitModuleID int64) error {
if _, err := s.store.GetExamPrepUnitModuleByID(ctx, unitModuleID); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrModuleNotFound
}
return err
}
return nil
}
func (s *Service) CreateLesson(ctx context.Context, unitModuleID int64, input domain.CreateExamPrepLessonInput) (domain.ExamPrepLesson, error) {
if err := s.ensureModule(ctx, unitModuleID); err != nil {
return domain.ExamPrepLesson{}, err
}
return s.store.CreateExamPrepUnitModuleLesson(ctx, unitModuleID, input)
}
func (s *Service) ListLessonsByUnitModule(ctx context.Context, unitModuleID int64, publishedOnly bool, limit, offset int32) ([]domain.ExamPrepLesson, int64, error) {
if err := s.ensureModule(ctx, unitModuleID); err != nil {
return nil, 0, err
}
if limit <= 0 {
limit = 20
}
if limit > 200 {
limit = 200
}
if offset < 0 {
offset = 0
}
return s.store.ListExamPrepUnitModuleLessonsByUnitModuleID(ctx, unitModuleID, publishedOnly, limit, offset)
}
func (s *Service) GetLessonByID(ctx context.Context, id int64) (domain.ExamPrepLesson, error) {
l, err := s.store.GetExamPrepUnitModuleLessonByID(ctx, id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return domain.ExamPrepLesson{}, ErrLessonNotFound
}
return domain.ExamPrepLesson{}, err
}
return l, nil
}
func (s *Service) UpdateLesson(ctx context.Context, id int64, input domain.UpdateExamPrepLessonInput) (domain.ExamPrepLesson, error) {
l, err := s.store.UpdateExamPrepUnitModuleLesson(ctx, id, input)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return domain.ExamPrepLesson{}, ErrLessonNotFound
}
return domain.ExamPrepLesson{}, err
}
return l, nil
}
func (s *Service) DeleteLesson(ctx context.Context, id int64) error {
if _, err := s.store.GetExamPrepUnitModuleLessonByID(ctx, id); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrLessonNotFound
}
return err
}
return s.store.DeleteExamPrepUnitModuleLesson(ctx, id)
}
func (s *Service) ReorderLessonsInUnitModule(ctx context.Context, unitModuleID int64, ordered []int64) error {
if err := s.ensureModule(ctx, unitModuleID); err != nil {
return err
}
expected, err := s.store.ListExamPrepUnitModuleLessonIDsByUnitModule(ctx, unitModuleID)
if err != nil {
return err
}
if err := domain.ValidateReorderPermutation(ordered, expected); err != nil {
return err
}
if len(ordered) == 0 {
return nil
}
return s.store.ReorderExamPrepUnitModuleLessonsInUnitModule(ctx, unitModuleID, ordered)
}
func (s *Service) ensureLesson(ctx context.Context, lessonID int64) error {
if _, err := s.store.GetExamPrepUnitModuleLessonByID(ctx, lessonID); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrLessonNotFound
}
return err
}
return nil
}
func (s *Service) validateQuestionSet(ctx context.Context, id int64) error {
set, err := s.store.GetQuestionSetByID(ctx, id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrQuestionSetNotFound
}
return err
}
if set.SetType != string(domain.QuestionSetTypePractice) {
return ErrExamPrepPracticeRequiresPracticeSet
}
return nil
}
func (s *Service) validateExamPrepParent(ctx context.Context, parent domain.ExamPrepPracticeParent) error {
switch parent.Kind {
case domain.ExamPrepParentKindCatalogCourse:
return s.ensureCatalogCourse(ctx, parent.ID)
case domain.ExamPrepParentKindUnit:
return s.ensureUnit(ctx, parent.ID)
case domain.ExamPrepParentKindLesson:
return s.ensureLesson(ctx, parent.ID)
default:
return ErrInvalidExamPrepPracticeParent
}
}
func (s *Service) validateExamPrepParentList(ctx context.Context, parents []domain.ExamPrepPracticeParent) error {
for _, parent := range parents {
if err := s.validateExamPrepParent(ctx, parent); err != nil {
return err
}
}
return nil
}
func (s *Service) resolveNestedCreateParents(input domain.CreateExamPrepPracticeInput, urlParent domain.ExamPrepPracticeParent) []domain.ExamPrepPracticeParent {
if input.Parents != nil {
return input.ResolvedParents()
}
return []domain.ExamPrepPracticeParent{urlParent}
}
func (s *Service) createExamPrepPractice(ctx context.Context, parents []domain.ExamPrepPracticeParent, input domain.CreateExamPrepPracticeInput) (domain.ExamPrepPractice, error) {
if err := s.validateQuestionSetParents(ctx, input.QuestionSetID, parents); err != nil {
return domain.ExamPrepPractice{}, err
}
if input.PersonaID != nil {
if err := s.ensurePersonaRef(ctx, *input.PersonaID); err != nil {
return domain.ExamPrepPractice{}, err
}
}
if len(parents) > 0 {
if err := s.validateExamPrepParentList(ctx, parents); err != nil {
return domain.ExamPrepPractice{}, err
}
}
return s.store.CreateExamPrepPractice(ctx, input, parents)
}
func (s *Service) CreateExamPrepPracticeFromInput(ctx context.Context, input domain.CreateExamPrepPracticeInput) (domain.ExamPrepPractice, error) {
return s.createExamPrepPractice(ctx, input.ResolvedParents(), input)
}
func (s *Service) listExamPrepPractices(ctx context.Context, parent domain.ExamPrepPracticeParent, publishedOnly bool, limit, offset int32) ([]domain.ExamPrepPractice, int64, error) {
if limit > 200 {
limit = 200
}
if offset < 0 {
offset = 0
}
return s.store.ListExamPrepPracticesByParent(ctx, parent, publishedOnly, limit, offset)
}
func (s *Service) CreateExamPrepPractice(ctx context.Context, lessonID int64, input domain.CreateExamPrepPracticeInput) (domain.ExamPrepPractice, error) {
if err := s.ensureLesson(ctx, lessonID); err != nil {
return domain.ExamPrepPractice{}, err
}
parents := s.resolveNestedCreateParents(input, domain.ExamPrepPracticeParent{
Kind: domain.ExamPrepParentKindLesson,
ID: lessonID,
})
return s.createExamPrepPractice(ctx, parents, input)
}
func (s *Service) CreateExamPrepPracticeForCatalogCourse(ctx context.Context, catalogCourseID int64, input domain.CreateExamPrepPracticeInput) (domain.ExamPrepPractice, error) {
if err := s.ensureCatalogCourse(ctx, catalogCourseID); err != nil {
return domain.ExamPrepPractice{}, err
}
parents := s.resolveNestedCreateParents(input, domain.ExamPrepPracticeParent{
Kind: domain.ExamPrepParentKindCatalogCourse,
ID: catalogCourseID,
})
return s.createExamPrepPractice(ctx, parents, input)
}
func (s *Service) CreateExamPrepPracticeForUnit(ctx context.Context, unitID int64, input domain.CreateExamPrepPracticeInput) (domain.ExamPrepPractice, error) {
if err := s.ensureUnit(ctx, unitID); err != nil {
return domain.ExamPrepPractice{}, err
}
parents := s.resolveNestedCreateParents(input, domain.ExamPrepPracticeParent{
Kind: domain.ExamPrepParentKindUnit,
ID: unitID,
})
return s.createExamPrepPractice(ctx, parents, input)
}
func (s *Service) ListExamPrepPracticesByLesson(ctx context.Context, lessonID int64, publishedOnly bool, limit, offset int32) ([]domain.ExamPrepPractice, int64, error) {
if err := s.ensureLesson(ctx, lessonID); err != nil {
return nil, 0, err
}
return s.listExamPrepPractices(ctx, domain.ExamPrepPracticeParent{
Kind: domain.ExamPrepParentKindLesson,
ID: lessonID,
}, publishedOnly, limit, offset)
}
func (s *Service) ListExamPrepPracticesByCatalogCourse(ctx context.Context, catalogCourseID int64, publishedOnly bool, limit, offset int32) ([]domain.ExamPrepPractice, int64, error) {
if err := s.ensureCatalogCourse(ctx, catalogCourseID); err != nil {
return nil, 0, err
}
return s.listExamPrepPractices(ctx, domain.ExamPrepPracticeParent{
Kind: domain.ExamPrepParentKindCatalogCourse,
ID: catalogCourseID,
}, publishedOnly, limit, offset)
}
func (s *Service) ListExamPrepPracticesByUnit(ctx context.Context, unitID int64, publishedOnly bool, limit, offset int32) ([]domain.ExamPrepPractice, int64, error) {
if err := s.ensureUnit(ctx, unitID); err != nil {
return nil, 0, err
}
return s.listExamPrepPractices(ctx, domain.ExamPrepPracticeParent{
Kind: domain.ExamPrepParentKindUnit,
ID: unitID,
}, publishedOnly, limit, offset)
}
func (s *Service) ListAllExamPrepPractices(ctx context.Context, publishedOnly, unlinkedOnly bool, limit, offset int32) ([]domain.ExamPrepPractice, int64, error) {
if limit > 200 {
limit = 200
}
if offset < 0 {
offset = 0
}
return s.store.ListAllExamPrepPractices(ctx, publishedOnly, unlinkedOnly, limit, offset)
}
func (s *Service) UpdateExamPrepPracticeParents(ctx context.Context, id int64, in domain.UpdateExamPrepPracticeParentsInput) (domain.ExamPrepPractice, error) {
if _, err := s.store.GetExamPrepLessonPracticeByID(ctx, id); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return domain.ExamPrepPractice{}, ErrPracticeNotFound
}
return domain.ExamPrepPractice{}, err
}
parents := domain.DedupeExamPrepPracticeParents(in.Parents)
existing, err := s.GetExamPrepPracticeByID(ctx, id)
if err != nil {
return domain.ExamPrepPractice{}, err
}
if err := s.validateQuestionSetParents(ctx, existing.QuestionSetID, parents); err != nil {
return domain.ExamPrepPractice{}, err
}
if len(parents) > 0 {
if err := s.validateExamPrepParentList(ctx, parents); err != nil {
return domain.ExamPrepPractice{}, err
}
}
if err := s.store.ReplaceExamPrepPracticeParents(ctx, id, parents); err != nil {
return domain.ExamPrepPractice{}, err
}
return s.store.GetExamPrepLessonPracticeByID(ctx, id)
}
func (s *Service) UnlinkExamPrepPracticeParent(ctx context.Context, id int64, parent domain.ExamPrepPracticeParent) (domain.ExamPrepPractice, error) {
kind, ok := domain.ParseExamPrepParentKind(string(parent.Kind))
if !ok || parent.ID <= 0 {
return domain.ExamPrepPractice{}, ErrInvalidExamPrepPracticeParent
}
parent.Kind = kind
if _, err := s.store.GetExamPrepLessonPracticeByID(ctx, id); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return domain.ExamPrepPractice{}, ErrPracticeNotFound
}
return domain.ExamPrepPractice{}, err
}
deleted, err := s.store.DeleteExamPrepPracticeParent(ctx, id, parent)
if err != nil {
return domain.ExamPrepPractice{}, err
}
if !deleted {
return domain.ExamPrepPractice{}, ErrExamPrepPracticeParentLinkNotFound
}
return s.store.GetExamPrepLessonPracticeByID(ctx, id)
}
func (s *Service) GetExamPrepPracticeByID(ctx context.Context, id int64) (domain.ExamPrepPractice, error) {
p, err := s.store.GetExamPrepLessonPracticeByID(ctx, id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return domain.ExamPrepPractice{}, ErrPracticeNotFound
}
return domain.ExamPrepPractice{}, err
}
return p, nil
}
func (s *Service) TryGetExamPrepPracticeByQuestionSetID(ctx context.Context, questionSetID int64) (domain.ExamPrepPractice, bool, error) {
return s.store.TryGetExamPrepLessonPracticeByQuestionSetID(ctx, questionSetID)
}
func (s *Service) UpdateExamPrepPractice(ctx context.Context, id int64, input domain.UpdateExamPrepPracticeInput) (domain.ExamPrepPractice, error) {
if input.QuestionSetID != nil {
existing, err := s.GetExamPrepPracticeByID(ctx, id)
if err != nil {
return domain.ExamPrepPractice{}, err
}
currentSet, setErr := s.store.GetQuestionSetByID(ctx, existing.QuestionSetID)
if setErr != nil {
return domain.ExamPrepPractice{}, setErr
}
if currentSet.SetType == "QUIZ" {
nextSet, nextErr := s.store.GetQuestionSetByID(ctx, *input.QuestionSetID)
if nextErr != nil {
return domain.ExamPrepPractice{}, ErrQuestionSetNotFound
}
if nextSet.SetType != "QUIZ" {
return domain.ExamPrepPractice{}, ErrExamPrepQuizRequiresQuizSet
}
}
if err := s.validateQuestionSetParents(ctx, *input.QuestionSetID, domain.ExamPrepPracticeParentsDeref(existing.Parents)); err != nil {
return domain.ExamPrepPractice{}, err
}
}
if input.PersonaID != nil {
if err := s.ensurePersonaRef(ctx, *input.PersonaID); err != nil {
return domain.ExamPrepPractice{}, err
}
}
p, err := s.store.UpdateExamPrepLessonPractice(ctx, id, input)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return domain.ExamPrepPractice{}, ErrPracticeNotFound
}
return domain.ExamPrepPractice{}, err
}
return p, nil
}
func (s *Service) DeleteExamPrepPractice(ctx context.Context, id int64) error {
if _, err := s.store.GetExamPrepLessonPracticeByID(ctx, id); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrPracticeNotFound
}
return err
}
return s.store.DeleteExamPrepLessonPractice(ctx, id)
}
func (s *Service) validateAssessmentParent(ctx context.Context, parent domain.ExamPrepAssessmentParent) error {
if parent.ID <= 0 {
return ErrInvalidExamPrepAssessmentParent
}
switch parent.Kind {
case "CATALOG_COURSE":
return s.ensureCatalogCourse(ctx, parent.ID)
case "UNIT":
return s.ensureUnit(ctx, parent.ID)
case "MODULE":
return s.ensureModule(ctx, parent.ID)
case "LESSON":
return s.ensureLesson(ctx, parent.ID)
default:
return ErrInvalidExamPrepAssessmentParent
}
}
func (s *Service) LinkExamPrepAssessment(ctx context.Context, in domain.LinkExamPrepAssessmentInput) (domain.QuestionSet, error) {
set, err := s.store.GetQuestionSetByID(ctx, in.QuestionSetID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return domain.QuestionSet{}, ErrQuestionSetNotFound
}
return domain.QuestionSet{}, err
}
if set.SetType != string(domain.QuestionSetTypeExam) {
return domain.QuestionSet{}, ErrExamPrepAssessmentRequiresExamSet
}
parent := domain.ExamPrepAssessmentParent{Kind: in.ParentKind, ID: in.ParentID}
if err := s.validateAssessmentParent(ctx, parent); err != nil {
return domain.QuestionSet{}, err
}
if err := s.store.LinkExamPrepAssessment(ctx, in.QuestionSetID, parent); err != nil {
return domain.QuestionSet{}, err
}
return set, nil
}
func (s *Service) ListExamPrepAssessmentsByParent(ctx context.Context, parent domain.ExamPrepAssessmentParent) ([]domain.QuestionSet, error) {
if err := s.validateAssessmentParent(ctx, parent); err != nil {
return nil, err
}
return s.store.ListExamPrepAssessmentsByParent(ctx, parent)
}
func (s *Service) UnlinkExamPrepAssessment(ctx context.Context, questionSetID int64, parent domain.ExamPrepAssessmentParent) error {
if questionSetID <= 0 {
return ErrQuestionSetNotFound
}
if err := s.validateAssessmentParent(ctx, parent); err != nil {
return err
}
set, err := s.store.GetQuestionSetByID(ctx, questionSetID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrQuestionSetNotFound
}
return err
}
if set.SetType != string(domain.QuestionSetTypeExam) {
return ErrExamPrepAssessmentRequiresExamSet
}
deleted, err := s.store.UnlinkExamPrepAssessment(ctx, questionSetID, parent)
if err != nil {
return err
}
if !deleted {
return ErrQuestionSetNotExamPrepAssessment
}
return nil
}
func (s *Service) GetChapterLessonContainer(ctx context.Context, chapterID int64) (int64, error) {
if err := s.ensureUnit(ctx, chapterID); err != nil {
return 0, err
}
return s.store.GetChapterLessonContainer(ctx, chapterID)
}