package practices import ( "Yimaru-Backend/internal/domain" "Yimaru-Backend/internal/ports" "Yimaru-Backend/internal/services/courses" "Yimaru-Backend/internal/services/lessons" "Yimaru-Backend/internal/services/modules" "context" "errors" "github.com/jackc/pgx/v5" ) var ( ErrPracticeNotFound = errors.New("practice not found") ErrQuestionSetNotFound = errors.New("question set not found") ErrInvalidPracticeParent = errors.New("parent_kind and parent_id do not match an allowed parent") ErrPracticeParentLimitExceeded = errors.New("this course, module, or lesson already has a practice") ErrPracticeParentLinkNotFound = errors.New("practice is not linked to that parent") ) type Service struct { practices ports.LmsPracticeStore courses ports.CourseStore modules ports.ModuleStore lessons ports.LessonStore qs ports.QuestionSetByID personas ports.LmsPersonaReader } func NewService( practices ports.LmsPracticeStore, courses ports.CourseStore, modules ports.ModuleStore, lessons ports.LessonStore, qs ports.QuestionSetByID, personas ports.LmsPersonaReader, ) *Service { return &Service{ practices: practices, courses: courses, modules: modules, lessons: lessons, qs: qs, personas: personas, } } func (s *Service) validateQuestionSet(ctx context.Context, id int64) error { _, err := s.qs.GetQuestionSetByID(ctx, id) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return ErrQuestionSetNotFound } return err } return nil } func (s *Service) validatePersonaCatalog(ctx context.Context, id int64) error { if id <= 0 { return domain.ErrPersonaNotFound } _, err := s.personas.GetLmsPersonaByID(ctx, id) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return domain.ErrPersonaNotFound } return err } return nil } func (s *Service) validateParent(ctx context.Context, parent domain.PracticeParent) error { switch parent.Kind { case domain.ParentKindCourse: if _, e := s.courses.GetCourseByID(ctx, parent.ID); e != nil { if errors.Is(e, pgx.ErrNoRows) { return courses.ErrCourseNotFound } return e } case domain.ParentKindModule: if _, e := s.modules.GetModuleByID(ctx, parent.ID); e != nil { if errors.Is(e, pgx.ErrNoRows) { return modules.ErrModuleNotFound } return e } case domain.ParentKindLesson: if _, e := s.lessons.GetLessonByID(ctx, parent.ID); e != nil { if errors.Is(e, pgx.ErrNoRows) { return lessons.ErrLessonNotFound } return e } default: return ErrInvalidPracticeParent } return nil } func (s *Service) validateParentAvailability( ctx context.Context, parent domain.PracticeParent, excludePracticeID int64, ) error { if err := s.validateParent(ctx, parent); err != nil { return err } count, err := s.practices.CountLmsPracticesByParentExcludingPractice( ctx, parent.Kind, parent.ID, excludePracticeID, ) if err != nil { return err } if count > 0 { return ErrPracticeParentLimitExceeded } return nil } func (s *Service) validateParentList(ctx context.Context, parents []domain.PracticeParent, excludePracticeID int64) error { for _, parent := range parents { if err := s.validateParentAvailability(ctx, parent, excludePracticeID); err != nil { return err } } return nil } func (s *Service) Create(ctx context.Context, in domain.CreatePracticeInput) (domain.Practice, error) { if err := s.validateQuestionSet(ctx, in.QuestionSetID); err != nil { return domain.Practice{}, err } if in.PersonaID != nil { if err := s.validatePersonaCatalog(ctx, *in.PersonaID); err != nil { return domain.Practice{}, err } } parents := in.ResolvedParents() if len(parents) > 0 { if err := s.validateParentList(ctx, parents, 0); err != nil { return domain.Practice{}, err } } return s.practices.CreateLmsPractice(ctx, in, parents) } func (s *Service) TryGetByQuestionSetID(ctx context.Context, questionSetID int64) (domain.Practice, bool, error) { return s.practices.TryGetLmsPracticeByQuestionSetID(ctx, questionSetID) } func (s *Service) GetByID(ctx context.Context, id int64) (domain.Practice, error) { p, err := s.practices.GetLmsPracticeByID(ctx, id) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return domain.Practice{}, ErrPracticeNotFound } return domain.Practice{}, err } return p, nil } func clampPracticePage(limit, offset int32) (int32, int32) { if limit <= 0 { limit = 20 } if limit > 200 { limit = 200 } if offset < 0 { offset = 0 } return limit, offset } func (s *Service) ListByCourse(ctx context.Context, courseID int64, publishedOnly bool, limit, offset int32) ([]domain.Practice, int64, error) { if _, err := s.courses.GetCourseByID(ctx, courseID); err != nil { if errors.Is(err, pgx.ErrNoRows) { return nil, 0, courses.ErrCourseNotFound } return nil, 0, err } limit, offset = clampPracticePage(limit, offset) return s.practices.ListLmsPracticesByCourseID(ctx, courseID, publishedOnly, limit, offset) } func (s *Service) ListByModule(ctx context.Context, moduleID int64, publishedOnly bool, limit, offset int32) ([]domain.Practice, int64, error) { if _, err := s.modules.GetModuleByID(ctx, moduleID); err != nil { if errors.Is(err, pgx.ErrNoRows) { return nil, 0, modules.ErrModuleNotFound } return nil, 0, err } limit, offset = clampPracticePage(limit, offset) return s.practices.ListLmsPracticesByModuleID(ctx, moduleID, publishedOnly, limit, offset) } func (s *Service) ListByLesson(ctx context.Context, lessonID int64, publishedOnly bool, limit, offset int32) ([]domain.Practice, int64, error) { if _, err := s.lessons.GetLessonByID(ctx, lessonID); err != nil { if errors.Is(err, pgx.ErrNoRows) { return nil, 0, lessons.ErrLessonNotFound } return nil, 0, err } limit, offset = clampPracticePage(limit, offset) return s.practices.ListLmsPracticesByLessonID(ctx, lessonID, publishedOnly, limit, offset) } func (s *Service) ListAll(ctx context.Context, publishedOnly, unlinkedOnly bool, limit, offset int32) ([]domain.Practice, int64, error) { limit, offset = clampPracticePage(limit, offset) return s.practices.ListAllLmsPractices(ctx, publishedOnly, unlinkedOnly, limit, offset) } func (s *Service) Update(ctx context.Context, id int64, input domain.UpdatePracticeInput) (domain.Practice, error) { if input.QuestionSetID != nil { if err := s.validateQuestionSet(ctx, *input.QuestionSetID); err != nil { return domain.Practice{}, err } } if input.PersonaID != nil { if err := s.validatePersonaCatalog(ctx, *input.PersonaID); err != nil { return domain.Practice{}, err } } p, err := s.practices.UpdateLmsPractice(ctx, id, input) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return domain.Practice{}, ErrPracticeNotFound } return domain.Practice{}, err } return p, nil } // UpdateParents replaces all course/module/lesson links for a practice. func (s *Service) UpdateParents(ctx context.Context, id int64, in domain.UpdatePracticeParentsInput) (domain.Practice, error) { if _, err := s.practices.GetLmsPracticeByID(ctx, id); err != nil { if errors.Is(err, pgx.ErrNoRows) { return domain.Practice{}, ErrPracticeNotFound } return domain.Practice{}, err } parents := domain.DedupePracticeParents(in.Parents) if len(parents) > 0 { if err := s.validateParentList(ctx, parents, id); err != nil { return domain.Practice{}, err } } if err := s.practices.ReplaceLmsPracticeParents(ctx, id, parents); err != nil { return domain.Practice{}, err } return s.practices.GetLmsPracticeByID(ctx, id) } // UnlinkParent removes a single course/module/lesson link from a practice. func (s *Service) UnlinkParent(ctx context.Context, id int64, parent domain.PracticeParent) (domain.Practice, error) { kind, ok := domain.ParsePracticeParentKind(string(parent.Kind)) if !ok || parent.ID <= 0 { return domain.Practice{}, ErrInvalidPracticeParent } parent.Kind = kind if _, err := s.practices.GetLmsPracticeByID(ctx, id); err != nil { if errors.Is(err, pgx.ErrNoRows) { return domain.Practice{}, ErrPracticeNotFound } return domain.Practice{}, err } deleted, err := s.practices.DeleteLmsPracticeParent(ctx, id, parent) if err != nil { return domain.Practice{}, err } if !deleted { return domain.Practice{}, ErrPracticeParentLinkNotFound } return s.practices.GetLmsPracticeByID(ctx, id) } func (s *Service) Delete(ctx context.Context, id int64) error { if _, err := s.practices.GetLmsPracticeByID(ctx, id); err != nil { if errors.Is(err, pgx.ErrNoRows) { return ErrPracticeNotFound } return err } return s.practices.DeleteLmsPractice(ctx, id) }