feat: allow multiple exam-prep practices per catalog course, unit, or lesson
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
208c653e02
commit
51c8cad698
|
|
@ -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);
|
||||||
|
|
@ -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;
|
||||||
|
|
@ -14,7 +14,6 @@ var ErrUnitNotFound = errors.New("exam prep unit not found")
|
||||||
var ErrModuleNotFound = errors.New("exam prep module not found")
|
var ErrModuleNotFound = errors.New("exam prep module not found")
|
||||||
var ErrLessonNotFound = errors.New("exam prep lesson not found")
|
var ErrLessonNotFound = errors.New("exam prep lesson not found")
|
||||||
var ErrPracticeNotFound = errors.New("exam prep practice 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).
|
// examPrepStore is implemented by *repository.Store (catalog courses, units, modules, lessons, practices, personas).
|
||||||
type examPrepStore interface {
|
type examPrepStore interface {
|
||||||
|
|
@ -364,21 +363,7 @@ func (s *Service) ensureLesson(ctx context.Context, lessonID int64) error {
|
||||||
return nil
|
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) {
|
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 input.PersonaID != nil {
|
||||||
if err := s.ensurePersonaRef(ctx, *input.PersonaID); err != nil {
|
if err := s.ensurePersonaRef(ctx, *input.PersonaID); err != nil {
|
||||||
return domain.ExamPrepPractice{}, err
|
return domain.ExamPrepPractice{}, err
|
||||||
|
|
|
||||||
|
|
@ -221,11 +221,6 @@ func (h *Handler) mapCreateExamPrepPracticeError(c *fiber.Ctx, err error) error
|
||||||
Message: "Persona not found",
|
Message: "Persona not found",
|
||||||
Error: err.Error(),
|
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:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user