RezinaLMS-BackEnd/internal/services/practices/service.go
2026-06-18 05:02:13 -07:00

274 lines
7.9 KiB
Go

package practices
import (
"Yimaru-Backend/internal/domain"
"Yimaru-Backend/internal/ports"
"Yimaru-Backend/internal/repository"
"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")
)
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) validateParentList(ctx context.Context, parents []domain.PracticeParent) error {
for _, parent := range parents {
if err := s.validateParent(ctx, parent); err != nil {
return err
}
}
return nil
}
func (s *Service) ensureParentsAvailable(ctx context.Context, parents []domain.PracticeParent, excludePracticeID int64) error {
for _, parent := range parents {
var count int64
var err error
if excludePracticeID > 0 {
count, err = s.practices.CountLmsPracticesByParentExcludingPractice(ctx, parent.Kind, parent.ID, excludePracticeID)
} else {
count, err = s.practices.CountLmsPracticesByParent(ctx, parent.Kind, parent.ID)
}
if err != nil {
return err
}
if count >= 1 {
return ErrPracticeParentLimitExceeded
}
}
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); err != nil {
return domain.Practice{}, err
}
if err := s.ensureParentsAvailable(ctx, parents, 0); err != nil {
return domain.Practice{}, err
}
}
p, err := s.practices.CreateLmsPractice(ctx, in, parents)
if err != nil && repository.IsUniqueViolation(err) {
return domain.Practice{}, ErrPracticeParentLimitExceeded
}
return p, err
}
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) 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); err != nil {
return domain.Practice{}, err
}
if err := s.ensureParentsAvailable(ctx, parents, id); err != nil {
return domain.Practice{}, err
}
}
if err := s.practices.ReplaceLmsPracticeParents(ctx, id, parents); err != nil {
if repository.IsUniqueViolation(err) {
return domain.Practice{}, ErrPracticeParentLimitExceeded
}
return domain.Practice{}, err
}
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)
}