From 51c8cad6980793348d95befa62c25d0e8d217428 Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Fri, 26 Jun 2026 02:30:34 -0700 Subject: [PATCH] feat: allow multiple exam-prep practices per catalog course, unit, or lesson Co-authored-by: Cursor --- ...am_prep_multiple_practices_per_parent.down.sql | 10 ++++++++++ ...exam_prep_multiple_practices_per_parent.up.sql | 3 +++ internal/services/examprep/service.go | 15 --------------- .../handlers/exam_prep_practice_handler.go | 5 ----- 4 files changed, 13 insertions(+), 20 deletions(-) create mode 100644 db/migrations/000094_exam_prep_multiple_practices_per_parent.down.sql create mode 100644 db/migrations/000094_exam_prep_multiple_practices_per_parent.up.sql diff --git a/db/migrations/000094_exam_prep_multiple_practices_per_parent.down.sql b/db/migrations/000094_exam_prep_multiple_practices_per_parent.down.sql new file mode 100644 index 00000000..94887199 --- /dev/null +++ b/db/migrations/000094_exam_prep_multiple_practices_per_parent.down.sql @@ -0,0 +1,10 @@ +-- Restore one practice per exam-prep parent (keep earliest link per parent). +DELETE FROM exam_prep.practice_parents a + USING exam_prep.practice_parents b +WHERE + a.parent_kind = b.parent_kind + AND a.parent_id = b.parent_id + AND a.id > b.id; + +ALTER TABLE exam_prep.practice_parents + ADD CONSTRAINT practice_parents_parent_kind_parent_id_key UNIQUE (parent_kind, parent_id); diff --git a/db/migrations/000094_exam_prep_multiple_practices_per_parent.up.sql b/db/migrations/000094_exam_prep_multiple_practices_per_parent.up.sql new file mode 100644 index 00000000..cae4c5f2 --- /dev/null +++ b/db/migrations/000094_exam_prep_multiple_practices_per_parent.up.sql @@ -0,0 +1,3 @@ +-- Exam-prep: allow multiple practices per catalog course, unit, or lesson. +ALTER TABLE exam_prep.practice_parents + DROP CONSTRAINT practice_parents_parent_kind_parent_id_key; diff --git a/internal/services/examprep/service.go b/internal/services/examprep/service.go index 32d7d816..6b9d34da 100644 --- a/internal/services/examprep/service.go +++ b/internal/services/examprep/service.go @@ -14,7 +14,6 @@ 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 ErrExamPrepPracticeParentLimitExceeded = errors.New("this catalog course, unit, or lesson already has a practice") // examPrepStore is implemented by *repository.Store (catalog courses, units, modules, lessons, practices, personas). type examPrepStore interface { @@ -364,21 +363,7 @@ func (s *Service) ensureLesson(ctx context.Context, lessonID int64) error { return nil } -func (s *Service) ensureExamPrepPracticeParentAvailable(ctx context.Context, parent domain.ExamPrepPracticeParent) error { - count, err := s.store.CountExamPrepPracticesByParent(ctx, parent.Kind, parent.ID) - if err != nil { - return err - } - if count > 0 { - return ErrExamPrepPracticeParentLimitExceeded - } - return nil -} - func (s *Service) createExamPrepPractice(ctx context.Context, parent domain.ExamPrepPracticeParent, input domain.CreateExamPrepPracticeInput) (domain.ExamPrepPractice, error) { - if err := s.ensureExamPrepPracticeParentAvailable(ctx, parent); err != nil { - return domain.ExamPrepPractice{}, err - } if input.PersonaID != nil { if err := s.ensurePersonaRef(ctx, *input.PersonaID); err != nil { return domain.ExamPrepPractice{}, err diff --git a/internal/web_server/handlers/exam_prep_practice_handler.go b/internal/web_server/handlers/exam_prep_practice_handler.go index aa445668..250ea11d 100644 --- a/internal/web_server/handlers/exam_prep_practice_handler.go +++ b/internal/web_server/handlers/exam_prep_practice_handler.go @@ -221,11 +221,6 @@ func (h *Handler) mapCreateExamPrepPracticeError(c *fiber.Ctx, err error) error Message: "Persona not found", Error: err.Error(), }) - case errors.Is(err, examprep.ErrExamPrepPracticeParentLimitExceeded): - return c.Status(fiber.StatusConflict).JSON(domain.ErrorResponse{ - Message: "This parent already has a practice", - Error: err.Error(), - }) default: return nil }