feat: allow LMS practices to link to multiple course/module/lesson parents
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
bc8d36b46f
commit
e67f666815
66
db/migrations/000089_lms_practice_parents.down.sql
Normal file
66
db/migrations/000089_lms_practice_parents.down.sql
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
ALTER TABLE lms_practices
|
||||
ADD COLUMN course_id BIGINT REFERENCES courses (id) ON DELETE CASCADE,
|
||||
ADD COLUMN module_id BIGINT REFERENCES modules (id) ON DELETE CASCADE,
|
||||
ADD COLUMN lesson_id BIGINT REFERENCES lessons (id) ON DELETE CASCADE;
|
||||
|
||||
UPDATE lms_practices lp
|
||||
SET course_id = lpp.parent_id
|
||||
FROM lms_practice_parents lpp
|
||||
WHERE lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'COURSE'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM lms_practice_parents x
|
||||
WHERE x.practice_id = lp.id
|
||||
AND x.parent_kind <> 'COURSE'
|
||||
);
|
||||
|
||||
UPDATE lms_practices lp
|
||||
SET module_id = lpp.parent_id
|
||||
FROM lms_practice_parents lpp
|
||||
WHERE lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'MODULE'
|
||||
AND lp.course_id IS NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM lms_practice_parents x
|
||||
WHERE x.practice_id = lp.id
|
||||
AND x.parent_kind NOT IN ('MODULE')
|
||||
);
|
||||
|
||||
UPDATE lms_practices lp
|
||||
SET lesson_id = lpp.parent_id
|
||||
FROM lms_practice_parents lpp
|
||||
WHERE lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'LESSON'
|
||||
AND lp.course_id IS NULL
|
||||
AND lp.module_id IS NULL;
|
||||
|
||||
ALTER TABLE lms_practices
|
||||
ADD CONSTRAINT lms_practices_one_parent CHECK (
|
||||
(course_id IS NOT NULL)::int
|
||||
+ (module_id IS NOT NULL)::int
|
||||
+ (lesson_id IS NOT NULL)::int
|
||||
= 1
|
||||
);
|
||||
|
||||
CREATE INDEX idx_lms_practices_course_id ON lms_practices (course_id);
|
||||
CREATE INDEX idx_lms_practices_module_id ON lms_practices (module_id);
|
||||
CREATE INDEX idx_lms_practices_lesson_id ON lms_practices (lesson_id);
|
||||
CREATE INDEX idx_lms_practices_course_created ON lms_practices (course_id, created_at DESC);
|
||||
CREATE INDEX idx_lms_practices_module_created ON lms_practices (module_id, created_at DESC);
|
||||
CREATE INDEX idx_lms_practices_lesson_created ON lms_practices (lesson_id, created_at DESC);
|
||||
|
||||
CREATE UNIQUE INDEX idx_lms_practices_one_per_course
|
||||
ON lms_practices (course_id)
|
||||
WHERE course_id IS NOT NULL;
|
||||
|
||||
CREATE UNIQUE INDEX idx_lms_practices_one_per_module
|
||||
ON lms_practices (module_id)
|
||||
WHERE module_id IS NOT NULL;
|
||||
|
||||
CREATE UNIQUE INDEX idx_lms_practices_one_per_lesson
|
||||
ON lms_practices (lesson_id)
|
||||
WHERE lesson_id IS NOT NULL;
|
||||
|
||||
DROP TABLE lms_practice_parents;
|
||||
46
db/migrations/000089_lms_practice_parents.up.sql
Normal file
46
db/migrations/000089_lms_practice_parents.up.sql
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
-- Practices can be linked to multiple courses, modules, and/or lessons via lms_practice_parents.
|
||||
CREATE TABLE lms_practice_parents (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
practice_id BIGINT NOT NULL REFERENCES lms_practices (id) ON DELETE CASCADE,
|
||||
parent_kind VARCHAR(30) NOT NULL CHECK (parent_kind IN ('COURSE', 'MODULE', 'LESSON')),
|
||||
parent_id BIGINT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE (practice_id, parent_kind, parent_id),
|
||||
UNIQUE (parent_kind, parent_id)
|
||||
);
|
||||
|
||||
INSERT INTO lms_practice_parents (practice_id, parent_kind, parent_id)
|
||||
SELECT id, 'COURSE', course_id
|
||||
FROM lms_practices
|
||||
WHERE course_id IS NOT NULL;
|
||||
|
||||
INSERT INTO lms_practice_parents (practice_id, parent_kind, parent_id)
|
||||
SELECT id, 'MODULE', module_id
|
||||
FROM lms_practices
|
||||
WHERE module_id IS NOT NULL;
|
||||
|
||||
INSERT INTO lms_practice_parents (practice_id, parent_kind, parent_id)
|
||||
SELECT id, 'LESSON', lesson_id
|
||||
FROM lms_practices
|
||||
WHERE lesson_id IS NOT NULL;
|
||||
|
||||
DROP INDEX IF EXISTS idx_lms_practices_one_per_course;
|
||||
DROP INDEX IF EXISTS idx_lms_practices_one_per_module;
|
||||
DROP INDEX IF EXISTS idx_lms_practices_one_per_lesson;
|
||||
DROP INDEX IF EXISTS idx_lms_practices_course_id;
|
||||
DROP INDEX IF EXISTS idx_lms_practices_module_id;
|
||||
DROP INDEX IF EXISTS idx_lms_practices_lesson_id;
|
||||
DROP INDEX IF EXISTS idx_lms_practices_course_created;
|
||||
DROP INDEX IF EXISTS idx_lms_practices_module_created;
|
||||
DROP INDEX IF EXISTS idx_lms_practices_lesson_created;
|
||||
|
||||
ALTER TABLE lms_practices
|
||||
DROP CONSTRAINT lms_practices_one_parent;
|
||||
|
||||
ALTER TABLE lms_practices
|
||||
DROP COLUMN course_id,
|
||||
DROP COLUMN module_id,
|
||||
DROP COLUMN lesson_id;
|
||||
|
||||
CREATE INDEX idx_lms_practice_parents_practice_id ON lms_practice_parents (practice_id);
|
||||
CREATE INDEX idx_lms_practice_parents_lookup ON lms_practice_parents (parent_kind, parent_id);
|
||||
|
|
@ -307,9 +307,9 @@ SELECT
|
|||
(SELECT COUNT(*)::bigint FROM lessons) AS lms_lessons,
|
||||
(SELECT COUNT(*)::bigint FROM lessons WHERE NULLIF(BTRIM(video_url), '') IS NOT NULL) AS lms_lessons_with_video,
|
||||
(SELECT COUNT(*)::bigint FROM lms_practices) AS lms_practices,
|
||||
(SELECT COUNT(*)::bigint FROM lms_practices WHERE course_id IS NOT NULL) AS lms_practices_at_course,
|
||||
(SELECT COUNT(*)::bigint FROM lms_practices WHERE module_id IS NOT NULL) AS lms_practices_at_module,
|
||||
(SELECT COUNT(*)::bigint FROM lms_practices WHERE lesson_id IS NOT NULL) AS lms_practices_at_lesson,
|
||||
(SELECT COUNT(*)::bigint FROM lms_practice_parents WHERE parent_kind = 'COURSE') AS lms_practices_at_course,
|
||||
(SELECT COUNT(*)::bigint FROM lms_practice_parents WHERE parent_kind = 'MODULE') AS lms_practices_at_module,
|
||||
(SELECT COUNT(*)::bigint FROM lms_practice_parents WHERE parent_kind = 'LESSON') AS lms_practices_at_lesson,
|
||||
(SELECT COUNT(*)::bigint FROM exam_prep.catalog_courses) AS exam_prep_catalog_courses,
|
||||
(SELECT COUNT(*)::bigint FROM exam_prep.units) AS exam_prep_units,
|
||||
(SELECT COUNT(*)::bigint FROM exam_prep.unit_modules) AS exam_prep_unit_modules,
|
||||
|
|
|
|||
|
|
@ -85,12 +85,13 @@ UNION ALL
|
|||
FROM
|
||||
user_practice_progress upp
|
||||
INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
|
||||
AND lp.lesson_id IS NOT NULL
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'LESSON'
|
||||
INNER JOIN question_sets qs ON qs.id = upp.question_set_id
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
INNER JOIN lessons l ON l.id = lp.lesson_id
|
||||
INNER JOIN lessons l ON l.id = lpp.parent_id
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
AND c.program_id = m.program_id
|
||||
|
|
@ -131,13 +132,13 @@ UNION ALL
|
|||
FROM
|
||||
user_practice_progress upp
|
||||
INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
|
||||
AND lp.module_id IS NOT NULL
|
||||
AND lp.lesson_id IS NULL
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'MODULE'
|
||||
INNER JOIN question_sets qs ON qs.id = upp.question_set_id
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
INNER JOIN modules m ON m.id = lp.module_id
|
||||
INNER JOIN modules m ON m.id = lpp.parent_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
AND c.program_id = m.program_id
|
||||
INNER JOIN programs p ON p.id = c.program_id
|
||||
|
|
@ -175,14 +176,13 @@ UNION ALL
|
|||
FROM
|
||||
user_practice_progress upp
|
||||
INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
|
||||
AND lp.course_id IS NOT NULL
|
||||
AND lp.module_id IS NULL
|
||||
AND lp.lesson_id IS NULL
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'COURSE'
|
||||
INNER JOIN question_sets qs ON qs.id = upp.question_set_id
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
INNER JOIN courses c ON c.id = lp.course_id
|
||||
INNER JOIN courses c ON c.id = lpp.parent_id
|
||||
INNER JOIN programs p ON p.id = c.program_id
|
||||
LEFT JOIN lms_user_program_progress prf ON prf.user_id = upp.user_id
|
||||
AND prf.program_id = p.id
|
||||
|
|
|
|||
|
|
@ -22,10 +22,10 @@ SELECT
|
|||
c.*,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM lms_practices p
|
||||
WHERE p.course_id = c.id
|
||||
AND p.module_id IS NULL
|
||||
AND p.lesson_id IS NULL
|
||||
FROM lms_practice_parents lpp
|
||||
INNER JOIN lms_practices p ON p.id = lpp.practice_id
|
||||
WHERE lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id = c.id
|
||||
AND p.publish_status = 'PUBLISHED'
|
||||
) AS has_practice
|
||||
FROM courses
|
||||
|
|
@ -84,24 +84,23 @@ SELECT
|
|||
WHERE
|
||||
m.course_id = c.id
|
||||
AND l.publish_status = 'PUBLISHED') AS lesson_count,
|
||||
-- Practices whose parent is the course only (lms_practices.course_id). Excludes
|
||||
-- practices linked via module_id or lesson_id, even for modules/lessons in this course.
|
||||
-- Practices whose parent is the course only. Excludes practices linked via module or lesson parents.
|
||||
(
|
||||
SELECT
|
||||
COUNT(*)::bigint
|
||||
FROM
|
||||
lms_practices p
|
||||
lms_practice_parents lpp
|
||||
INNER JOIN lms_practices p ON p.id = lpp.practice_id
|
||||
WHERE
|
||||
p.course_id = c.id
|
||||
AND p.module_id IS NULL
|
||||
AND p.lesson_id IS NULL
|
||||
lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id = c.id
|
||||
AND p.publish_status = 'PUBLISHED') AS practice_count,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM lms_practices p
|
||||
WHERE p.course_id = c.id
|
||||
AND p.module_id IS NULL
|
||||
AND p.lesson_id IS NULL
|
||||
FROM lms_practice_parents lpp
|
||||
INNER JOIN lms_practices p ON p.id = lpp.practice_id
|
||||
WHERE lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id = c.id
|
||||
AND p.publish_status = 'PUBLISHED'
|
||||
) AS has_practice
|
||||
FROM
|
||||
|
|
|
|||
|
|
@ -23,8 +23,10 @@ SELECT
|
|||
l.*,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM lms_practices p
|
||||
WHERE p.lesson_id = l.id
|
||||
FROM lms_practice_parents lpp
|
||||
INNER JOIN lms_practices p ON p.id = lpp.practice_id
|
||||
WHERE lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id = l.id
|
||||
AND p.publish_status = 'PUBLISHED'
|
||||
) AS has_practice
|
||||
FROM lessons
|
||||
|
|
@ -57,8 +59,10 @@ SELECT
|
|||
l.updated_at,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM lms_practices p
|
||||
WHERE p.lesson_id = l.id
|
||||
FROM lms_practice_parents lpp
|
||||
INNER JOIN lms_practices p ON p.id = lpp.practice_id
|
||||
WHERE lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id = l.id
|
||||
AND p.publish_status = 'PUBLISHED'
|
||||
) AS has_practice
|
||||
FROM
|
||||
|
|
|
|||
|
|
@ -23,9 +23,10 @@ SELECT
|
|||
m.*,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM lms_practices p
|
||||
WHERE p.module_id = m.id
|
||||
AND p.lesson_id IS NULL
|
||||
FROM lms_practice_parents lpp
|
||||
INNER JOIN lms_practices p ON p.id = lpp.practice_id
|
||||
WHERE lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id = m.id
|
||||
AND p.publish_status = 'PUBLISHED'
|
||||
) AS has_practice
|
||||
FROM modules
|
||||
|
|
@ -70,9 +71,10 @@ SELECT
|
|||
m.updated_at,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM lms_practices p
|
||||
WHERE p.module_id = m.id
|
||||
AND p.lesson_id IS NULL
|
||||
FROM lms_practice_parents lpp
|
||||
INNER JOIN lms_practices p ON p.id = lpp.practice_id
|
||||
WHERE lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id = m.id
|
||||
AND p.publish_status = 'PUBLISHED'
|
||||
) AS has_practice
|
||||
FROM
|
||||
|
|
|
|||
43
db/query/lms_practice_parents.sql
Normal file
43
db/query/lms_practice_parents.sql
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
-- name: InsertLmsPracticeParent :one
|
||||
INSERT INTO lms_practice_parents (practice_id, parent_kind, parent_id)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING *;
|
||||
|
||||
-- name: DeleteLmsPracticeParentsByPracticeID :exec
|
||||
DELETE FROM lms_practice_parents
|
||||
WHERE practice_id = $1;
|
||||
|
||||
-- name: ListLmsPracticeParentsByPracticeID :many
|
||||
SELECT *
|
||||
FROM lms_practice_parents
|
||||
WHERE practice_id = $1
|
||||
ORDER BY id ASC;
|
||||
|
||||
-- name: ListLmsPracticeParentsByPracticeIDs :many
|
||||
SELECT *
|
||||
FROM lms_practice_parents
|
||||
WHERE practice_id = ANY (sqlc.arg('practice_ids')::bigint[])
|
||||
ORDER BY practice_id ASC, id ASC;
|
||||
|
||||
-- name: CountLmsPracticesByParent :one
|
||||
SELECT COUNT(*)::bigint
|
||||
FROM lms_practice_parents
|
||||
WHERE parent_kind = $1
|
||||
AND parent_id = $2;
|
||||
|
||||
-- name: CountLmsPracticesByParentExcludingPractice :one
|
||||
SELECT COUNT(*)::bigint
|
||||
FROM lms_practice_parents
|
||||
WHERE parent_kind = $1
|
||||
AND parent_id = $2
|
||||
AND practice_id <> $3;
|
||||
|
||||
-- name: ListLmsPracticeParentsByQuestionSetID :many
|
||||
SELECT
|
||||
lpp.parent_kind,
|
||||
lpp.parent_id,
|
||||
lpp.practice_id
|
||||
FROM lms_practice_parents lpp
|
||||
INNER JOIN lms_practices lp ON lp.id = lpp.practice_id
|
||||
WHERE lp.question_set_id = $1
|
||||
ORDER BY lp.id DESC, lpp.id ASC;
|
||||
|
|
@ -1,8 +1,7 @@
|
|||
-- name: CreateLmsPractice :one
|
||||
INSERT INTO lms_practices (
|
||||
course_id, module_id, lesson_id,
|
||||
title, story_description, story_image, persona_id, question_set_id, quick_tips, publish_status
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetLmsPracticeByID :one
|
||||
|
|
@ -21,9 +20,6 @@ LIMIT 1;
|
|||
SELECT
|
||||
COUNT(*) OVER () AS total_count,
|
||||
p.id,
|
||||
p.course_id,
|
||||
p.module_id,
|
||||
p.lesson_id,
|
||||
p.title,
|
||||
p.story_description,
|
||||
p.story_image,
|
||||
|
|
@ -34,7 +30,9 @@ SELECT
|
|||
p.created_at,
|
||||
p.updated_at
|
||||
FROM lms_practices p
|
||||
WHERE p.course_id = $1
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = p.id
|
||||
WHERE lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id = $1
|
||||
AND (
|
||||
sqlc.arg('published_only')::boolean = FALSE
|
||||
OR p.publish_status = 'PUBLISHED'::TEXT
|
||||
|
|
@ -46,9 +44,6 @@ LIMIT $2 OFFSET $3;
|
|||
SELECT
|
||||
COUNT(*) OVER () AS total_count,
|
||||
p.id,
|
||||
p.course_id,
|
||||
p.module_id,
|
||||
p.lesson_id,
|
||||
p.title,
|
||||
p.story_description,
|
||||
p.story_image,
|
||||
|
|
@ -59,7 +54,9 @@ SELECT
|
|||
p.created_at,
|
||||
p.updated_at
|
||||
FROM lms_practices p
|
||||
WHERE p.module_id = $1
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = p.id
|
||||
WHERE lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id = $1
|
||||
AND (
|
||||
sqlc.arg('published_only')::boolean = FALSE
|
||||
OR p.publish_status = 'PUBLISHED'::TEXT
|
||||
|
|
@ -71,9 +68,6 @@ LIMIT $2 OFFSET $3;
|
|||
SELECT
|
||||
COUNT(*) OVER () AS total_count,
|
||||
p.id,
|
||||
p.course_id,
|
||||
p.module_id,
|
||||
p.lesson_id,
|
||||
p.title,
|
||||
p.story_description,
|
||||
p.story_image,
|
||||
|
|
@ -84,7 +78,9 @@ SELECT
|
|||
p.created_at,
|
||||
p.updated_at
|
||||
FROM lms_practices p
|
||||
WHERE p.lesson_id = $1
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = p.id
|
||||
WHERE lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id = $1
|
||||
AND (
|
||||
sqlc.arg('published_only')::boolean = FALSE
|
||||
OR p.publish_status = 'PUBLISHED'::TEXT
|
||||
|
|
@ -112,15 +108,18 @@ WHERE id = $1;
|
|||
|
||||
-- name: CountLmsPracticesByCourseID :one
|
||||
SELECT COUNT(*)::bigint
|
||||
FROM lms_practices
|
||||
WHERE course_id = $1;
|
||||
FROM lms_practice_parents
|
||||
WHERE parent_kind = 'COURSE'
|
||||
AND parent_id = $1;
|
||||
|
||||
-- name: CountLmsPracticesByModuleID :one
|
||||
SELECT COUNT(*)::bigint
|
||||
FROM lms_practices
|
||||
WHERE module_id = $1;
|
||||
FROM lms_practice_parents
|
||||
WHERE parent_kind = 'MODULE'
|
||||
AND parent_id = $1;
|
||||
|
||||
-- name: CountLmsPracticesByLessonID :one
|
||||
SELECT COUNT(*)::bigint
|
||||
FROM lms_practices
|
||||
WHERE lesson_id = $1;
|
||||
FROM lms_practice_parents
|
||||
WHERE parent_kind = 'LESSON'
|
||||
AND parent_id = $1;
|
||||
|
|
|
|||
|
|
@ -181,9 +181,11 @@ SELECT
|
|||
count(*)::int AS n
|
||||
FROM
|
||||
lms_practices lp
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
lp.lesson_id = $1
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id = $1
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED';
|
||||
|
|
@ -193,10 +195,12 @@ SELECT
|
|||
count(*)::int AS n
|
||||
FROM
|
||||
lms_practices lp
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
INNER JOIN user_practice_progress upp ON upp.question_set_id = lp.question_set_id
|
||||
WHERE
|
||||
lp.lesson_id = $1
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id = $1
|
||||
AND upp.user_id = $2
|
||||
AND upp.completed_at IS NOT NULL
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
|
|
@ -265,26 +269,27 @@ WHERE
|
|||
|
||||
-- name: ListLMSCompletedLessonIDsByUser :many
|
||||
SELECT
|
||||
lp.lesson_id
|
||||
lpp.parent_id AS lesson_id
|
||||
FROM
|
||||
lms_practices AS lp
|
||||
lms_practice_parents lpp
|
||||
INNER JOIN lms_practices AS lp ON lp.id = lpp.practice_id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
LEFT JOIN user_practice_progress upp ON upp.question_set_id = lp.question_set_id
|
||||
AND upp.user_id = $1
|
||||
AND upp.completed_at IS NOT NULL
|
||||
WHERE
|
||||
lp.lesson_id IS NOT NULL
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
GROUP BY
|
||||
lp.lesson_id
|
||||
lpp.parent_id
|
||||
HAVING
|
||||
count(DISTINCT lp.question_set_id) > 0
|
||||
AND count(DISTINCT upp.question_set_id) >= count(DISTINCT lp.question_set_id)
|
||||
ORDER BY
|
||||
max(upp.completed_at) ASC,
|
||||
lp.lesson_id ASC;
|
||||
lpp.parent_id ASC;
|
||||
|
||||
-- name: ListLMSCompletedModuleIDsByUser :many
|
||||
SELECT
|
||||
|
|
@ -295,15 +300,18 @@ FROM (
|
|||
lp.question_set_id
|
||||
FROM
|
||||
modules m
|
||||
INNER JOIN lms_practices lp ON (
|
||||
lp.module_id = m.id
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
lessons
|
||||
WHERE
|
||||
module_id = m.id))
|
||||
INNER JOIN lms_practice_parents lpp ON (
|
||||
(lpp.parent_kind = 'MODULE' AND lpp.parent_id = m.id)
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
lessons
|
||||
WHERE
|
||||
module_id = m.id)))
|
||||
INNER JOIN lms_practices lp ON lp.id = lpp.practice_id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
qs.set_type = 'PRACTICE'
|
||||
|
|
@ -330,23 +338,28 @@ FROM (
|
|||
lp.question_set_id
|
||||
FROM
|
||||
courses c
|
||||
INNER JOIN lms_practices lp ON (
|
||||
lp.course_id = c.id
|
||||
OR lp.module_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
modules
|
||||
WHERE
|
||||
course_id = c.id)
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = c.id))
|
||||
INNER JOIN lms_practice_parents lpp ON (
|
||||
(lpp.parent_kind = 'COURSE' AND lpp.parent_id = c.id)
|
||||
OR (
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
modules
|
||||
WHERE
|
||||
course_id = c.id))
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = c.id)))
|
||||
INNER JOIN lms_practices lp ON lp.id = lpp.practice_id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
qs.set_type = 'PRACTICE'
|
||||
|
|
@ -373,23 +386,28 @@ FROM (
|
|||
lp.question_set_id
|
||||
FROM
|
||||
courses c
|
||||
INNER JOIN lms_practices lp ON (
|
||||
lp.course_id = c.id
|
||||
OR lp.module_id IN (
|
||||
SELECT
|
||||
m.id
|
||||
FROM
|
||||
modules m
|
||||
WHERE
|
||||
m.course_id = c.id)
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = c.id))
|
||||
INNER JOIN lms_practice_parents lpp ON (
|
||||
(lpp.parent_kind = 'COURSE' AND lpp.parent_id = c.id)
|
||||
OR (
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
m.id
|
||||
FROM
|
||||
modules m
|
||||
WHERE
|
||||
m.course_id = c.id))
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = c.id)))
|
||||
INNER JOIN lms_practices lp ON lp.id = lpp.practice_id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
qs.set_type = 'PRACTICE'
|
||||
|
|
@ -463,16 +481,25 @@ FROM
|
|||
lms_practices lp
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
(
|
||||
lp.module_id = $1
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
lessons
|
||||
WHERE
|
||||
module_id = $1
|
||||
AND publish_status = 'PUBLISHED'))
|
||||
EXISTS (
|
||||
SELECT
|
||||
1
|
||||
FROM
|
||||
lms_practice_parents lpp
|
||||
WHERE
|
||||
lpp.practice_id = lp.id
|
||||
AND (
|
||||
(lpp.parent_kind = 'MODULE' AND lpp.parent_id = $1)
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
lessons
|
||||
WHERE
|
||||
module_id = $1
|
||||
AND publish_status = 'PUBLISHED'))))
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED';
|
||||
|
|
@ -485,16 +512,25 @@ FROM
|
|||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
INNER JOIN user_practice_progress upp ON upp.question_set_id = lp.question_set_id
|
||||
WHERE
|
||||
(
|
||||
lp.module_id = $1
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
lessons
|
||||
WHERE
|
||||
module_id = $1
|
||||
AND publish_status = 'PUBLISHED'))
|
||||
EXISTS (
|
||||
SELECT
|
||||
1
|
||||
FROM
|
||||
lms_practice_parents lpp
|
||||
WHERE
|
||||
lpp.practice_id = lp.id
|
||||
AND (
|
||||
(lpp.parent_kind = 'MODULE' AND lpp.parent_id = $1)
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
lessons
|
||||
WHERE
|
||||
module_id = $1
|
||||
AND publish_status = 'PUBLISHED'))))
|
||||
AND upp.user_id = $2
|
||||
AND upp.completed_at IS NOT NULL
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
|
|
@ -508,26 +544,37 @@ FROM
|
|||
lms_practices lp
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
(
|
||||
lp.course_id = $1
|
||||
OR lp.module_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
modules
|
||||
WHERE
|
||||
course_id = $1
|
||||
AND publish_status = 'PUBLISHED')
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'))
|
||||
EXISTS (
|
||||
SELECT
|
||||
1
|
||||
FROM
|
||||
lms_practice_parents lpp
|
||||
WHERE
|
||||
lpp.practice_id = lp.id
|
||||
AND (
|
||||
(lpp.parent_kind = 'COURSE' AND lpp.parent_id = $1)
|
||||
OR (
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
modules
|
||||
WHERE
|
||||
course_id = $1
|
||||
AND publish_status = 'PUBLISHED'))
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'))))
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED';
|
||||
|
|
@ -540,26 +587,37 @@ FROM
|
|||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
INNER JOIN user_practice_progress upp ON upp.question_set_id = lp.question_set_id
|
||||
WHERE
|
||||
(
|
||||
lp.course_id = $1
|
||||
OR lp.module_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
modules
|
||||
WHERE
|
||||
course_id = $1
|
||||
AND publish_status = 'PUBLISHED')
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'))
|
||||
EXISTS (
|
||||
SELECT
|
||||
1
|
||||
FROM
|
||||
lms_practice_parents lpp
|
||||
WHERE
|
||||
lpp.practice_id = lp.id
|
||||
AND (
|
||||
(lpp.parent_kind = 'COURSE' AND lpp.parent_id = $1)
|
||||
OR (
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
modules
|
||||
WHERE
|
||||
course_id = $1
|
||||
AND publish_status = 'PUBLISHED'))
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'))))
|
||||
AND upp.user_id = $2
|
||||
AND upp.completed_at IS NOT NULL
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
|
|
@ -573,37 +631,50 @@ FROM
|
|||
lms_practices lp
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
(
|
||||
lp.course_id IN (
|
||||
SELECT
|
||||
c.id
|
||||
FROM
|
||||
courses c
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND c.publish_status = 'PUBLISHED')
|
||||
OR lp.module_id IN (
|
||||
SELECT
|
||||
m.id
|
||||
FROM
|
||||
modules m
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED')
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED'))
|
||||
EXISTS (
|
||||
SELECT
|
||||
1
|
||||
FROM
|
||||
lms_practice_parents lpp
|
||||
WHERE
|
||||
lpp.practice_id = lp.id
|
||||
AND (
|
||||
(
|
||||
lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
c.id
|
||||
FROM
|
||||
courses c
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND c.publish_status = 'PUBLISHED'))
|
||||
OR (
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
m.id
|
||||
FROM
|
||||
modules m
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED'))
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED'))))
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED';
|
||||
|
|
@ -616,37 +687,50 @@ FROM
|
|||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
INNER JOIN user_practice_progress upp ON upp.question_set_id = lp.question_set_id
|
||||
WHERE
|
||||
(
|
||||
lp.course_id IN (
|
||||
SELECT
|
||||
c.id
|
||||
FROM
|
||||
courses c
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND c.publish_status = 'PUBLISHED')
|
||||
OR lp.module_id IN (
|
||||
SELECT
|
||||
m.id
|
||||
FROM
|
||||
modules m
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED')
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED'))
|
||||
EXISTS (
|
||||
SELECT
|
||||
1
|
||||
FROM
|
||||
lms_practice_parents lpp
|
||||
WHERE
|
||||
lpp.practice_id = lp.id
|
||||
AND (
|
||||
(
|
||||
lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
c.id
|
||||
FROM
|
||||
courses c
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND c.publish_status = 'PUBLISHED'))
|
||||
OR (
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
m.id
|
||||
FROM
|
||||
modules m
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED'))
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED'))))
|
||||
AND upp.user_id = $2
|
||||
AND upp.completed_at IS NOT NULL
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
|
|
@ -659,9 +743,11 @@ SELECT
|
|||
count(*)::int AS n
|
||||
FROM
|
||||
lms_practices lp
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
lp.module_id = $1
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id = $1
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED';
|
||||
|
|
@ -671,10 +757,12 @@ SELECT
|
|||
count(*)::int AS n
|
||||
FROM
|
||||
lms_practices lp
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
INNER JOIN user_practice_progress upp ON upp.question_set_id = lp.question_set_id
|
||||
WHERE
|
||||
lp.module_id = $1
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id = $1
|
||||
AND upp.user_id = $2
|
||||
AND upp.completed_at IS NOT NULL
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
|
|
@ -687,9 +775,11 @@ SELECT
|
|||
count(*)::int AS n
|
||||
FROM
|
||||
lms_practices lp
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
lp.course_id = $1
|
||||
lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id = $1
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED';
|
||||
|
|
@ -699,26 +789,14 @@ SELECT
|
|||
count(*)::int AS n
|
||||
FROM
|
||||
lms_practices lp
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
INNER JOIN user_practice_progress upp ON upp.question_set_id = lp.question_set_id
|
||||
WHERE
|
||||
lp.course_id = $1
|
||||
lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id = $1
|
||||
AND upp.user_id = $2
|
||||
AND upp.completed_at IS NOT NULL
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED';
|
||||
|
||||
-- name: GetPracticeScopeByQuestionSetID :one
|
||||
SELECT
|
||||
id,
|
||||
course_id,
|
||||
module_id,
|
||||
lesson_id
|
||||
FROM
|
||||
lms_practices
|
||||
WHERE
|
||||
question_set_id = $1
|
||||
ORDER BY
|
||||
id DESC
|
||||
LIMIT 1;
|
||||
|
|
|
|||
|
|
@ -10,9 +10,6 @@ WITH matched AS (
|
|||
p.persona_id,
|
||||
p.quick_tips,
|
||||
p.publish_status,
|
||||
p.course_id,
|
||||
p.module_id,
|
||||
p.lesson_id,
|
||||
NULL::bigint AS exam_prep_lesson_id,
|
||||
p.created_at,
|
||||
p.updated_at,
|
||||
|
|
@ -64,10 +61,7 @@ WITH matched AS (
|
|||
p.persona_id,
|
||||
p.quick_tips,
|
||||
p.publish_status,
|
||||
NULL::bigint AS course_id,
|
||||
NULL::bigint AS module_id,
|
||||
NULL::bigint AS lesson_id,
|
||||
p.unit_module_lesson_id AS exam_prep_lesson_id,
|
||||
NULL::bigint AS exam_prep_lesson_id,
|
||||
p.created_at,
|
||||
p.updated_at,
|
||||
(
|
||||
|
|
@ -117,9 +111,6 @@ SELECT
|
|||
persona_id,
|
||||
quick_tips,
|
||||
publish_status,
|
||||
course_id,
|
||||
module_id,
|
||||
lesson_id,
|
||||
exam_prep_lesson_id,
|
||||
created_at,
|
||||
updated_at,
|
||||
|
|
|
|||
|
|
@ -99,12 +99,13 @@ FROM (
|
|||
FROM
|
||||
user_practice_progress upp
|
||||
INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
|
||||
AND lp.lesson_id IS NOT NULL
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'LESSON'
|
||||
INNER JOIN question_sets qs ON qs.id = upp.question_set_id
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
INNER JOIN lessons l ON l.id = lp.lesson_id
|
||||
INNER JOIN lessons l ON l.id = lpp.parent_id
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
AND c.program_id = m.program_id
|
||||
|
|
@ -130,13 +131,13 @@ FROM (
|
|||
FROM
|
||||
user_practice_progress upp
|
||||
INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
|
||||
AND lp.module_id IS NOT NULL
|
||||
AND lp.lesson_id IS NULL
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'MODULE'
|
||||
INNER JOIN question_sets qs ON qs.id = upp.question_set_id
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
INNER JOIN modules m ON m.id = lp.module_id
|
||||
INNER JOIN modules m ON m.id = lpp.parent_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
AND c.program_id = m.program_id
|
||||
INNER JOIN programs p ON p.id = c.program_id
|
||||
|
|
@ -161,14 +162,13 @@ FROM (
|
|||
FROM
|
||||
user_practice_progress upp
|
||||
INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
|
||||
AND lp.course_id IS NOT NULL
|
||||
AND lp.module_id IS NULL
|
||||
AND lp.lesson_id IS NULL
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'COURSE'
|
||||
INNER JOIN question_sets qs ON qs.id = upp.question_set_id
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
INNER JOIN courses c ON c.id = lp.course_id
|
||||
INNER JOIN courses c ON c.id = lpp.parent_id
|
||||
INNER JOIN programs p ON p.id = c.program_id
|
||||
WHERE
|
||||
upp.user_id = $1
|
||||
|
|
|
|||
847
docs/practice-multi-parent-admin-panel-integration.md
Normal file
847
docs/practice-multi-parent-admin-panel-integration.md
Normal file
|
|
@ -0,0 +1,847 @@
|
|||
# Practice Multi-Parent Links — Admin Panel Integration Guide
|
||||
|
||||
This guide documents how to update the Yimaru **admin panel practice creation and editing** flow for **multi-parent LMS practices**. A single practice can now be linked to **multiple** courses, modules, lessons, or any combination of those hierarchy nodes.
|
||||
|
||||
> **Scope:** LMS (`/api/v1/practices`) only. **Exam-prep** practices are unchanged — they still attach to exactly one `unit_module_lesson_id`.
|
||||
>
|
||||
> **Related:** Question authoring inside a practice (sections, `associated_question_id`) is documented in [practice-question-associations-admin-panel-integration.md](./practice-question-associations-admin-panel-integration.md).
|
||||
|
||||
---
|
||||
|
||||
## Table of contents
|
||||
|
||||
1. [Feature overview](#feature-overview)
|
||||
2. [Breaking changes for the admin panel](#breaking-changes-for-the-admin-panel)
|
||||
3. [Concepts: parents, kinds, and constraints](#concepts-parents-kinds-and-constraints)
|
||||
4. [Architecture](#architecture)
|
||||
5. [Prerequisites (backend)](#prerequisites-backend)
|
||||
6. [Authentication & permissions](#authentication--permissions)
|
||||
7. [Data model](#data-model)
|
||||
8. [Response envelopes](#response-envelopes)
|
||||
9. [Create practice](#create-practice)
|
||||
10. [Update practice shell (including parents)](#update-practice-shell-including-parents)
|
||||
11. [Full practice load/save (questions)](#full-practice-loadsave-questions)
|
||||
12. [List & read endpoints](#list--read-endpoints)
|
||||
13. [Recommended admin UI changes](#recommended-admin-ui-changes)
|
||||
14. [Form state & serialization](#form-state--serialization)
|
||||
15. [Client-side validation](#client-side-validation)
|
||||
16. [TypeScript types & helpers](#typescript-types--helpers)
|
||||
17. [React integration patterns](#react-integration-patterns)
|
||||
18. [Error handling](#error-handling)
|
||||
19. [Integration checklist](#integration-checklist)
|
||||
20. [Limitations](#limitations)
|
||||
21. [Quick reference](#quick-reference)
|
||||
|
||||
---
|
||||
|
||||
## Feature overview
|
||||
|
||||
| Capability | Before | After |
|
||||
|------------|--------|-------|
|
||||
| Parents per LMS practice | Exactly one (`parent_kind` + `parent_id`) | **One or many** via `parents[]` |
|
||||
| Mix course + module + lesson on same practice | Not allowed | **Allowed** |
|
||||
| Same practice appears in multiple lesson catalogs | Not allowed | **Allowed** (one row, many parent links) |
|
||||
| Each course/module/lesson can own a practice | One practice max | **Still one practice max per entity** |
|
||||
| Exam-prep parent model | Single lesson | Unchanged |
|
||||
|
||||
### What problem this solves
|
||||
|
||||
Content teams often reuse the same practice shell (story, persona, question set) in more than one place — for example:
|
||||
|
||||
- The same capstone practice linked to **Module A** and **Module B**
|
||||
- A shared review practice attached to **Lesson 3** and directly to **Course 1**
|
||||
- One question set surfaced from both a **lesson** and a **module** entry point
|
||||
|
||||
Previously the admin had to duplicate practices (and question sets) to achieve this. Now one practice ID can be parented by multiple hierarchy nodes.
|
||||
|
||||
---
|
||||
|
||||
## Breaking changes for the admin panel
|
||||
|
||||
Update any code that reads or writes practice parent fields.
|
||||
|
||||
### Removed from `Practice` responses
|
||||
|
||||
| Old field | Replacement |
|
||||
|-----------|-------------|
|
||||
| `parent_kind` | `parents[].parent_kind` |
|
||||
| `parent_id` | `parents[].parent_id` |
|
||||
|
||||
### Create request — preferred shape
|
||||
|
||||
```json
|
||||
{
|
||||
"parents": [
|
||||
{ "parent_kind": "LESSON", "parent_id": 42 },
|
||||
{ "parent_kind": "COURSE", "parent_id": 7 }
|
||||
],
|
||||
"question_set_id": 100
|
||||
}
|
||||
```
|
||||
|
||||
### Create request — legacy (still supported)
|
||||
|
||||
If `parents` is omitted, the API accepts the old single-parent fields:
|
||||
|
||||
```json
|
||||
{
|
||||
"parent_kind": "LESSON",
|
||||
"parent_id": 42,
|
||||
"question_set_id": 100
|
||||
}
|
||||
```
|
||||
|
||||
### Where parents are **not** updated
|
||||
|
||||
`PUT /api/v1/practices/:id/full` updates story metadata, question set settings, and questions — **it does not accept `parents`**. Parent links are managed through:
|
||||
|
||||
- `POST /api/v1/practices` (create)
|
||||
- `PUT /api/v1/practices/:id` (shell update, including `parents`)
|
||||
|
||||
---
|
||||
|
||||
## Concepts: parents, kinds, and constraints
|
||||
|
||||
### `PracticeParent`
|
||||
|
||||
| Field | Type | Values |
|
||||
|-------|------|--------|
|
||||
| `parent_kind` | string | `COURSE` \| `MODULE` \| `LESSON` |
|
||||
| `parent_id` | number | ID of the course, module, or lesson row |
|
||||
|
||||
### Rules
|
||||
|
||||
1. **At least one parent** is required on create (via `parents` or legacy `parent_kind` + `parent_id`).
|
||||
2. **Duplicates are ignored** — sending the same `(parent_kind, parent_id)` twice is deduplicated server-side.
|
||||
3. **One practice per parent entity** — each course, module, or lesson can still be linked to **at most one** practice. Assigning a parent that is already taken returns **409 Conflict**.
|
||||
4. **A practice may have many parents** — the inverse is not limited.
|
||||
5. **Mixed kinds are allowed** — e.g. one `COURSE` parent and two `LESSON` parents on the same practice.
|
||||
6. **List endpoints filter by direct parent kind** — `GET /courses/:id/practices` returns practices with a `COURSE` parent equal to `:id`, not practices that only appear in the course via a lesson parent.
|
||||
|
||||
### Visual diagram
|
||||
|
||||
```mermaid
|
||||
flowchart TB
|
||||
subgraph PracticeP["Practice #55 (one question set)"]
|
||||
QS[Question set + questions]
|
||||
end
|
||||
|
||||
L12["Lesson 12"]
|
||||
L18["Lesson 18"]
|
||||
C7["Course 7"]
|
||||
|
||||
L12 -->|parent_kind LESSON| PracticeP
|
||||
L18 -->|parent_kind LESSON| PracticeP
|
||||
C7 -->|parent_kind COURSE| PracticeP
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Architecture
|
||||
|
||||
```mermaid
|
||||
flowchart TB
|
||||
subgraph AdminPanel["Admin panel"]
|
||||
CreateForm[Practice create form]
|
||||
ParentPicker[Multi-parent picker]
|
||||
ShellEdit[Practice settings tab]
|
||||
QuestionEditor[Question editor]
|
||||
end
|
||||
|
||||
subgraph API["Yimaru API"]
|
||||
PostPractice["POST /practices"]
|
||||
PutPractice["PUT /practices/:id"]
|
||||
GetFull["GET /practices/:id/full"]
|
||||
PutFull["PUT /practices/:id/full"]
|
||||
end
|
||||
|
||||
subgraph DB["PostgreSQL"]
|
||||
LP["lms_practices"]
|
||||
LPP["lms_practice_parents"]
|
||||
end
|
||||
|
||||
CreateForm --> ParentPicker
|
||||
ParentPicker --> PostPractice
|
||||
ShellEdit --> PutPractice
|
||||
QuestionEditor --> PutFull
|
||||
GetFull --> QuestionEditor
|
||||
PostPractice --> LP
|
||||
PostPractice --> LPP
|
||||
PutPractice --> LPP
|
||||
```
|
||||
|
||||
- **Migration:** `000089_lms_practice_parents` — junction table; migrates legacy columns; drops `course_id` / `module_id` / `lesson_id` from `lms_practices`.
|
||||
- **Domain:** `internal/domain/practice.go` — `PracticeParent`, `parents[]` on `Practice`.
|
||||
- **Service:** `internal/services/practices/service.go` — validates parents, enforces per-entity uniqueness.
|
||||
- **Repository:** `internal/repository/lms_practices.go` — transactional create + parent inserts; replace parents on update.
|
||||
|
||||
---
|
||||
|
||||
## Prerequisites (backend)
|
||||
|
||||
Before testing from the admin panel:
|
||||
|
||||
1. Run database migrations (includes `000089`):
|
||||
|
||||
```bash
|
||||
docker compose up migrate
|
||||
```
|
||||
|
||||
2. Redeploy or restart the API on a build that includes multi-parent support.
|
||||
|
||||
3. Confirm the junction table exists:
|
||||
|
||||
```sql
|
||||
\d lms_practice_parents
|
||||
```
|
||||
|
||||
4. Confirm legacy columns are gone from `lms_practices`:
|
||||
|
||||
```sql
|
||||
\d lms_practices
|
||||
-- should NOT list course_id, module_id, lesson_id
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Authentication & permissions
|
||||
|
||||
Use a **team member** or **platform admin** token:
|
||||
|
||||
```http
|
||||
Authorization: Bearer <access_token>
|
||||
Content-Type: application/json
|
||||
```
|
||||
|
||||
### LMS practice shell (parents + metadata)
|
||||
|
||||
| Action | Permission | HTTP |
|
||||
|--------|------------|------|
|
||||
| Create practice | `practices.create` | `POST /api/v1/practices` |
|
||||
| Get practice shell | `practices.get` | `GET /api/v1/practices/:id` |
|
||||
| Update shell (incl. parents) | `practices.update` | `PUT /api/v1/practices/:id` |
|
||||
| Delete practice | `practices.delete` | `DELETE /api/v1/practices/:id` |
|
||||
| List by course / module / lesson | `practices.list` | `GET /api/v1/courses\|modules\|lessons/:id/practices` |
|
||||
|
||||
### Practice content (questions)
|
||||
|
||||
| Action | Permission | HTTP |
|
||||
|--------|------------|------|
|
||||
| Load practice + questions | `practices.get` | `GET /api/v1/practices/:id/full` |
|
||||
| Save practice + questions | `practices.update` | `PUT /api/v1/practices/:id/full` |
|
||||
|
||||
Parent links are **not** part of the `/full` payload — keep parent editing on the shell create/update routes above.
|
||||
|
||||
---
|
||||
|
||||
## Data model
|
||||
|
||||
### `Practice` (response)
|
||||
|
||||
```json
|
||||
{
|
||||
"id": 55,
|
||||
"parents": [
|
||||
{ "parent_kind": "LESSON", "parent_id": 12 },
|
||||
{ "parent_kind": "LESSON", "parent_id": 18 },
|
||||
{ "parent_kind": "COURSE", "parent_id": 7 }
|
||||
],
|
||||
"title": "Module capstone",
|
||||
"story_description": "…",
|
||||
"story_image": "https://…",
|
||||
"persona_id": 3,
|
||||
"question_set_id": 100,
|
||||
"publish_status": "PUBLISHED",
|
||||
"quick_tips": "…",
|
||||
"created_at": "2026-06-18T10:00:00Z",
|
||||
"updated_at": "2026-06-18T11:00:00Z"
|
||||
}
|
||||
```
|
||||
|
||||
`access` is included for learner roles only; admin tokens typically receive `access: null`.
|
||||
|
||||
### `CreatePracticeInput`
|
||||
|
||||
| Field | Required | Notes |
|
||||
|-------|----------|-------|
|
||||
| `parents` | Preferred | Non-empty array of `{ parent_kind, parent_id }` |
|
||||
| `parent_kind` | Legacy fallback | Use with `parent_id` when `parents` omitted |
|
||||
| `parent_id` | Legacy fallback | Use with `parent_kind` when `parents` omitted |
|
||||
| `question_set_id` | **Yes** | Must reference an existing question set |
|
||||
| `title` | No | Defaults to empty string |
|
||||
| `story_description` | No | |
|
||||
| `story_image` | No | |
|
||||
| `persona_id` | No | Must exist in `lms_personas` when set |
|
||||
| `quick_tips` | No | |
|
||||
| `publish_status` | No | `DRAFT` or `PUBLISHED`; omit → `PUBLISHED` |
|
||||
|
||||
### `UpdatePracticeInput`
|
||||
|
||||
All fields optional. When `parents` is present, it **replaces the entire parent set** (not a merge).
|
||||
|
||||
| Field | Notes |
|
||||
|-------|-------|
|
||||
| `parents` | Full replacement list |
|
||||
| `title`, `story_description`, … | Same as before |
|
||||
| `publish_status` | `DRAFT` / `PUBLISHED` |
|
||||
|
||||
---
|
||||
|
||||
## Response envelopes
|
||||
|
||||
### Success (create / get / update shell)
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"status_code": 201,
|
||||
"message": "Practice created successfully",
|
||||
"data": { /* Practice */ }
|
||||
}
|
||||
```
|
||||
|
||||
### Success (list)
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"status_code": 200,
|
||||
"message": "Practices retrieved successfully",
|
||||
"data": {
|
||||
"practices": [ /* Practice[] — each includes parents */ ],
|
||||
"total_count": 1,
|
||||
"limit": 20,
|
||||
"offset": 0
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Error
|
||||
|
||||
```json
|
||||
{
|
||||
"message": "This course, module, or lesson already has a practice",
|
||||
"error": "this course, module, or lesson already has a practice"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Create practice
|
||||
|
||||
```http
|
||||
POST /api/v1/practices
|
||||
Authorization: Bearer <token>
|
||||
Content-Type: application/json
|
||||
```
|
||||
|
||||
### Example — multiple parents
|
||||
|
||||
```json
|
||||
{
|
||||
"parents": [
|
||||
{ "parent_kind": "MODULE", "parent_id": 9 },
|
||||
{ "parent_kind": "LESSON", "parent_id": 42 }
|
||||
],
|
||||
"title": "Shared review",
|
||||
"question_set_id": 100,
|
||||
"publish_status": "DRAFT"
|
||||
}
|
||||
```
|
||||
|
||||
### Example — launched from a lesson page (legacy-compatible)
|
||||
|
||||
When the user clicks “Add practice” on Lesson 42, pre-fill one parent but allow adding more before submit:
|
||||
|
||||
```json
|
||||
{
|
||||
"parents": [
|
||||
{ "parent_kind": "LESSON", "parent_id": 42 }
|
||||
],
|
||||
"question_set_id": 100
|
||||
}
|
||||
```
|
||||
|
||||
Equivalent legacy body (still works):
|
||||
|
||||
```json
|
||||
{
|
||||
"parent_kind": "LESSON",
|
||||
"parent_id": 42,
|
||||
"question_set_id": 100
|
||||
}
|
||||
```
|
||||
|
||||
### Typical admin create flow
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant UI as Admin UI
|
||||
participant API as API
|
||||
|
||||
UI->>API: POST /question-sets (optional — create empty practice set first)
|
||||
API-->>UI: question_set_id
|
||||
|
||||
UI->>API: POST /practices { parents, question_set_id, title, … }
|
||||
API-->>UI: practice { id, parents, question_set_id }
|
||||
|
||||
UI->>API: PUT /practices/:id/full { questions, question_set, practice }
|
||||
API-->>UI: full practice payload
|
||||
```
|
||||
|
||||
1. Create or select a **question set** (`question_set_id`).
|
||||
2. **POST /practices** with `parents` + `question_set_id`.
|
||||
3. Navigate to the question editor; use **PUT /practices/:id/full** for questions (see associations guide).
|
||||
|
||||
---
|
||||
|
||||
## Update practice shell (including parents)
|
||||
|
||||
```http
|
||||
PUT /api/v1/practices/:id
|
||||
Authorization: Bearer <token>
|
||||
Content-Type: application/json
|
||||
```
|
||||
|
||||
### Replace all parent links
|
||||
|
||||
```json
|
||||
{
|
||||
"parents": [
|
||||
{ "parent_kind": "COURSE", "parent_id": 7 },
|
||||
{ "parent_kind": "LESSON", "parent_id": 42 }
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Sending `parents: []` is rejected (invalid parent). To change parents, send the **full desired list**.
|
||||
|
||||
### Update metadata only (parents unchanged)
|
||||
|
||||
Omit `parents` from the body:
|
||||
|
||||
```json
|
||||
{
|
||||
"title": "Updated title",
|
||||
"publish_status": "PUBLISHED"
|
||||
}
|
||||
```
|
||||
|
||||
### Conflict when retargeting a taken parent
|
||||
|
||||
If Lesson 99 already has a different practice, adding it to this practice’s `parents` returns **409** with message `This course, module, or lesson already has a practice`.
|
||||
|
||||
---
|
||||
|
||||
## Full practice load/save (questions)
|
||||
|
||||
Parent links appear in the **`practice`** object returned by full load, but **cannot be changed** via full save.
|
||||
|
||||
```http
|
||||
GET /api/v1/practices/:id/full
|
||||
PUT /api/v1/practices/:id/full
|
||||
```
|
||||
|
||||
### GET response shape (abbreviated)
|
||||
|
||||
```json
|
||||
{
|
||||
"data": {
|
||||
"practice": {
|
||||
"id": 55,
|
||||
"parents": [
|
||||
{ "parent_kind": "LESSON", "parent_id": 42 }
|
||||
],
|
||||
"title": "…",
|
||||
"question_set_id": 100
|
||||
},
|
||||
"question_set": { /* … */ },
|
||||
"questions": [ /* … */ ]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### PUT — shell section (no parents)
|
||||
|
||||
```json
|
||||
{
|
||||
"practice": {
|
||||
"title": "New title",
|
||||
"publish_status": "PUBLISHED"
|
||||
},
|
||||
"question_set": { "status": "PUBLISHED" },
|
||||
"questions": [ /* … */ ]
|
||||
}
|
||||
```
|
||||
|
||||
To edit **where** the practice appears in the hierarchy, call `PUT /api/v1/practices/:id` with `parents` — either from a dedicated “Locations” tab or before/after a full save.
|
||||
|
||||
---
|
||||
|
||||
## List & read endpoints
|
||||
|
||||
Each list route returns practices that have a **direct** parent of the matching kind:
|
||||
|
||||
| Endpoint | Returns practices where… |
|
||||
|----------|--------------------------|
|
||||
| `GET /api/v1/courses/:id/practices` | `parents` contains `{ parent_kind: "COURSE", parent_id: :id }` |
|
||||
| `GET /api/v1/modules/:id/practices` | `parents` contains `{ parent_kind: "MODULE", parent_id: :id }` |
|
||||
| `GET /api/v1/lessons/:id/practices` | `parents` contains `{ parent_kind: "LESSON", parent_id: :id }` |
|
||||
|
||||
Query params: `limit` (default 20, max 200), `offset`.
|
||||
|
||||
### Global lookup by question type
|
||||
|
||||
`GET /api/v1/questions/type-definitions/:id/practices` returns LMS rows with `parents` (exam-prep rows still use `exam_prep_lesson_id`).
|
||||
|
||||
---
|
||||
|
||||
## Recommended admin UI changes
|
||||
|
||||
### 1. Replace single-parent controls
|
||||
|
||||
| Old UI | New UI |
|
||||
|--------|--------|
|
||||
| Radio: Course / Module / Lesson + one ID picker | **Multi-row parent list** with kind + searchable entity picker |
|
||||
| Read `practice.parent_kind` | Read `practice.parents` |
|
||||
| Disable parent after create | Allow edit via **Practice settings → Locations** (`PUT /practices/:id`) |
|
||||
|
||||
### 2. “Locations” or “Attached to” section
|
||||
|
||||
Show all parents as chips with hierarchy context, e.g.:
|
||||
|
||||
- `Course: Algebra I`
|
||||
- `Module: Unit 3`
|
||||
- `Lesson: Factoring intro`
|
||||
|
||||
Actions:
|
||||
|
||||
- **Add location** — append row; validate entity not already used by another practice
|
||||
- **Remove location** — drop from local list (must keep ≥ 1 parent on save)
|
||||
- **Save locations** — `PUT /practices/:id` with full `parents` array
|
||||
|
||||
### 3. Contextual create
|
||||
|
||||
When opening create from a lesson/module/course page:
|
||||
|
||||
1. Pre-populate `parents` with that entity.
|
||||
2. Show “Also attach to…” to add more parents before first save.
|
||||
3. Still require `question_set_id` (create set inline or pick existing).
|
||||
|
||||
### 4. Practice picker / duplicate detection
|
||||
|
||||
Before adding parent `{ kind, id }`, optionally call the list endpoint for that entity:
|
||||
|
||||
```http
|
||||
GET /api/v1/lessons/42/practices?limit=1
|
||||
```
|
||||
|
||||
If `total_count > 0` and the returned practice `id` ≠ current practice, warn that the lesson already has a practice.
|
||||
|
||||
### 5. Breadcrumbs & navigation
|
||||
|
||||
A practice with multiple parents may appear in several admin navigation paths. Use `parents` to render all entry points, not a single breadcrumb.
|
||||
|
||||
---
|
||||
|
||||
## Form state & serialization
|
||||
|
||||
### Suggested editor state
|
||||
|
||||
```typescript
|
||||
type PracticeParentKind = "COURSE" | "MODULE" | "LESSON";
|
||||
|
||||
interface PracticeParent {
|
||||
parent_kind: PracticeParentKind;
|
||||
parent_id: number;
|
||||
}
|
||||
|
||||
interface PracticeCreateFormState {
|
||||
parents: PracticeParent[];
|
||||
question_set_id: number | null;
|
||||
title: string;
|
||||
publish_status: "DRAFT" | "PUBLISHED";
|
||||
// … story, persona, tips
|
||||
}
|
||||
|
||||
interface PracticeShellState extends PracticeCreateFormState {
|
||||
id: number;
|
||||
}
|
||||
```
|
||||
|
||||
### Create payload mapper
|
||||
|
||||
```typescript
|
||||
function toCreatePracticePayload(state: PracticeCreateFormState) {
|
||||
return {
|
||||
parents: dedupeParents(state.parents),
|
||||
question_set_id: state.question_set_id!,
|
||||
title: state.title || undefined,
|
||||
publish_status: state.publish_status,
|
||||
};
|
||||
}
|
||||
|
||||
function dedupeParents(parents: PracticeParent[]): PracticeParent[] {
|
||||
const seen = new Set<string>();
|
||||
return parents.filter((p) => {
|
||||
if (!p.parent_kind || p.parent_id <= 0) return false;
|
||||
const key = `${p.parent_kind}:${p.parent_id}`;
|
||||
if (seen.has(key)) return false;
|
||||
seen.add(key);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
### Normalizing API → form (migration from old clients)
|
||||
|
||||
```typescript
|
||||
function parentsFromPractice(practice: {
|
||||
parents?: PracticeParent[];
|
||||
parent_kind?: PracticeParentKind;
|
||||
parent_id?: number;
|
||||
}): PracticeParent[] {
|
||||
if (practice.parents?.length) return practice.parents;
|
||||
if (practice.parent_kind && practice.parent_id) {
|
||||
return [{ parent_kind: practice.parent_kind, parent_id: practice.parent_id }];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
```
|
||||
|
||||
Use this helper during rollout if cached responses might still have the old shape.
|
||||
|
||||
---
|
||||
|
||||
## Client-side validation
|
||||
|
||||
Mirror server rules before submit:
|
||||
|
||||
| Rule | Error message (suggested) |
|
||||
|------|---------------------------|
|
||||
| `parents.length >= 1` on create | “Select at least one course, module, or lesson.” |
|
||||
| Each `parent_id > 0` | “Pick a valid parent for each row.” |
|
||||
| No duplicate `(kind, id)` | “The same location is listed twice.” |
|
||||
| `question_set_id` set on create | “Link a question set before creating the practice.” |
|
||||
| On parent add: optional occupancy check | “This lesson already has another practice.” |
|
||||
|
||||
Server remains authoritative for entity existence (404) and uniqueness (409).
|
||||
|
||||
---
|
||||
|
||||
## TypeScript types & helpers
|
||||
|
||||
```typescript
|
||||
interface ApiResponse<T> {
|
||||
success: boolean;
|
||||
status_code: number;
|
||||
message: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
interface Practice {
|
||||
id: number;
|
||||
parents: PracticeParent[];
|
||||
title: string;
|
||||
story_description?: string | null;
|
||||
story_image?: string | null;
|
||||
persona_id?: number | null;
|
||||
question_set_id: number;
|
||||
publish_status: "DRAFT" | "PUBLISHED";
|
||||
quick_tips?: string | null;
|
||||
created_at: string;
|
||||
updated_at?: string | null;
|
||||
}
|
||||
|
||||
interface CreatePracticeInput {
|
||||
parents?: PracticeParent[];
|
||||
parent_kind?: PracticeParentKind;
|
||||
parent_id?: number;
|
||||
question_set_id: number;
|
||||
title?: string;
|
||||
story_description?: string;
|
||||
story_image?: string;
|
||||
persona_id?: number;
|
||||
quick_tips?: string;
|
||||
publish_status?: "DRAFT" | "PUBLISHED";
|
||||
}
|
||||
|
||||
interface UpdatePracticeInput {
|
||||
parents?: PracticeParent[];
|
||||
title?: string;
|
||||
story_description?: string;
|
||||
story_image?: string;
|
||||
persona_id?: number;
|
||||
question_set_id?: number;
|
||||
quick_tips?: string;
|
||||
publish_status?: "DRAFT" | "PUBLISHED";
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## React integration patterns
|
||||
|
||||
### Create practice with multiple parents
|
||||
|
||||
```typescript
|
||||
async function createPractice(
|
||||
payload: CreatePracticeInput,
|
||||
token: string
|
||||
): Promise<Practice> {
|
||||
const res = await fetch(`${API_BASE}/api/v1/practices`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const body = await res.json().catch(() => ({}));
|
||||
throw new Error(body.message || body.error || `Create failed (${res.status})`);
|
||||
}
|
||||
|
||||
const json: ApiResponse<Practice> = await res.json();
|
||||
return json.data;
|
||||
}
|
||||
```
|
||||
|
||||
### Update locations only
|
||||
|
||||
```typescript
|
||||
async function updatePracticeParents(
|
||||
practiceId: number,
|
||||
parents: PracticeParent[],
|
||||
token: string
|
||||
): Promise<Practice> {
|
||||
const res = await fetch(`${API_BASE}/api/v1/practices/${practiceId}`, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ parents: dedupeParents(parents) }),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const body = await res.json().catch(() => ({}));
|
||||
throw new Error(body.message || body.error || `Update failed (${res.status})`);
|
||||
}
|
||||
|
||||
const json: ApiResponse<Practice> = await res.json();
|
||||
return json.data;
|
||||
}
|
||||
```
|
||||
|
||||
### Load full editor (parents read-only in question save)
|
||||
|
||||
```typescript
|
||||
async function loadPracticeFull(practiceId: number, token: string) {
|
||||
const res = await fetch(`${API_BASE}/api/v1/practices/${practiceId}/full`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
if (!res.ok) throw new Error(`Load failed (${res.status})`);
|
||||
const json = await res.json();
|
||||
return json.data as {
|
||||
practice: Practice;
|
||||
question_set: unknown;
|
||||
questions: unknown[];
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
Keep `practice.parents` in shell state; persist parent changes with `updatePracticeParents`, not with `PUT .../full`.
|
||||
|
||||
---
|
||||
|
||||
## Error handling
|
||||
|
||||
| HTTP | `message` | Typical cause | Admin UI action |
|
||||
|------|-----------|---------------|-----------------|
|
||||
| 400 | `Invalid parent` | No parents resolved, bad kind, empty list | Highlight parent picker |
|
||||
| 400 | `Validation failed` | Malformed body | Show field errors |
|
||||
| 404 | `Course not found` / `Module not found` / `Lesson not found` | Stale ID in picker | Refresh hierarchy data |
|
||||
| 404 | `Question set not found` | Invalid `question_set_id` | Re-link question set |
|
||||
| 404 | `Persona not found` | Invalid `persona_id` | Fix persona selection |
|
||||
| 409 | `This course, module, or lesson already has a practice` | Parent already linked elsewhere | Pick a different entity or edit the other practice |
|
||||
| 403 | Permission denied | Missing `practices.create` / `practices.update` | RBAC message |
|
||||
|
||||
Always surface the API `error` string when present.
|
||||
|
||||
---
|
||||
|
||||
## Integration checklist
|
||||
|
||||
### Data & API
|
||||
|
||||
- [ ] Migration `000089` applied in target environments
|
||||
- [ ] Create form sends `parents[]` (or legacy fields only during transition)
|
||||
- [ ] All reads use `practice.parents` instead of `parent_kind` / `parent_id`
|
||||
- [ ] Parent edits use `PUT /api/v1/practices/:id` — **not** `PUT .../full`
|
||||
- [ ] Full editor (`GET/PUT .../full`) unchanged for questions — see [associations guide](./practice-question-associations-admin-panel-integration.md)
|
||||
- [ ] List pages still use course/module/lesson scoped list endpoints; expect same practice ID if multi-parented
|
||||
|
||||
### UI
|
||||
|
||||
- [ ] Multi-row parent picker on create
|
||||
- [ ] “Locations” panel on practice settings with add/remove
|
||||
- [ ] Contextual pre-fill when creating from hierarchy page
|
||||
- [ ] Conflict toast on 409 (parent already taken)
|
||||
- [ ] Breadcrumbs / back-links support multiple parents
|
||||
|
||||
### QA scenarios
|
||||
|
||||
- [ ] Create with one parent (parity with old flow)
|
||||
- [ ] Create with course + two lessons
|
||||
- [ ] Update parents: add a module link to existing practice
|
||||
- [ ] Update parents: remove one link, keep others
|
||||
- [ ] Attempt to attach lesson that already has another practice → 409
|
||||
- [ ] List by lesson shows practice when lesson is one of several parents
|
||||
- [ ] Full save of questions does not clear or alter parents
|
||||
- [ ] Draft practice with multiple parents; publish via shell update
|
||||
|
||||
---
|
||||
|
||||
## Limitations
|
||||
|
||||
| Topic | Behavior |
|
||||
|-------|----------|
|
||||
| Exam-prep practices | Still **one** `unit_module_lesson_id` per practice; this guide does not apply |
|
||||
| One practice per entity | A given course, module, or lesson cannot be parent to two different practices |
|
||||
| Full update payload | `parents` not supported on `PUT .../full` |
|
||||
| Indirect course listing | `GET /courses/:id/practices` does **not** include practices only linked via lessons inside that course — add an explicit `COURSE` parent if the practice should appear on the course list |
|
||||
| Progression | Completing a multi-parent practice can trigger rollups for **each** parent scope (lesson module, direct course, etc.) |
|
||||
|
||||
---
|
||||
|
||||
## Quick reference
|
||||
|
||||
| Task | Method | Path | Body highlights |
|
||||
|------|--------|------|-----------------|
|
||||
| Create practice | `POST` | `/api/v1/practices` | `parents`, `question_set_id` |
|
||||
| Get shell | `GET` | `/api/v1/practices/:id` | — |
|
||||
| Update shell + parents | `PUT` | `/api/v1/practices/:id` | `parents` replaces all links |
|
||||
| Load questions | `GET` | `/api/v1/practices/:id/full` | `data.practice.parents` |
|
||||
| Save questions | `PUT` | `/api/v1/practices/:id/full` | no `parents` |
|
||||
| List on course | `GET` | `/api/v1/courses/:id/practices` | — |
|
||||
| List on module | `GET` | `/api/v1/modules/:id/practices` | — |
|
||||
| List on lesson | `GET` | `/api/v1/lessons/:id/practices` | — |
|
||||
|
||||
### Minimal create example
|
||||
|
||||
```bash
|
||||
curl -s -X POST "$API/api/v1/practices" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"parents": [
|
||||
{ "parent_kind": "LESSON", "parent_id": 42 },
|
||||
{ "parent_kind": "MODULE", "parent_id": 9 }
|
||||
],
|
||||
"question_set_id": 100,
|
||||
"title": "Shared practice",
|
||||
"publish_status": "DRAFT"
|
||||
}'
|
||||
```
|
||||
|
|
@ -27,9 +27,9 @@ SELECT
|
|||
(SELECT COUNT(*)::bigint FROM lessons) AS lms_lessons,
|
||||
(SELECT COUNT(*)::bigint FROM lessons WHERE NULLIF(BTRIM(video_url), '') IS NOT NULL) AS lms_lessons_with_video,
|
||||
(SELECT COUNT(*)::bigint FROM lms_practices) AS lms_practices,
|
||||
(SELECT COUNT(*)::bigint FROM lms_practices WHERE course_id IS NOT NULL) AS lms_practices_at_course,
|
||||
(SELECT COUNT(*)::bigint FROM lms_practices WHERE module_id IS NOT NULL) AS lms_practices_at_module,
|
||||
(SELECT COUNT(*)::bigint FROM lms_practices WHERE lesson_id IS NOT NULL) AS lms_practices_at_lesson,
|
||||
(SELECT COUNT(*)::bigint FROM lms_practice_parents WHERE parent_kind = 'COURSE') AS lms_practices_at_course,
|
||||
(SELECT COUNT(*)::bigint FROM lms_practice_parents WHERE parent_kind = 'MODULE') AS lms_practices_at_module,
|
||||
(SELECT COUNT(*)::bigint FROM lms_practice_parents WHERE parent_kind = 'LESSON') AS lms_practices_at_lesson,
|
||||
(SELECT COUNT(*)::bigint FROM exam_prep.catalog_courses) AS exam_prep_catalog_courses,
|
||||
(SELECT COUNT(*)::bigint FROM exam_prep.units) AS exam_prep_units,
|
||||
(SELECT COUNT(*)::bigint FROM exam_prep.unit_modules) AS exam_prep_unit_modules,
|
||||
|
|
|
|||
|
|
@ -96,12 +96,13 @@ UNION ALL
|
|||
FROM
|
||||
user_practice_progress upp
|
||||
INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
|
||||
AND lp.lesson_id IS NOT NULL
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'LESSON'
|
||||
INNER JOIN question_sets qs ON qs.id = upp.question_set_id
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
INNER JOIN lessons l ON l.id = lp.lesson_id
|
||||
INNER JOIN lessons l ON l.id = lpp.parent_id
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
AND c.program_id = m.program_id
|
||||
|
|
@ -142,13 +143,13 @@ UNION ALL
|
|||
FROM
|
||||
user_practice_progress upp
|
||||
INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
|
||||
AND lp.module_id IS NOT NULL
|
||||
AND lp.lesson_id IS NULL
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'MODULE'
|
||||
INNER JOIN question_sets qs ON qs.id = upp.question_set_id
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
INNER JOIN modules m ON m.id = lp.module_id
|
||||
INNER JOIN modules m ON m.id = lpp.parent_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
AND c.program_id = m.program_id
|
||||
INNER JOIN programs p ON p.id = c.program_id
|
||||
|
|
@ -186,14 +187,13 @@ UNION ALL
|
|||
FROM
|
||||
user_practice_progress upp
|
||||
INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
|
||||
AND lp.course_id IS NOT NULL
|
||||
AND lp.module_id IS NULL
|
||||
AND lp.lesson_id IS NULL
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'COURSE'
|
||||
INNER JOIN question_sets qs ON qs.id = upp.question_set_id
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
INNER JOIN courses c ON c.id = lp.course_id
|
||||
INNER JOIN courses c ON c.id = lpp.parent_id
|
||||
INNER JOIN programs p ON p.id = c.program_id
|
||||
LEFT JOIN lms_user_program_progress prf ON prf.user_id = upp.user_id
|
||||
AND prf.program_id = p.id
|
||||
|
|
|
|||
|
|
@ -82,10 +82,10 @@ SELECT
|
|||
c.id, c.program_id, c.name, c.description, c.thumbnail, c.created_at, c.updated_at, c.sort_order, c.publish_status, c.access_tier,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM lms_practices p
|
||||
WHERE p.course_id = c.id
|
||||
AND p.module_id IS NULL
|
||||
AND p.lesson_id IS NULL
|
||||
FROM lms_practice_parents lpp
|
||||
INNER JOIN lms_practices p ON p.id = lpp.practice_id
|
||||
WHERE lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id = c.id
|
||||
AND p.publish_status = 'PUBLISHED'
|
||||
) AS has_practice
|
||||
FROM courses
|
||||
|
|
@ -187,24 +187,23 @@ SELECT
|
|||
WHERE
|
||||
m.course_id = c.id
|
||||
AND l.publish_status = 'PUBLISHED') AS lesson_count,
|
||||
-- Practices whose parent is the course only (lms_practices.course_id). Excludes
|
||||
-- practices linked via module_id or lesson_id, even for modules/lessons in this course.
|
||||
-- Practices whose parent is the course only. Excludes practices linked via module or lesson parents.
|
||||
(
|
||||
SELECT
|
||||
COUNT(*)::bigint
|
||||
FROM
|
||||
lms_practices p
|
||||
lms_practice_parents lpp
|
||||
INNER JOIN lms_practices p ON p.id = lpp.practice_id
|
||||
WHERE
|
||||
p.course_id = c.id
|
||||
AND p.module_id IS NULL
|
||||
AND p.lesson_id IS NULL
|
||||
lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id = c.id
|
||||
AND p.publish_status = 'PUBLISHED') AS practice_count,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM lms_practices p
|
||||
WHERE p.course_id = c.id
|
||||
AND p.module_id IS NULL
|
||||
AND p.lesson_id IS NULL
|
||||
FROM lms_practice_parents lpp
|
||||
INNER JOIN lms_practices p ON p.id = lpp.practice_id
|
||||
WHERE lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id = c.id
|
||||
AND p.publish_status = 'PUBLISHED'
|
||||
) AS has_practice
|
||||
FROM
|
||||
|
|
|
|||
|
|
@ -86,8 +86,10 @@ SELECT
|
|||
l.id, l.module_id, l.title, l.video_url, l.thumbnail, l.description, l.created_at, l.updated_at, l.sort_order, l.publish_status, l.access_tier,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM lms_practices p
|
||||
WHERE p.lesson_id = l.id
|
||||
FROM lms_practice_parents lpp
|
||||
INNER JOIN lms_practices p ON p.id = lpp.practice_id
|
||||
WHERE lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id = l.id
|
||||
AND p.publish_status = 'PUBLISHED'
|
||||
) AS has_practice
|
||||
FROM lessons
|
||||
|
|
@ -177,8 +179,10 @@ SELECT
|
|||
l.updated_at,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM lms_practices p
|
||||
WHERE p.lesson_id = l.id
|
||||
FROM lms_practice_parents lpp
|
||||
INNER JOIN lms_practices p ON p.id = lpp.practice_id
|
||||
WHERE lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id = l.id
|
||||
AND p.publish_status = 'PUBLISHED'
|
||||
) AS has_practice
|
||||
FROM
|
||||
|
|
|
|||
|
|
@ -86,9 +86,10 @@ SELECT
|
|||
m.id, m.program_id, m.course_id, m.name, m.description, m.icon, m.created_at, m.updated_at, m.sort_order, m.publish_status, m.access_tier,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM lms_practices p
|
||||
WHERE p.module_id = m.id
|
||||
AND p.lesson_id IS NULL
|
||||
FROM lms_practice_parents lpp
|
||||
INNER JOIN lms_practices p ON p.id = lpp.practice_id
|
||||
WHERE lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id = m.id
|
||||
AND p.publish_status = 'PUBLISHED'
|
||||
) AS has_practice
|
||||
FROM modules
|
||||
|
|
@ -178,9 +179,10 @@ SELECT
|
|||
m.updated_at,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM lms_practices p
|
||||
WHERE p.module_id = m.id
|
||||
AND p.lesson_id IS NULL
|
||||
FROM lms_practice_parents lpp
|
||||
INNER JOIN lms_practices p ON p.id = lpp.practice_id
|
||||
WHERE lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id = m.id
|
||||
AND p.publish_status = 'PUBLISHED'
|
||||
) AS has_practice
|
||||
FROM
|
||||
|
|
|
|||
188
gen/db/lms_practice_parents.sql.go
Normal file
188
gen/db/lms_practice_parents.sql.go
Normal file
|
|
@ -0,0 +1,188 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
// source: lms_practice_parents.sql
|
||||
|
||||
package dbgen
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const CountLmsPracticesByParent = `-- name: CountLmsPracticesByParent :one
|
||||
SELECT COUNT(*)::bigint
|
||||
FROM lms_practice_parents
|
||||
WHERE parent_kind = $1
|
||||
AND parent_id = $2
|
||||
`
|
||||
|
||||
type CountLmsPracticesByParentParams struct {
|
||||
ParentKind string `json:"parent_kind"`
|
||||
ParentID int64 `json:"parent_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) CountLmsPracticesByParent(ctx context.Context, arg CountLmsPracticesByParentParams) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, CountLmsPracticesByParent, arg.ParentKind, arg.ParentID)
|
||||
var column_1 int64
|
||||
err := row.Scan(&column_1)
|
||||
return column_1, err
|
||||
}
|
||||
|
||||
const CountLmsPracticesByParentExcludingPractice = `-- name: CountLmsPracticesByParentExcludingPractice :one
|
||||
SELECT COUNT(*)::bigint
|
||||
FROM lms_practice_parents
|
||||
WHERE parent_kind = $1
|
||||
AND parent_id = $2
|
||||
AND practice_id <> $3
|
||||
`
|
||||
|
||||
type CountLmsPracticesByParentExcludingPracticeParams struct {
|
||||
ParentKind string `json:"parent_kind"`
|
||||
ParentID int64 `json:"parent_id"`
|
||||
PracticeID int64 `json:"practice_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) CountLmsPracticesByParentExcludingPractice(ctx context.Context, arg CountLmsPracticesByParentExcludingPracticeParams) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, CountLmsPracticesByParentExcludingPractice, arg.ParentKind, arg.ParentID, arg.PracticeID)
|
||||
var column_1 int64
|
||||
err := row.Scan(&column_1)
|
||||
return column_1, err
|
||||
}
|
||||
|
||||
const DeleteLmsPracticeParentsByPracticeID = `-- name: DeleteLmsPracticeParentsByPracticeID :exec
|
||||
DELETE FROM lms_practice_parents
|
||||
WHERE practice_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteLmsPracticeParentsByPracticeID(ctx context.Context, practiceID int64) error {
|
||||
_, err := q.db.Exec(ctx, DeleteLmsPracticeParentsByPracticeID, practiceID)
|
||||
return err
|
||||
}
|
||||
|
||||
const InsertLmsPracticeParent = `-- name: InsertLmsPracticeParent :one
|
||||
INSERT INTO lms_practice_parents (practice_id, parent_kind, parent_id)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id, practice_id, parent_kind, parent_id, created_at
|
||||
`
|
||||
|
||||
type InsertLmsPracticeParentParams struct {
|
||||
PracticeID int64 `json:"practice_id"`
|
||||
ParentKind string `json:"parent_kind"`
|
||||
ParentID int64 `json:"parent_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) InsertLmsPracticeParent(ctx context.Context, arg InsertLmsPracticeParentParams) (LmsPracticeParent, error) {
|
||||
row := q.db.QueryRow(ctx, InsertLmsPracticeParent, arg.PracticeID, arg.ParentKind, arg.ParentID)
|
||||
var i LmsPracticeParent
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.PracticeID,
|
||||
&i.ParentKind,
|
||||
&i.ParentID,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const ListLmsPracticeParentsByPracticeID = `-- name: ListLmsPracticeParentsByPracticeID :many
|
||||
SELECT id, practice_id, parent_kind, parent_id, created_at
|
||||
FROM lms_practice_parents
|
||||
WHERE practice_id = $1
|
||||
ORDER BY id ASC
|
||||
`
|
||||
|
||||
func (q *Queries) ListLmsPracticeParentsByPracticeID(ctx context.Context, practiceID int64) ([]LmsPracticeParent, error) {
|
||||
rows, err := q.db.Query(ctx, ListLmsPracticeParentsByPracticeID, practiceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []LmsPracticeParent
|
||||
for rows.Next() {
|
||||
var i LmsPracticeParent
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.PracticeID,
|
||||
&i.ParentKind,
|
||||
&i.ParentID,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const ListLmsPracticeParentsByPracticeIDs = `-- name: ListLmsPracticeParentsByPracticeIDs :many
|
||||
SELECT id, practice_id, parent_kind, parent_id, created_at
|
||||
FROM lms_practice_parents
|
||||
WHERE practice_id = ANY ($1::bigint[])
|
||||
ORDER BY practice_id ASC, id ASC
|
||||
`
|
||||
|
||||
func (q *Queries) ListLmsPracticeParentsByPracticeIDs(ctx context.Context, practiceIds []int64) ([]LmsPracticeParent, error) {
|
||||
rows, err := q.db.Query(ctx, ListLmsPracticeParentsByPracticeIDs, practiceIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []LmsPracticeParent
|
||||
for rows.Next() {
|
||||
var i LmsPracticeParent
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.PracticeID,
|
||||
&i.ParentKind,
|
||||
&i.ParentID,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const ListLmsPracticeParentsByQuestionSetID = `-- name: ListLmsPracticeParentsByQuestionSetID :many
|
||||
SELECT
|
||||
lpp.parent_kind,
|
||||
lpp.parent_id,
|
||||
lpp.practice_id
|
||||
FROM lms_practice_parents lpp
|
||||
INNER JOIN lms_practices lp ON lp.id = lpp.practice_id
|
||||
WHERE lp.question_set_id = $1
|
||||
ORDER BY lp.id DESC, lpp.id ASC
|
||||
`
|
||||
|
||||
type ListLmsPracticeParentsByQuestionSetIDRow struct {
|
||||
ParentKind string `json:"parent_kind"`
|
||||
ParentID int64 `json:"parent_id"`
|
||||
PracticeID int64 `json:"practice_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) ListLmsPracticeParentsByQuestionSetID(ctx context.Context, questionSetID int64) ([]ListLmsPracticeParentsByQuestionSetIDRow, error) {
|
||||
rows, err := q.db.Query(ctx, ListLmsPracticeParentsByQuestionSetID, questionSetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListLmsPracticeParentsByQuestionSetIDRow
|
||||
for rows.Next() {
|
||||
var i ListLmsPracticeParentsByQuestionSetIDRow
|
||||
if err := rows.Scan(&i.ParentKind, &i.ParentID, &i.PracticeID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
|
@ -13,12 +13,13 @@ import (
|
|||
|
||||
const CountLmsPracticesByCourseID = `-- name: CountLmsPracticesByCourseID :one
|
||||
SELECT COUNT(*)::bigint
|
||||
FROM lms_practices
|
||||
WHERE course_id = $1
|
||||
FROM lms_practice_parents
|
||||
WHERE parent_kind = 'COURSE'
|
||||
AND parent_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) CountLmsPracticesByCourseID(ctx context.Context, courseID pgtype.Int8) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, CountLmsPracticesByCourseID, courseID)
|
||||
func (q *Queries) CountLmsPracticesByCourseID(ctx context.Context, parentID int64) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, CountLmsPracticesByCourseID, parentID)
|
||||
var column_1 int64
|
||||
err := row.Scan(&column_1)
|
||||
return column_1, err
|
||||
|
|
@ -26,12 +27,13 @@ func (q *Queries) CountLmsPracticesByCourseID(ctx context.Context, courseID pgty
|
|||
|
||||
const CountLmsPracticesByLessonID = `-- name: CountLmsPracticesByLessonID :one
|
||||
SELECT COUNT(*)::bigint
|
||||
FROM lms_practices
|
||||
WHERE lesson_id = $1
|
||||
FROM lms_practice_parents
|
||||
WHERE parent_kind = 'LESSON'
|
||||
AND parent_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) CountLmsPracticesByLessonID(ctx context.Context, lessonID pgtype.Int8) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, CountLmsPracticesByLessonID, lessonID)
|
||||
func (q *Queries) CountLmsPracticesByLessonID(ctx context.Context, parentID int64) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, CountLmsPracticesByLessonID, parentID)
|
||||
var column_1 int64
|
||||
err := row.Scan(&column_1)
|
||||
return column_1, err
|
||||
|
|
@ -39,12 +41,13 @@ func (q *Queries) CountLmsPracticesByLessonID(ctx context.Context, lessonID pgty
|
|||
|
||||
const CountLmsPracticesByModuleID = `-- name: CountLmsPracticesByModuleID :one
|
||||
SELECT COUNT(*)::bigint
|
||||
FROM lms_practices
|
||||
WHERE module_id = $1
|
||||
FROM lms_practice_parents
|
||||
WHERE parent_kind = 'MODULE'
|
||||
AND parent_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) CountLmsPracticesByModuleID(ctx context.Context, moduleID pgtype.Int8) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, CountLmsPracticesByModuleID, moduleID)
|
||||
func (q *Queries) CountLmsPracticesByModuleID(ctx context.Context, parentID int64) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, CountLmsPracticesByModuleID, parentID)
|
||||
var column_1 int64
|
||||
err := row.Scan(&column_1)
|
||||
return column_1, err
|
||||
|
|
@ -52,16 +55,12 @@ func (q *Queries) CountLmsPracticesByModuleID(ctx context.Context, moduleID pgty
|
|||
|
||||
const CreateLmsPractice = `-- name: CreateLmsPractice :one
|
||||
INSERT INTO lms_practices (
|
||||
course_id, module_id, lesson_id,
|
||||
title, story_description, story_image, persona_id, question_set_id, quick_tips, publish_status
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
RETURNING id, course_id, module_id, lesson_id, title, story_description, story_image, persona_id, question_set_id, quick_tips, created_at, updated_at, publish_status
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id, title, story_description, story_image, persona_id, question_set_id, quick_tips, created_at, updated_at, publish_status
|
||||
`
|
||||
|
||||
type CreateLmsPracticeParams struct {
|
||||
CourseID pgtype.Int8 `json:"course_id"`
|
||||
ModuleID pgtype.Int8 `json:"module_id"`
|
||||
LessonID pgtype.Int8 `json:"lesson_id"`
|
||||
Title string `json:"title"`
|
||||
StoryDescription pgtype.Text `json:"story_description"`
|
||||
StoryImage pgtype.Text `json:"story_image"`
|
||||
|
|
@ -73,9 +72,6 @@ type CreateLmsPracticeParams struct {
|
|||
|
||||
func (q *Queries) CreateLmsPractice(ctx context.Context, arg CreateLmsPracticeParams) (LmsPractice, error) {
|
||||
row := q.db.QueryRow(ctx, CreateLmsPractice,
|
||||
arg.CourseID,
|
||||
arg.ModuleID,
|
||||
arg.LessonID,
|
||||
arg.Title,
|
||||
arg.StoryDescription,
|
||||
arg.StoryImage,
|
||||
|
|
@ -87,9 +83,6 @@ func (q *Queries) CreateLmsPractice(ctx context.Context, arg CreateLmsPracticePa
|
|||
var i LmsPractice
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CourseID,
|
||||
&i.ModuleID,
|
||||
&i.LessonID,
|
||||
&i.Title,
|
||||
&i.StoryDescription,
|
||||
&i.StoryImage,
|
||||
|
|
@ -114,7 +107,7 @@ func (q *Queries) DeleteLmsPractice(ctx context.Context, id int64) error {
|
|||
}
|
||||
|
||||
const GetLmsPracticeByID = `-- name: GetLmsPracticeByID :one
|
||||
SELECT id, course_id, module_id, lesson_id, title, story_description, story_image, persona_id, question_set_id, quick_tips, created_at, updated_at, publish_status
|
||||
SELECT id, title, story_description, story_image, persona_id, question_set_id, quick_tips, created_at, updated_at, publish_status
|
||||
FROM lms_practices
|
||||
WHERE id = $1
|
||||
`
|
||||
|
|
@ -124,9 +117,6 @@ func (q *Queries) GetLmsPracticeByID(ctx context.Context, id int64) (LmsPractice
|
|||
var i LmsPractice
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CourseID,
|
||||
&i.ModuleID,
|
||||
&i.LessonID,
|
||||
&i.Title,
|
||||
&i.StoryDescription,
|
||||
&i.StoryImage,
|
||||
|
|
@ -141,7 +131,7 @@ func (q *Queries) GetLmsPracticeByID(ctx context.Context, id int64) (LmsPractice
|
|||
}
|
||||
|
||||
const GetLmsPracticeByQuestionSetID = `-- name: GetLmsPracticeByQuestionSetID :one
|
||||
SELECT id, course_id, module_id, lesson_id, title, story_description, story_image, persona_id, question_set_id, quick_tips, created_at, updated_at, publish_status
|
||||
SELECT id, title, story_description, story_image, persona_id, question_set_id, quick_tips, created_at, updated_at, publish_status
|
||||
FROM lms_practices
|
||||
WHERE question_set_id = $1
|
||||
ORDER BY id DESC
|
||||
|
|
@ -153,9 +143,6 @@ func (q *Queries) GetLmsPracticeByQuestionSetID(ctx context.Context, questionSet
|
|||
var i LmsPractice
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CourseID,
|
||||
&i.ModuleID,
|
||||
&i.LessonID,
|
||||
&i.Title,
|
||||
&i.StoryDescription,
|
||||
&i.StoryImage,
|
||||
|
|
@ -173,9 +160,6 @@ const ListLmsPracticesByCourseID = `-- name: ListLmsPracticesByCourseID :many
|
|||
SELECT
|
||||
COUNT(*) OVER () AS total_count,
|
||||
p.id,
|
||||
p.course_id,
|
||||
p.module_id,
|
||||
p.lesson_id,
|
||||
p.title,
|
||||
p.story_description,
|
||||
p.story_image,
|
||||
|
|
@ -186,7 +170,9 @@ SELECT
|
|||
p.created_at,
|
||||
p.updated_at
|
||||
FROM lms_practices p
|
||||
WHERE p.course_id = $1
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = p.id
|
||||
WHERE lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id = $1
|
||||
AND (
|
||||
$4::boolean = FALSE
|
||||
OR p.publish_status = 'PUBLISHED'::TEXT
|
||||
|
|
@ -196,18 +182,15 @@ LIMIT $2 OFFSET $3
|
|||
`
|
||||
|
||||
type ListLmsPracticesByCourseIDParams struct {
|
||||
CourseID pgtype.Int8 `json:"course_id"`
|
||||
Limit int32 `json:"limit"`
|
||||
Offset int32 `json:"offset"`
|
||||
PublishedOnly bool `json:"published_only"`
|
||||
ParentID int64 `json:"parent_id"`
|
||||
Limit int32 `json:"limit"`
|
||||
Offset int32 `json:"offset"`
|
||||
PublishedOnly bool `json:"published_only"`
|
||||
}
|
||||
|
||||
type ListLmsPracticesByCourseIDRow struct {
|
||||
TotalCount int64 `json:"total_count"`
|
||||
ID int64 `json:"id"`
|
||||
CourseID pgtype.Int8 `json:"course_id"`
|
||||
ModuleID pgtype.Int8 `json:"module_id"`
|
||||
LessonID pgtype.Int8 `json:"lesson_id"`
|
||||
Title string `json:"title"`
|
||||
StoryDescription pgtype.Text `json:"story_description"`
|
||||
StoryImage pgtype.Text `json:"story_image"`
|
||||
|
|
@ -221,7 +204,7 @@ type ListLmsPracticesByCourseIDRow struct {
|
|||
|
||||
func (q *Queries) ListLmsPracticesByCourseID(ctx context.Context, arg ListLmsPracticesByCourseIDParams) ([]ListLmsPracticesByCourseIDRow, error) {
|
||||
rows, err := q.db.Query(ctx, ListLmsPracticesByCourseID,
|
||||
arg.CourseID,
|
||||
arg.ParentID,
|
||||
arg.Limit,
|
||||
arg.Offset,
|
||||
arg.PublishedOnly,
|
||||
|
|
@ -236,9 +219,6 @@ func (q *Queries) ListLmsPracticesByCourseID(ctx context.Context, arg ListLmsPra
|
|||
if err := rows.Scan(
|
||||
&i.TotalCount,
|
||||
&i.ID,
|
||||
&i.CourseID,
|
||||
&i.ModuleID,
|
||||
&i.LessonID,
|
||||
&i.Title,
|
||||
&i.StoryDescription,
|
||||
&i.StoryImage,
|
||||
|
|
@ -263,9 +243,6 @@ const ListLmsPracticesByLessonID = `-- name: ListLmsPracticesByLessonID :many
|
|||
SELECT
|
||||
COUNT(*) OVER () AS total_count,
|
||||
p.id,
|
||||
p.course_id,
|
||||
p.module_id,
|
||||
p.lesson_id,
|
||||
p.title,
|
||||
p.story_description,
|
||||
p.story_image,
|
||||
|
|
@ -276,7 +253,9 @@ SELECT
|
|||
p.created_at,
|
||||
p.updated_at
|
||||
FROM lms_practices p
|
||||
WHERE p.lesson_id = $1
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = p.id
|
||||
WHERE lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id = $1
|
||||
AND (
|
||||
$4::boolean = FALSE
|
||||
OR p.publish_status = 'PUBLISHED'::TEXT
|
||||
|
|
@ -286,18 +265,15 @@ LIMIT $2 OFFSET $3
|
|||
`
|
||||
|
||||
type ListLmsPracticesByLessonIDParams struct {
|
||||
LessonID pgtype.Int8 `json:"lesson_id"`
|
||||
Limit int32 `json:"limit"`
|
||||
Offset int32 `json:"offset"`
|
||||
PublishedOnly bool `json:"published_only"`
|
||||
ParentID int64 `json:"parent_id"`
|
||||
Limit int32 `json:"limit"`
|
||||
Offset int32 `json:"offset"`
|
||||
PublishedOnly bool `json:"published_only"`
|
||||
}
|
||||
|
||||
type ListLmsPracticesByLessonIDRow struct {
|
||||
TotalCount int64 `json:"total_count"`
|
||||
ID int64 `json:"id"`
|
||||
CourseID pgtype.Int8 `json:"course_id"`
|
||||
ModuleID pgtype.Int8 `json:"module_id"`
|
||||
LessonID pgtype.Int8 `json:"lesson_id"`
|
||||
Title string `json:"title"`
|
||||
StoryDescription pgtype.Text `json:"story_description"`
|
||||
StoryImage pgtype.Text `json:"story_image"`
|
||||
|
|
@ -311,7 +287,7 @@ type ListLmsPracticesByLessonIDRow struct {
|
|||
|
||||
func (q *Queries) ListLmsPracticesByLessonID(ctx context.Context, arg ListLmsPracticesByLessonIDParams) ([]ListLmsPracticesByLessonIDRow, error) {
|
||||
rows, err := q.db.Query(ctx, ListLmsPracticesByLessonID,
|
||||
arg.LessonID,
|
||||
arg.ParentID,
|
||||
arg.Limit,
|
||||
arg.Offset,
|
||||
arg.PublishedOnly,
|
||||
|
|
@ -326,9 +302,6 @@ func (q *Queries) ListLmsPracticesByLessonID(ctx context.Context, arg ListLmsPra
|
|||
if err := rows.Scan(
|
||||
&i.TotalCount,
|
||||
&i.ID,
|
||||
&i.CourseID,
|
||||
&i.ModuleID,
|
||||
&i.LessonID,
|
||||
&i.Title,
|
||||
&i.StoryDescription,
|
||||
&i.StoryImage,
|
||||
|
|
@ -353,9 +326,6 @@ const ListLmsPracticesByModuleID = `-- name: ListLmsPracticesByModuleID :many
|
|||
SELECT
|
||||
COUNT(*) OVER () AS total_count,
|
||||
p.id,
|
||||
p.course_id,
|
||||
p.module_id,
|
||||
p.lesson_id,
|
||||
p.title,
|
||||
p.story_description,
|
||||
p.story_image,
|
||||
|
|
@ -366,7 +336,9 @@ SELECT
|
|||
p.created_at,
|
||||
p.updated_at
|
||||
FROM lms_practices p
|
||||
WHERE p.module_id = $1
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = p.id
|
||||
WHERE lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id = $1
|
||||
AND (
|
||||
$4::boolean = FALSE
|
||||
OR p.publish_status = 'PUBLISHED'::TEXT
|
||||
|
|
@ -376,18 +348,15 @@ LIMIT $2 OFFSET $3
|
|||
`
|
||||
|
||||
type ListLmsPracticesByModuleIDParams struct {
|
||||
ModuleID pgtype.Int8 `json:"module_id"`
|
||||
Limit int32 `json:"limit"`
|
||||
Offset int32 `json:"offset"`
|
||||
PublishedOnly bool `json:"published_only"`
|
||||
ParentID int64 `json:"parent_id"`
|
||||
Limit int32 `json:"limit"`
|
||||
Offset int32 `json:"offset"`
|
||||
PublishedOnly bool `json:"published_only"`
|
||||
}
|
||||
|
||||
type ListLmsPracticesByModuleIDRow struct {
|
||||
TotalCount int64 `json:"total_count"`
|
||||
ID int64 `json:"id"`
|
||||
CourseID pgtype.Int8 `json:"course_id"`
|
||||
ModuleID pgtype.Int8 `json:"module_id"`
|
||||
LessonID pgtype.Int8 `json:"lesson_id"`
|
||||
Title string `json:"title"`
|
||||
StoryDescription pgtype.Text `json:"story_description"`
|
||||
StoryImage pgtype.Text `json:"story_image"`
|
||||
|
|
@ -401,7 +370,7 @@ type ListLmsPracticesByModuleIDRow struct {
|
|||
|
||||
func (q *Queries) ListLmsPracticesByModuleID(ctx context.Context, arg ListLmsPracticesByModuleIDParams) ([]ListLmsPracticesByModuleIDRow, error) {
|
||||
rows, err := q.db.Query(ctx, ListLmsPracticesByModuleID,
|
||||
arg.ModuleID,
|
||||
arg.ParentID,
|
||||
arg.Limit,
|
||||
arg.Offset,
|
||||
arg.PublishedOnly,
|
||||
|
|
@ -416,9 +385,6 @@ func (q *Queries) ListLmsPracticesByModuleID(ctx context.Context, arg ListLmsPra
|
|||
if err := rows.Scan(
|
||||
&i.TotalCount,
|
||||
&i.ID,
|
||||
&i.CourseID,
|
||||
&i.ModuleID,
|
||||
&i.LessonID,
|
||||
&i.Title,
|
||||
&i.StoryDescription,
|
||||
&i.StoryImage,
|
||||
|
|
@ -451,7 +417,7 @@ SET
|
|||
publish_status = COALESCE($7::varchar, publish_status),
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = $8
|
||||
RETURNING id, course_id, module_id, lesson_id, title, story_description, story_image, persona_id, question_set_id, quick_tips, created_at, updated_at, publish_status
|
||||
RETURNING id, title, story_description, story_image, persona_id, question_set_id, quick_tips, created_at, updated_at, publish_status
|
||||
`
|
||||
|
||||
type UpdateLmsPracticeParams struct {
|
||||
|
|
@ -479,9 +445,6 @@ func (q *Queries) UpdateLmsPractice(ctx context.Context, arg UpdateLmsPracticePa
|
|||
var i LmsPractice
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CourseID,
|
||||
&i.ModuleID,
|
||||
&i.LessonID,
|
||||
&i.Title,
|
||||
&i.StoryDescription,
|
||||
&i.StoryImage,
|
||||
|
|
|
|||
|
|
@ -7,8 +7,6 @@ package dbgen
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const CountCoursesInProgram = `-- name: CountCoursesInProgram :one
|
||||
|
|
@ -106,17 +104,19 @@ SELECT
|
|||
count(*)::int AS n
|
||||
FROM
|
||||
lms_practices lp
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
lp.course_id = $1
|
||||
lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id = $1
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
`
|
||||
|
||||
// Published practices directly attached to course_id (not via module_id/lesson_id).
|
||||
func (q *Queries) CountPublishedDirectPracticesInCourse(ctx context.Context, courseID pgtype.Int8) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountPublishedDirectPracticesInCourse, courseID)
|
||||
func (q *Queries) CountPublishedDirectPracticesInCourse(ctx context.Context, parentID int64) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountPublishedDirectPracticesInCourse, parentID)
|
||||
var n int32
|
||||
err := row.Scan(&n)
|
||||
return n, err
|
||||
|
|
@ -127,17 +127,19 @@ SELECT
|
|||
count(*)::int AS n
|
||||
FROM
|
||||
lms_practices lp
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
lp.module_id = $1
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id = $1
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
`
|
||||
|
||||
// Published practices directly attached to module_id (not via lesson_id).
|
||||
func (q *Queries) CountPublishedDirectPracticesInModule(ctx context.Context, moduleID pgtype.Int8) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountPublishedDirectPracticesInModule, moduleID)
|
||||
func (q *Queries) CountPublishedDirectPracticesInModule(ctx context.Context, parentID int64) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountPublishedDirectPracticesInModule, parentID)
|
||||
var n int32
|
||||
err := row.Scan(&n)
|
||||
return n, err
|
||||
|
|
@ -150,33 +152,44 @@ FROM
|
|||
lms_practices lp
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
(
|
||||
lp.course_id = $1
|
||||
OR lp.module_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
modules
|
||||
WHERE
|
||||
course_id = $1
|
||||
AND publish_status = 'PUBLISHED')
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'))
|
||||
EXISTS (
|
||||
SELECT
|
||||
1
|
||||
FROM
|
||||
lms_practice_parents lpp
|
||||
WHERE
|
||||
lpp.practice_id = lp.id
|
||||
AND (
|
||||
(lpp.parent_kind = 'COURSE' AND lpp.parent_id = $1)
|
||||
OR (
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
modules
|
||||
WHERE
|
||||
course_id = $1
|
||||
AND publish_status = 'PUBLISHED'))
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'))))
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
`
|
||||
|
||||
func (q *Queries) CountPublishedPracticesInCourse(ctx context.Context, courseID pgtype.Int8) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountPublishedPracticesInCourse, courseID)
|
||||
func (q *Queries) CountPublishedPracticesInCourse(ctx context.Context, parentID int64) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountPublishedPracticesInCourse, parentID)
|
||||
var n int32
|
||||
err := row.Scan(&n)
|
||||
return n, err
|
||||
|
|
@ -187,16 +200,18 @@ SELECT
|
|||
count(*)::int AS n
|
||||
FROM
|
||||
lms_practices lp
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
lp.lesson_id = $1
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id = $1
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
`
|
||||
|
||||
func (q *Queries) CountPublishedPracticesInLesson(ctx context.Context, lessonID pgtype.Int8) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountPublishedPracticesInLesson, lessonID)
|
||||
func (q *Queries) CountPublishedPracticesInLesson(ctx context.Context, parentID int64) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountPublishedPracticesInLesson, parentID)
|
||||
var n int32
|
||||
err := row.Scan(&n)
|
||||
return n, err
|
||||
|
|
@ -209,24 +224,33 @@ FROM
|
|||
lms_practices lp
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
(
|
||||
lp.module_id = $1
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
lessons
|
||||
WHERE
|
||||
module_id = $1
|
||||
AND publish_status = 'PUBLISHED'))
|
||||
EXISTS (
|
||||
SELECT
|
||||
1
|
||||
FROM
|
||||
lms_practice_parents lpp
|
||||
WHERE
|
||||
lpp.practice_id = lp.id
|
||||
AND (
|
||||
(lpp.parent_kind = 'MODULE' AND lpp.parent_id = $1)
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
lessons
|
||||
WHERE
|
||||
module_id = $1
|
||||
AND publish_status = 'PUBLISHED'))))
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
`
|
||||
|
||||
// Published practices in a module (direct module practices and practices on published lessons in the module).
|
||||
func (q *Queries) CountPublishedPracticesInModule(ctx context.Context, moduleID pgtype.Int8) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountPublishedPracticesInModule, moduleID)
|
||||
func (q *Queries) CountPublishedPracticesInModule(ctx context.Context, parentID int64) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountPublishedPracticesInModule, parentID)
|
||||
var n int32
|
||||
err := row.Scan(&n)
|
||||
return n, err
|
||||
|
|
@ -239,37 +263,50 @@ FROM
|
|||
lms_practices lp
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
(
|
||||
lp.course_id IN (
|
||||
SELECT
|
||||
c.id
|
||||
FROM
|
||||
courses c
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND c.publish_status = 'PUBLISHED')
|
||||
OR lp.module_id IN (
|
||||
SELECT
|
||||
m.id
|
||||
FROM
|
||||
modules m
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED')
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED'))
|
||||
EXISTS (
|
||||
SELECT
|
||||
1
|
||||
FROM
|
||||
lms_practice_parents lpp
|
||||
WHERE
|
||||
lpp.practice_id = lp.id
|
||||
AND (
|
||||
(
|
||||
lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
c.id
|
||||
FROM
|
||||
courses c
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND c.publish_status = 'PUBLISHED'))
|
||||
OR (
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
m.id
|
||||
FROM
|
||||
modules m
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED'))
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED'))))
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
|
|
@ -410,10 +447,12 @@ SELECT
|
|||
count(*)::int AS n
|
||||
FROM
|
||||
lms_practices lp
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
INNER JOIN user_practice_progress upp ON upp.question_set_id = lp.question_set_id
|
||||
WHERE
|
||||
lp.course_id = $1
|
||||
lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id = $1
|
||||
AND upp.user_id = $2
|
||||
AND upp.completed_at IS NOT NULL
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
|
|
@ -422,12 +461,12 @@ WHERE
|
|||
`
|
||||
|
||||
type CountUserCompletedPublishedDirectPracticesInCourseParams struct {
|
||||
CourseID pgtype.Int8 `json:"course_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
ParentID int64 `json:"parent_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) CountUserCompletedPublishedDirectPracticesInCourse(ctx context.Context, arg CountUserCompletedPublishedDirectPracticesInCourseParams) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountUserCompletedPublishedDirectPracticesInCourse, arg.CourseID, arg.UserID)
|
||||
row := q.db.QueryRow(ctx, CountUserCompletedPublishedDirectPracticesInCourse, arg.ParentID, arg.UserID)
|
||||
var n int32
|
||||
err := row.Scan(&n)
|
||||
return n, err
|
||||
|
|
@ -438,10 +477,12 @@ SELECT
|
|||
count(*)::int AS n
|
||||
FROM
|
||||
lms_practices lp
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
INNER JOIN user_practice_progress upp ON upp.question_set_id = lp.question_set_id
|
||||
WHERE
|
||||
lp.module_id = $1
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id = $1
|
||||
AND upp.user_id = $2
|
||||
AND upp.completed_at IS NOT NULL
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
|
|
@ -450,12 +491,12 @@ WHERE
|
|||
`
|
||||
|
||||
type CountUserCompletedPublishedDirectPracticesInModuleParams struct {
|
||||
ModuleID pgtype.Int8 `json:"module_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
ParentID int64 `json:"parent_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) CountUserCompletedPublishedDirectPracticesInModule(ctx context.Context, arg CountUserCompletedPublishedDirectPracticesInModuleParams) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountUserCompletedPublishedDirectPracticesInModule, arg.ModuleID, arg.UserID)
|
||||
row := q.db.QueryRow(ctx, CountUserCompletedPublishedDirectPracticesInModule, arg.ParentID, arg.UserID)
|
||||
var n int32
|
||||
err := row.Scan(&n)
|
||||
return n, err
|
||||
|
|
@ -469,26 +510,37 @@ FROM
|
|||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
INNER JOIN user_practice_progress upp ON upp.question_set_id = lp.question_set_id
|
||||
WHERE
|
||||
(
|
||||
lp.course_id = $1
|
||||
OR lp.module_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
modules
|
||||
WHERE
|
||||
course_id = $1
|
||||
AND publish_status = 'PUBLISHED')
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'))
|
||||
EXISTS (
|
||||
SELECT
|
||||
1
|
||||
FROM
|
||||
lms_practice_parents lpp
|
||||
WHERE
|
||||
lpp.practice_id = lp.id
|
||||
AND (
|
||||
(lpp.parent_kind = 'COURSE' AND lpp.parent_id = $1)
|
||||
OR (
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
modules
|
||||
WHERE
|
||||
course_id = $1
|
||||
AND publish_status = 'PUBLISHED'))
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'))))
|
||||
AND upp.user_id = $2
|
||||
AND upp.completed_at IS NOT NULL
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
|
|
@ -497,12 +549,12 @@ WHERE
|
|||
`
|
||||
|
||||
type CountUserCompletedPublishedPracticesInCourseParams struct {
|
||||
CourseID pgtype.Int8 `json:"course_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
ParentID int64 `json:"parent_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) CountUserCompletedPublishedPracticesInCourse(ctx context.Context, arg CountUserCompletedPublishedPracticesInCourseParams) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountUserCompletedPublishedPracticesInCourse, arg.CourseID, arg.UserID)
|
||||
row := q.db.QueryRow(ctx, CountUserCompletedPublishedPracticesInCourse, arg.ParentID, arg.UserID)
|
||||
var n int32
|
||||
err := row.Scan(&n)
|
||||
return n, err
|
||||
|
|
@ -513,10 +565,12 @@ SELECT
|
|||
count(*)::int AS n
|
||||
FROM
|
||||
lms_practices lp
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
INNER JOIN user_practice_progress upp ON upp.question_set_id = lp.question_set_id
|
||||
WHERE
|
||||
lp.lesson_id = $1
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id = $1
|
||||
AND upp.user_id = $2
|
||||
AND upp.completed_at IS NOT NULL
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
|
|
@ -525,12 +579,12 @@ WHERE
|
|||
`
|
||||
|
||||
type CountUserCompletedPublishedPracticesInLessonParams struct {
|
||||
LessonID pgtype.Int8 `json:"lesson_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
ParentID int64 `json:"parent_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) CountUserCompletedPublishedPracticesInLesson(ctx context.Context, arg CountUserCompletedPublishedPracticesInLessonParams) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountUserCompletedPublishedPracticesInLesson, arg.LessonID, arg.UserID)
|
||||
row := q.db.QueryRow(ctx, CountUserCompletedPublishedPracticesInLesson, arg.ParentID, arg.UserID)
|
||||
var n int32
|
||||
err := row.Scan(&n)
|
||||
return n, err
|
||||
|
|
@ -544,16 +598,25 @@ FROM
|
|||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
INNER JOIN user_practice_progress upp ON upp.question_set_id = lp.question_set_id
|
||||
WHERE
|
||||
(
|
||||
lp.module_id = $1
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
lessons
|
||||
WHERE
|
||||
module_id = $1
|
||||
AND publish_status = 'PUBLISHED'))
|
||||
EXISTS (
|
||||
SELECT
|
||||
1
|
||||
FROM
|
||||
lms_practice_parents lpp
|
||||
WHERE
|
||||
lpp.practice_id = lp.id
|
||||
AND (
|
||||
(lpp.parent_kind = 'MODULE' AND lpp.parent_id = $1)
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
lessons
|
||||
WHERE
|
||||
module_id = $1
|
||||
AND publish_status = 'PUBLISHED'))))
|
||||
AND upp.user_id = $2
|
||||
AND upp.completed_at IS NOT NULL
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
|
|
@ -562,12 +625,12 @@ WHERE
|
|||
`
|
||||
|
||||
type CountUserCompletedPublishedPracticesInModuleParams struct {
|
||||
ModuleID pgtype.Int8 `json:"module_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
ParentID int64 `json:"parent_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) CountUserCompletedPublishedPracticesInModule(ctx context.Context, arg CountUserCompletedPublishedPracticesInModuleParams) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, CountUserCompletedPublishedPracticesInModule, arg.ModuleID, arg.UserID)
|
||||
row := q.db.QueryRow(ctx, CountUserCompletedPublishedPracticesInModule, arg.ParentID, arg.UserID)
|
||||
var n int32
|
||||
err := row.Scan(&n)
|
||||
return n, err
|
||||
|
|
@ -581,37 +644,50 @@ FROM
|
|||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
INNER JOIN user_practice_progress upp ON upp.question_set_id = lp.question_set_id
|
||||
WHERE
|
||||
(
|
||||
lp.course_id IN (
|
||||
SELECT
|
||||
c.id
|
||||
FROM
|
||||
courses c
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND c.publish_status = 'PUBLISHED')
|
||||
OR lp.module_id IN (
|
||||
SELECT
|
||||
m.id
|
||||
FROM
|
||||
modules m
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED')
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED'))
|
||||
EXISTS (
|
||||
SELECT
|
||||
1
|
||||
FROM
|
||||
lms_practice_parents lpp
|
||||
WHERE
|
||||
lpp.practice_id = lp.id
|
||||
AND (
|
||||
(
|
||||
lpp.parent_kind = 'COURSE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
c.id
|
||||
FROM
|
||||
courses c
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND c.publish_status = 'PUBLISHED'))
|
||||
OR (
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
m.id
|
||||
FROM
|
||||
modules m
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED'))
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
WHERE
|
||||
c.program_id = $1
|
||||
AND l.publish_status = 'PUBLISHED'
|
||||
AND m.publish_status = 'PUBLISHED'
|
||||
AND c.publish_status = 'PUBLISHED'))))
|
||||
AND upp.user_id = $2
|
||||
AND upp.completed_at IS NOT NULL
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
|
|
@ -631,40 +707,6 @@ func (q *Queries) CountUserCompletedPublishedPracticesInProgram(ctx context.Cont
|
|||
return n, err
|
||||
}
|
||||
|
||||
const GetPracticeScopeByQuestionSetID = `-- name: GetPracticeScopeByQuestionSetID :one
|
||||
SELECT
|
||||
id,
|
||||
course_id,
|
||||
module_id,
|
||||
lesson_id
|
||||
FROM
|
||||
lms_practices
|
||||
WHERE
|
||||
question_set_id = $1
|
||||
ORDER BY
|
||||
id DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
type GetPracticeScopeByQuestionSetIDRow struct {
|
||||
ID int64 `json:"id"`
|
||||
CourseID pgtype.Int8 `json:"course_id"`
|
||||
ModuleID pgtype.Int8 `json:"module_id"`
|
||||
LessonID pgtype.Int8 `json:"lesson_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetPracticeScopeByQuestionSetID(ctx context.Context, questionSetID int64) (GetPracticeScopeByQuestionSetIDRow, error) {
|
||||
row := q.db.QueryRow(ctx, GetPracticeScopeByQuestionSetID, questionSetID)
|
||||
var i GetPracticeScopeByQuestionSetIDRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CourseID,
|
||||
&i.ModuleID,
|
||||
&i.LessonID,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const GetPreviousCourseInProgram = `-- name: GetPreviousCourseInProgram :one
|
||||
SELECT
|
||||
c2.id, c2.program_id, c2.name, c2.description, c2.thumbnail, c2.created_at, c2.updated_at, c2.sort_order, c2.publish_status, c2.access_tier
|
||||
|
|
@ -969,23 +1011,28 @@ FROM (
|
|||
lp.question_set_id
|
||||
FROM
|
||||
courses c
|
||||
INNER JOIN lms_practices lp ON (
|
||||
lp.course_id = c.id
|
||||
OR lp.module_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
modules
|
||||
WHERE
|
||||
course_id = c.id)
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = c.id))
|
||||
INNER JOIN lms_practice_parents lpp ON (
|
||||
(lpp.parent_kind = 'COURSE' AND lpp.parent_id = c.id)
|
||||
OR (
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
modules
|
||||
WHERE
|
||||
course_id = c.id))
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = c.id)))
|
||||
INNER JOIN lms_practices lp ON lp.id = lpp.practice_id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
qs.set_type = 'PRACTICE'
|
||||
|
|
@ -1026,37 +1073,38 @@ func (q *Queries) ListLMSCompletedCourseIDsByUser(ctx context.Context, userID in
|
|||
|
||||
const ListLMSCompletedLessonIDsByUser = `-- name: ListLMSCompletedLessonIDsByUser :many
|
||||
SELECT
|
||||
lp.lesson_id
|
||||
lpp.parent_id AS lesson_id
|
||||
FROM
|
||||
lms_practices AS lp
|
||||
lms_practice_parents lpp
|
||||
INNER JOIN lms_practices AS lp ON lp.id = lpp.practice_id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
LEFT JOIN user_practice_progress upp ON upp.question_set_id = lp.question_set_id
|
||||
AND upp.user_id = $1
|
||||
AND upp.completed_at IS NOT NULL
|
||||
WHERE
|
||||
lp.lesson_id IS NOT NULL
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
GROUP BY
|
||||
lp.lesson_id
|
||||
lpp.parent_id
|
||||
HAVING
|
||||
count(DISTINCT lp.question_set_id) > 0
|
||||
AND count(DISTINCT upp.question_set_id) >= count(DISTINCT lp.question_set_id)
|
||||
ORDER BY
|
||||
max(upp.completed_at) ASC,
|
||||
lp.lesson_id ASC
|
||||
lpp.parent_id ASC
|
||||
`
|
||||
|
||||
func (q *Queries) ListLMSCompletedLessonIDsByUser(ctx context.Context, userID int64) ([]pgtype.Int8, error) {
|
||||
func (q *Queries) ListLMSCompletedLessonIDsByUser(ctx context.Context, userID int64) ([]int64, error) {
|
||||
rows, err := q.db.Query(ctx, ListLMSCompletedLessonIDsByUser, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []pgtype.Int8
|
||||
var items []int64
|
||||
for rows.Next() {
|
||||
var lesson_id pgtype.Int8
|
||||
var lesson_id int64
|
||||
if err := rows.Scan(&lesson_id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -1077,15 +1125,18 @@ FROM (
|
|||
lp.question_set_id
|
||||
FROM
|
||||
modules m
|
||||
INNER JOIN lms_practices lp ON (
|
||||
lp.module_id = m.id
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
lessons
|
||||
WHERE
|
||||
module_id = m.id))
|
||||
INNER JOIN lms_practice_parents lpp ON (
|
||||
(lpp.parent_kind = 'MODULE' AND lpp.parent_id = m.id)
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
lessons
|
||||
WHERE
|
||||
module_id = m.id)))
|
||||
INNER JOIN lms_practices lp ON lp.id = lpp.practice_id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
qs.set_type = 'PRACTICE'
|
||||
|
|
@ -1133,23 +1184,28 @@ FROM (
|
|||
lp.question_set_id
|
||||
FROM
|
||||
courses c
|
||||
INNER JOIN lms_practices lp ON (
|
||||
lp.course_id = c.id
|
||||
OR lp.module_id IN (
|
||||
SELECT
|
||||
m.id
|
||||
FROM
|
||||
modules m
|
||||
WHERE
|
||||
m.course_id = c.id)
|
||||
OR lp.lesson_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = c.id))
|
||||
INNER JOIN lms_practice_parents lpp ON (
|
||||
(lpp.parent_kind = 'COURSE' AND lpp.parent_id = c.id)
|
||||
OR (
|
||||
lpp.parent_kind = 'MODULE'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
m.id
|
||||
FROM
|
||||
modules m
|
||||
WHERE
|
||||
m.course_id = c.id))
|
||||
OR (
|
||||
lpp.parent_kind = 'LESSON'
|
||||
AND lpp.parent_id IN (
|
||||
SELECT
|
||||
l.id
|
||||
FROM
|
||||
lessons l
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
WHERE
|
||||
m.course_id = c.id)))
|
||||
INNER JOIN lms_practices lp ON lp.id = lpp.practice_id
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
qs.set_type = 'PRACTICE'
|
||||
|
|
|
|||
|
|
@ -188,9 +188,6 @@ type LmsPersona struct {
|
|||
|
||||
type LmsPractice struct {
|
||||
ID int64 `json:"id"`
|
||||
CourseID pgtype.Int8 `json:"course_id"`
|
||||
ModuleID pgtype.Int8 `json:"module_id"`
|
||||
LessonID pgtype.Int8 `json:"lesson_id"`
|
||||
Title string `json:"title"`
|
||||
StoryDescription pgtype.Text `json:"story_description"`
|
||||
StoryImage pgtype.Text `json:"story_image"`
|
||||
|
|
@ -202,6 +199,14 @@ type LmsPractice struct {
|
|||
PublishStatus string `json:"publish_status"`
|
||||
}
|
||||
|
||||
type LmsPracticeParent struct {
|
||||
ID int64 `json:"id"`
|
||||
PracticeID int64 `json:"practice_id"`
|
||||
ParentKind string `json:"parent_kind"`
|
||||
ParentID int64 `json:"parent_id"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type LmsUserCourseProgress struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
CourseID int64 `json:"course_id"`
|
||||
|
|
|
|||
|
|
@ -23,9 +23,6 @@ WITH matched AS (
|
|||
p.persona_id,
|
||||
p.quick_tips,
|
||||
p.publish_status,
|
||||
p.course_id,
|
||||
p.module_id,
|
||||
p.lesson_id,
|
||||
NULL::bigint AS exam_prep_lesson_id,
|
||||
p.created_at,
|
||||
p.updated_at,
|
||||
|
|
@ -77,10 +74,7 @@ WITH matched AS (
|
|||
p.persona_id,
|
||||
p.quick_tips,
|
||||
p.publish_status,
|
||||
NULL::bigint AS course_id,
|
||||
NULL::bigint AS module_id,
|
||||
NULL::bigint AS lesson_id,
|
||||
p.unit_module_lesson_id AS exam_prep_lesson_id,
|
||||
NULL::bigint AS exam_prep_lesson_id,
|
||||
p.created_at,
|
||||
p.updated_at,
|
||||
(
|
||||
|
|
@ -130,9 +124,6 @@ SELECT
|
|||
persona_id,
|
||||
quick_tips,
|
||||
publish_status,
|
||||
course_id,
|
||||
module_id,
|
||||
lesson_id,
|
||||
exam_prep_lesson_id,
|
||||
created_at,
|
||||
updated_at,
|
||||
|
|
@ -162,9 +153,6 @@ type ListPracticesByQuestionTypeDefinitionRow struct {
|
|||
PersonaID pgtype.Int8 `json:"persona_id"`
|
||||
QuickTips pgtype.Text `json:"quick_tips"`
|
||||
PublishStatus string `json:"publish_status"`
|
||||
CourseID pgtype.Int8 `json:"course_id"`
|
||||
ModuleID pgtype.Int8 `json:"module_id"`
|
||||
LessonID pgtype.Int8 `json:"lesson_id"`
|
||||
ExamPrepLessonID pgtype.Int8 `json:"exam_prep_lesson_id"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
|
|
@ -197,9 +185,6 @@ func (q *Queries) ListPracticesByQuestionTypeDefinition(ctx context.Context, arg
|
|||
&i.PersonaID,
|
||||
&i.QuickTips,
|
||||
&i.PublishStatus,
|
||||
&i.CourseID,
|
||||
&i.ModuleID,
|
||||
&i.LessonID,
|
||||
&i.ExamPrepLessonID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
|
|
|
|||
|
|
@ -222,12 +222,13 @@ FROM (
|
|||
FROM
|
||||
user_practice_progress upp
|
||||
INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
|
||||
AND lp.lesson_id IS NOT NULL
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'LESSON'
|
||||
INNER JOIN question_sets qs ON qs.id = upp.question_set_id
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
INNER JOIN lessons l ON l.id = lp.lesson_id
|
||||
INNER JOIN lessons l ON l.id = lpp.parent_id
|
||||
INNER JOIN modules m ON m.id = l.module_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
AND c.program_id = m.program_id
|
||||
|
|
@ -253,13 +254,13 @@ FROM (
|
|||
FROM
|
||||
user_practice_progress upp
|
||||
INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
|
||||
AND lp.module_id IS NOT NULL
|
||||
AND lp.lesson_id IS NULL
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'MODULE'
|
||||
INNER JOIN question_sets qs ON qs.id = upp.question_set_id
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
INNER JOIN modules m ON m.id = lp.module_id
|
||||
INNER JOIN modules m ON m.id = lpp.parent_id
|
||||
INNER JOIN courses c ON c.id = m.course_id
|
||||
AND c.program_id = m.program_id
|
||||
INNER JOIN programs p ON p.id = c.program_id
|
||||
|
|
@ -284,14 +285,13 @@ FROM (
|
|||
FROM
|
||||
user_practice_progress upp
|
||||
INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
|
||||
AND lp.course_id IS NOT NULL
|
||||
AND lp.module_id IS NULL
|
||||
AND lp.lesson_id IS NULL
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
INNER JOIN lms_practice_parents lpp ON lpp.practice_id = lp.id
|
||||
AND lpp.parent_kind = 'COURSE'
|
||||
INNER JOIN question_sets qs ON qs.id = upp.question_set_id
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
INNER JOIN courses c ON c.id = lp.course_id
|
||||
INNER JOIN courses c ON c.id = lpp.parent_id
|
||||
INNER JOIN programs p ON p.id = c.program_id
|
||||
WHERE
|
||||
upp.user_id = $1
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// ParentKind identifies which hierarchy entity owns a practice (exactly one).
|
||||
// ParentKind identifies which hierarchy entity a practice is linked to.
|
||||
type ParentKind string
|
||||
|
||||
const (
|
||||
|
|
@ -14,6 +14,12 @@ const (
|
|||
ParentKindLesson ParentKind = "LESSON"
|
||||
)
|
||||
|
||||
// PracticeParent links a practice to a course, module, or lesson.
|
||||
type PracticeParent struct {
|
||||
Kind ParentKind `json:"parent_kind"`
|
||||
ID int64 `json:"parent_id"`
|
||||
}
|
||||
|
||||
// PracticePublishStatus controls learner visibility for a practice shell (independent of question_set.status).
|
||||
type PracticePublishStatus string
|
||||
|
||||
|
|
@ -39,21 +45,20 @@ func PracticePublishStatusFromDB(raw string) PracticePublishStatus {
|
|||
return ParsePracticePublishStatusInput(raw)
|
||||
}
|
||||
|
||||
// Practice is question-set content (story, persona, tips) scoped to a course, module, or lesson.
|
||||
// Practice is question-set content (story, persona, tips) linked to one or more courses, modules, or lessons.
|
||||
type Practice struct {
|
||||
ID int64 `json:"id"`
|
||||
ParentKind ParentKind `json:"parent_kind"`
|
||||
ParentID int64 `json:"parent_id"`
|
||||
Title string `json:"title"`
|
||||
StoryDescription *string `json:"story_description,omitempty"`
|
||||
StoryImage *string `json:"story_image,omitempty"`
|
||||
PersonaID *int64 `json:"persona_id,omitempty"` // lms_personas.id when set
|
||||
QuestionSetID int64 `json:"question_set_id"`
|
||||
PublishStatus PracticePublishStatus `json:"publish_status"`
|
||||
QuickTips *string `json:"quick_tips,omitempty"`
|
||||
Access *PracticeAccess `json:"access,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at,omitempty"`
|
||||
ID int64 `json:"id"`
|
||||
Parents []PracticeParent `json:"parents"`
|
||||
Title string `json:"title"`
|
||||
StoryDescription *string `json:"story_description,omitempty"`
|
||||
StoryImage *string `json:"story_image,omitempty"`
|
||||
PersonaID *int64 `json:"persona_id,omitempty"` // lms_personas.id when set
|
||||
QuestionSetID int64 `json:"question_set_id"`
|
||||
PublishStatus PracticePublishStatus `json:"publish_status"`
|
||||
QuickTips *string `json:"quick_tips,omitempty"`
|
||||
Access *PracticeAccess `json:"access,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at,omitempty"`
|
||||
}
|
||||
|
||||
type PracticeAccess struct {
|
||||
|
|
@ -67,8 +72,11 @@ func (p Practice) VisibleToLearners() bool {
|
|||
}
|
||||
|
||||
type CreatePracticeInput struct {
|
||||
ParentKind ParentKind `json:"parent_kind" validate:"required,oneof=COURSE MODULE LESSON"`
|
||||
ParentID int64 `json:"parent_id" validate:"required,gt=0"`
|
||||
// Parents is the preferred way to attach a practice to one or more hierarchy entities.
|
||||
Parents []PracticeParent `json:"parents,omitempty" validate:"omitempty,dive"`
|
||||
// ParentKind and ParentID are accepted for backward compatibility when parents is omitted.
|
||||
ParentKind ParentKind `json:"parent_kind,omitempty" validate:"omitempty,oneof=COURSE MODULE LESSON"`
|
||||
ParentID int64 `json:"parent_id,omitempty" validate:"omitempty,gt=0"`
|
||||
Title *string `json:"title,omitempty"`
|
||||
StoryDescription *string `json:"story_description,omitempty"`
|
||||
StoryImage *string `json:"story_image,omitempty"`
|
||||
|
|
@ -79,12 +87,40 @@ type CreatePracticeInput struct {
|
|||
PublishStatus string `json:"publish_status,omitempty" validate:"omitempty,oneof=DRAFT draft PUBLISHED published"`
|
||||
}
|
||||
|
||||
type UpdatePracticeInput struct {
|
||||
Title *string `json:"title,omitempty"`
|
||||
StoryDescription *string `json:"story_description,omitempty"`
|
||||
StoryImage *string `json:"story_image,omitempty"`
|
||||
PersonaID *int64 `json:"persona_id,omitempty"`
|
||||
QuestionSetID *int64 `json:"question_set_id,omitempty"`
|
||||
QuickTips *string `json:"quick_tips,omitempty"`
|
||||
PublishStatus *string `json:"publish_status,omitempty" validate:"omitempty,oneof=DRAFT draft PUBLISHED published"`
|
||||
// ResolvedParents returns deduplicated parents from parents or the legacy single-parent fields.
|
||||
func (in CreatePracticeInput) ResolvedParents() []PracticeParent {
|
||||
if len(in.Parents) > 0 {
|
||||
return dedupePracticeParents(in.Parents)
|
||||
}
|
||||
if in.ParentKind != "" && in.ParentID > 0 {
|
||||
return []PracticeParent{{Kind: in.ParentKind, ID: in.ParentID}}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func dedupePracticeParents(parents []PracticeParent) []PracticeParent {
|
||||
seen := make(map[PracticeParent]struct{}, len(parents))
|
||||
out := make([]PracticeParent, 0, len(parents))
|
||||
for _, p := range parents {
|
||||
if p.Kind == "" || p.ID <= 0 {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[p]; ok {
|
||||
continue
|
||||
}
|
||||
seen[p] = struct{}{}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
type UpdatePracticeInput struct {
|
||||
Parents []PracticeParent `json:"parents,omitempty" validate:"omitempty,dive"`
|
||||
Title *string `json:"title,omitempty"`
|
||||
StoryDescription *string `json:"story_description,omitempty"`
|
||||
StoryImage *string `json:"story_image,omitempty"`
|
||||
PersonaID *int64 `json:"persona_id,omitempty"`
|
||||
QuestionSetID *int64 `json:"question_set_id,omitempty"`
|
||||
QuickTips *string `json:"quick_tips,omitempty"`
|
||||
PublishStatus *string `json:"publish_status,omitempty" validate:"omitempty,oneof=DRAFT draft PUBLISHED published"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,8 +14,7 @@ type PracticeByQuestionTypeDefinition struct {
|
|||
PersonaID *int64 `json:"persona_id,omitempty"`
|
||||
QuickTips *string `json:"quick_tips,omitempty"`
|
||||
PublishStatus PracticePublishStatus `json:"publish_status"`
|
||||
ParentKind *ParentKind `json:"parent_kind,omitempty"`
|
||||
ParentID *int64 `json:"parent_id,omitempty"`
|
||||
Parents []PracticeParent `json:"parents,omitempty"`
|
||||
ExamPrepLessonID *int64 `json:"exam_prep_lesson_id,omitempty"`
|
||||
MatchingQuestionCount int64 `json:"matching_question_count"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
|
|
|
|||
|
|
@ -16,12 +16,7 @@ type UserByID interface {
|
|||
}
|
||||
|
||||
type LmsPracticeStore interface {
|
||||
// courseID, moduleID, lessonID: exactly one non-nil, matching in.ParentKind / in.ParentID.
|
||||
CreateLmsPractice(
|
||||
ctx context.Context,
|
||||
in domain.CreatePracticeInput,
|
||||
courseID, moduleID, lessonID *int64,
|
||||
) (domain.Practice, error)
|
||||
CreateLmsPractice(ctx context.Context, in domain.CreatePracticeInput, parents []domain.PracticeParent) (domain.Practice, error)
|
||||
GetLmsPracticeByID(ctx context.Context, id int64) (domain.Practice, error)
|
||||
// TryGetLmsPracticeByQuestionSetID returns false when no LMS practice row references the question set.
|
||||
TryGetLmsPracticeByQuestionSetID(ctx context.Context, questionSetID int64) (domain.Practice, bool, error)
|
||||
|
|
@ -29,8 +24,8 @@ type LmsPracticeStore interface {
|
|||
ListLmsPracticesByModuleID(ctx context.Context, moduleID int64, publishedOnly bool, limit, offset int32) ([]domain.Practice, int64, error)
|
||||
ListLmsPracticesByLessonID(ctx context.Context, lessonID int64, publishedOnly bool, limit, offset int32) ([]domain.Practice, int64, error)
|
||||
UpdateLmsPractice(ctx context.Context, id int64, input domain.UpdatePracticeInput) (domain.Practice, error)
|
||||
ReplaceLmsPracticeParents(ctx context.Context, practiceID int64, parents []domain.PracticeParent) error
|
||||
DeleteLmsPractice(ctx context.Context, id int64) error
|
||||
CountLmsPracticesByCourseID(ctx context.Context, courseID int64) (int64, error)
|
||||
CountLmsPracticesByModuleID(ctx context.Context, moduleID int64) (int64, error)
|
||||
CountLmsPracticesByLessonID(ctx context.Context, lessonID int64) (int64, error)
|
||||
CountLmsPracticesByParent(ctx context.Context, kind domain.ParentKind, parentID int64) (int64, error)
|
||||
CountLmsPracticesByParentExcludingPractice(ctx context.Context, kind domain.ParentKind, parentID, practiceID int64) (int64, error)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,28 +40,27 @@ func (s *Store) LmsUserHasLessonProgress(ctx context.Context, userID, lessonID i
|
|||
|
||||
// LmsCountPublishedPracticesInLesson returns how many published practices are attached to a lesson.
|
||||
func (s *Store) LmsCountPublishedPracticesInLesson(ctx context.Context, lessonID int64) (int32, error) {
|
||||
return s.queries.CountPublishedPracticesInLesson(ctx, toPgInt8(&lessonID))
|
||||
return s.queries.CountPublishedPracticesInLesson(ctx, lessonID)
|
||||
}
|
||||
|
||||
// LmsCountPublishedPracticesInModule returns published practices in a module (direct + lesson-attached).
|
||||
func (s *Store) LmsCountPublishedPracticesInModule(ctx context.Context, moduleID int64) (int32, error) {
|
||||
return s.queries.CountPublishedPracticesInModule(ctx, toPgInt8(&moduleID))
|
||||
return s.queries.CountPublishedPracticesInModule(ctx, moduleID)
|
||||
}
|
||||
|
||||
// LmsCountPublishedPracticesInCourse returns published practices in a course (direct + module + lesson scope).
|
||||
func (s *Store) LmsCountPublishedPracticesInCourse(ctx context.Context, courseID int64) (int32, error) {
|
||||
return s.queries.CountPublishedPracticesInCourse(ctx, toPgInt8(&courseID))
|
||||
return s.queries.CountPublishedPracticesInCourse(ctx, courseID)
|
||||
}
|
||||
|
||||
// LmsUserPracticeProgressInLesson returns published practice completion counts scoped to a lesson.
|
||||
func (s *Store) LmsUserPracticeProgressInLesson(ctx context.Context, userID, lessonID int64) (completed, total int32, err error) {
|
||||
lessonIDPG := toPgInt8(&lessonID)
|
||||
total, err = s.queries.CountPublishedPracticesInLesson(ctx, lessonIDPG)
|
||||
total, err = s.queries.CountPublishedPracticesInLesson(ctx, lessonID)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
completed, err = s.queries.CountUserCompletedPublishedPracticesInLesson(ctx, dbgen.CountUserCompletedPublishedPracticesInLessonParams{
|
||||
LessonID: lessonIDPG,
|
||||
ParentID: lessonID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
@ -72,12 +71,12 @@ func (s *Store) LmsUserPracticeProgressInLesson(ctx context.Context, userID, les
|
|||
|
||||
// LmsUserLessonProgressInModule returns published practice completion counts in a module.
|
||||
func (s *Store) LmsUserLessonProgressInModule(ctx context.Context, userID, moduleID int64) (completed, total int32, err error) {
|
||||
total, err = s.queries.CountPublishedPracticesInModule(ctx, toPgInt8(&moduleID))
|
||||
total, err = s.queries.CountPublishedPracticesInModule(ctx, moduleID)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
completed, err = s.queries.CountUserCompletedPublishedPracticesInModule(ctx, dbgen.CountUserCompletedPublishedPracticesInModuleParams{
|
||||
ModuleID: toPgInt8(&moduleID),
|
||||
ParentID: moduleID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
@ -88,12 +87,12 @@ func (s *Store) LmsUserLessonProgressInModule(ctx context.Context, userID, modul
|
|||
|
||||
// LmsUserLessonProgressInCourse returns published practice completion counts in a course.
|
||||
func (s *Store) LmsUserLessonProgressInCourse(ctx context.Context, userID, courseID int64) (completed, total int32, err error) {
|
||||
total, err = s.queries.CountPublishedPracticesInCourse(ctx, toPgInt8(&courseID))
|
||||
total, err = s.queries.CountPublishedPracticesInCourse(ctx, courseID)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
completed, err = s.queries.CountUserCompletedPublishedPracticesInCourse(ctx, dbgen.CountUserCompletedPublishedPracticesInCourseParams{
|
||||
CourseID: toPgInt8(&courseID),
|
||||
ParentID: courseID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
@ -119,13 +118,12 @@ func (s *Store) LmsUserLessonProgressInProgram(ctx context.Context, userID, prog
|
|||
}
|
||||
|
||||
func (s *Store) LmsUserDirectPracticeProgressInModule(ctx context.Context, userID, moduleID int64) (completed, total int32, err error) {
|
||||
moduleIDPG := toPgInt8(&moduleID)
|
||||
total, err = s.queries.CountPublishedDirectPracticesInModule(ctx, moduleIDPG)
|
||||
total, err = s.queries.CountPublishedDirectPracticesInModule(ctx, moduleID)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
completed, err = s.queries.CountUserCompletedPublishedDirectPracticesInModule(ctx, dbgen.CountUserCompletedPublishedDirectPracticesInModuleParams{
|
||||
ModuleID: moduleIDPG,
|
||||
ParentID: moduleID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
@ -135,13 +133,12 @@ func (s *Store) LmsUserDirectPracticeProgressInModule(ctx context.Context, userI
|
|||
}
|
||||
|
||||
func (s *Store) LmsUserDirectPracticeProgressInCourse(ctx context.Context, userID, courseID int64) (completed, total int32, err error) {
|
||||
courseIDPG := toPgInt8(&courseID)
|
||||
total, err = s.queries.CountPublishedDirectPracticesInCourse(ctx, courseIDPG)
|
||||
total, err = s.queries.CountPublishedDirectPracticesInCourse(ctx, courseID)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
completed, err = s.queries.CountUserCompletedPublishedDirectPracticesInCourse(ctx, dbgen.CountUserCompletedPublishedDirectPracticesInCourseParams{
|
||||
CourseID: courseIDPG,
|
||||
ParentID: courseID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package repository
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
dbgen "Yimaru-Backend/gen/db"
|
||||
"Yimaru-Backend/internal/domain"
|
||||
|
|
@ -40,23 +41,29 @@ func derefString(p *string) string {
|
|||
return *p
|
||||
}
|
||||
|
||||
func lmsPracticeToDomain(p dbgen.LmsPractice) domain.Practice {
|
||||
func lmsPracticeParentFromDB(row dbgen.LmsPracticeParent) domain.PracticeParent {
|
||||
return domain.PracticeParent{
|
||||
Kind: domain.ParentKind(row.ParentKind),
|
||||
ID: row.ParentID,
|
||||
}
|
||||
}
|
||||
|
||||
func lmsPracticeParentsFromDB(rows []dbgen.LmsPracticeParent) []domain.PracticeParent {
|
||||
out := make([]domain.PracticeParent, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, lmsPracticeParentFromDB(row))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func lmsPracticeToDomain(p dbgen.LmsPractice, parents []domain.PracticeParent) domain.Practice {
|
||||
out := domain.Practice{
|
||||
ID: p.ID,
|
||||
Parents: parents,
|
||||
Title: p.Title,
|
||||
QuestionSetID: p.QuestionSetID,
|
||||
PublishStatus: domain.PracticePublishStatusFromDB(p.PublishStatus),
|
||||
}
|
||||
if p.CourseID.Valid {
|
||||
out.ParentKind = domain.ParentKindCourse
|
||||
out.ParentID = p.CourseID.Int64
|
||||
} else if p.ModuleID.Valid {
|
||||
out.ParentKind = domain.ParentKindModule
|
||||
out.ParentID = p.ModuleID.Int64
|
||||
} else if p.LessonID.Valid {
|
||||
out.ParentKind = domain.ParentKindLesson
|
||||
out.ParentID = p.LessonID.Int64
|
||||
}
|
||||
out.StoryDescription = fromPgText(p.StoryDescription)
|
||||
out.StoryImage = fromPgText(p.StoryImage)
|
||||
out.QuickTips = fromPgText(p.QuickTips)
|
||||
|
|
@ -69,42 +76,61 @@ func lmsPracticeToDomain(p dbgen.LmsPractice) domain.Practice {
|
|||
return out
|
||||
}
|
||||
|
||||
func lmsFromListRow(
|
||||
id, qid int64,
|
||||
publishStatus string,
|
||||
title string,
|
||||
cid, mid, lid pgtype.Int8,
|
||||
sd, si, qt pgtype.Text, pid pgtype.Int8,
|
||||
ca, ua pgtype.Timestamptz,
|
||||
) domain.Practice {
|
||||
return lmsPracticeToDomain(dbgen.LmsPractice{
|
||||
ID: id,
|
||||
CourseID: cid,
|
||||
ModuleID: mid,
|
||||
LessonID: lid,
|
||||
Title: title,
|
||||
StoryDescription: sd,
|
||||
StoryImage: si,
|
||||
PersonaID: pid,
|
||||
QuestionSetID: qid,
|
||||
QuickTips: qt,
|
||||
PublishStatus: publishStatus,
|
||||
CreatedAt: ca,
|
||||
UpdatedAt: ua,
|
||||
})
|
||||
func (s *Store) loadLmsPracticeParents(ctx context.Context, practiceID int64) ([]domain.PracticeParent, error) {
|
||||
rows, err := s.queries.ListLmsPracticeParentsByPracticeID(ctx, practiceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return lmsPracticeParentsFromDB(rows), nil
|
||||
}
|
||||
|
||||
func (s *Store) attachParentsToPractices(ctx context.Context, practices []domain.Practice) ([]domain.Practice, error) {
|
||||
if len(practices) == 0 {
|
||||
return practices, nil
|
||||
}
|
||||
ids := make([]int64, len(practices))
|
||||
indexByID := make(map[int64]int, len(practices))
|
||||
for i, p := range practices {
|
||||
ids[i] = p.ID
|
||||
indexByID[p.ID] = i
|
||||
}
|
||||
rows, err := s.queries.ListLmsPracticeParentsByPracticeIDs(ctx, ids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, row := range rows {
|
||||
idx := indexByID[row.PracticeID]
|
||||
practices[idx].Parents = append(practices[idx].Parents, lmsPracticeParentFromDB(row))
|
||||
}
|
||||
return practices, nil
|
||||
}
|
||||
|
||||
func (s *Store) insertLmsPracticeParents(ctx context.Context, q *dbgen.Queries, practiceID int64, parents []domain.PracticeParent) error {
|
||||
for _, parent := range parents {
|
||||
if _, err := q.InsertLmsPracticeParent(ctx, dbgen.InsertLmsPracticeParentParams{
|
||||
PracticeID: practiceID,
|
||||
ParentKind: string(parent.Kind),
|
||||
ParentID: parent.ID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateLmsPractice sets exactly one of courseID, moduleID, lessonID (non-nil).
|
||||
func (s *Store) CreateLmsPractice(
|
||||
ctx context.Context,
|
||||
in domain.CreatePracticeInput,
|
||||
courseID, moduleID, lessonID *int64,
|
||||
parents []domain.PracticeParent,
|
||||
) (domain.Practice, error) {
|
||||
ps := domain.ParsePracticePublishStatusInput(in.PublishStatus)
|
||||
p, err := s.queries.CreateLmsPractice(ctx, dbgen.CreateLmsPracticeParams{
|
||||
CourseID: int64PtrToPg8(courseID),
|
||||
ModuleID: int64PtrToPg8(moduleID),
|
||||
LessonID: int64PtrToPg8(lessonID),
|
||||
q, tx, err := s.BeginTx(ctx)
|
||||
if err != nil {
|
||||
return domain.Practice{}, fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
p, err := q.CreateLmsPractice(ctx, dbgen.CreateLmsPracticeParams{
|
||||
Title: derefString(in.Title),
|
||||
StoryDescription: toPgText(in.StoryDescription),
|
||||
StoryImage: toPgText(in.StoryImage),
|
||||
|
|
@ -116,7 +142,13 @@ func (s *Store) CreateLmsPractice(
|
|||
if err != nil {
|
||||
return domain.Practice{}, err
|
||||
}
|
||||
return lmsPracticeToDomain(p), nil
|
||||
if err := s.insertLmsPracticeParents(ctx, q, p.ID, parents); err != nil {
|
||||
return domain.Practice{}, err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return domain.Practice{}, fmt.Errorf("commit: %w", err)
|
||||
}
|
||||
return lmsPracticeToDomain(p, parents), nil
|
||||
}
|
||||
|
||||
func (s *Store) GetLmsPracticeByID(ctx context.Context, id int64) (domain.Practice, error) {
|
||||
|
|
@ -127,7 +159,11 @@ func (s *Store) GetLmsPracticeByID(ctx context.Context, id int64) (domain.Practi
|
|||
}
|
||||
return domain.Practice{}, err
|
||||
}
|
||||
return lmsPracticeToDomain(p), nil
|
||||
parents, err := s.loadLmsPracticeParents(ctx, id)
|
||||
if err != nil {
|
||||
return domain.Practice{}, err
|
||||
}
|
||||
return lmsPracticeToDomain(p, parents), nil
|
||||
}
|
||||
|
||||
// TryGetLmsPracticeByQuestionSetID returns false when no row exists.
|
||||
|
|
@ -139,12 +175,36 @@ func (s *Store) TryGetLmsPracticeByQuestionSetID(ctx context.Context, questionSe
|
|||
}
|
||||
return domain.Practice{}, false, err
|
||||
}
|
||||
return lmsPracticeToDomain(p), true, nil
|
||||
parents, err := s.loadLmsPracticeParents(ctx, p.ID)
|
||||
if err != nil {
|
||||
return domain.Practice{}, false, err
|
||||
}
|
||||
return lmsPracticeToDomain(p, parents), true, nil
|
||||
}
|
||||
|
||||
func lmsPracticeFromListRow(
|
||||
id, qid int64,
|
||||
publishStatus, title string,
|
||||
sd, si, qt pgtype.Text, pid pgtype.Int8,
|
||||
ca, ua pgtype.Timestamptz,
|
||||
) domain.Practice {
|
||||
return lmsPracticeToDomain(dbgen.LmsPractice{
|
||||
ID: id,
|
||||
Title: title,
|
||||
StoryDescription: sd,
|
||||
StoryImage: si,
|
||||
PersonaID: pid,
|
||||
QuestionSetID: qid,
|
||||
QuickTips: qt,
|
||||
PublishStatus: publishStatus,
|
||||
CreatedAt: ca,
|
||||
UpdatedAt: ua,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
func (s *Store) ListLmsPracticesByCourseID(ctx context.Context, courseID int64, publishedOnly bool, limit, offset int32) ([]domain.Practice, int64, error) {
|
||||
rows, err := s.queries.ListLmsPracticesByCourseID(ctx, dbgen.ListLmsPracticesByCourseIDParams{
|
||||
CourseID: pgtype.Int8{Int64: courseID, Valid: true},
|
||||
ParentID: courseID,
|
||||
PublishedOnly: publishedOnly,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
|
|
@ -161,19 +221,22 @@ func (s *Store) ListLmsPracticesByCourseID(ctx context.Context, courseID int64,
|
|||
if i == 0 {
|
||||
total = r.TotalCount
|
||||
}
|
||||
out = append(out, lmsFromListRow(
|
||||
out = append(out, lmsPracticeFromListRow(
|
||||
r.ID, r.QuestionSetID, r.PublishStatus, r.Title,
|
||||
r.CourseID, r.ModuleID, r.LessonID,
|
||||
r.StoryDescription, r.StoryImage, r.QuickTips,
|
||||
r.PersonaID, r.CreatedAt, r.UpdatedAt,
|
||||
))
|
||||
}
|
||||
out, err = s.attachParentsToPractices(ctx, out)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
|
||||
func (s *Store) ListLmsPracticesByModuleID(ctx context.Context, moduleID int64, publishedOnly bool, limit, offset int32) ([]domain.Practice, int64, error) {
|
||||
rows, err := s.queries.ListLmsPracticesByModuleID(ctx, dbgen.ListLmsPracticesByModuleIDParams{
|
||||
ModuleID: pgtype.Int8{Int64: moduleID, Valid: true},
|
||||
ParentID: moduleID,
|
||||
PublishedOnly: publishedOnly,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
|
|
@ -190,19 +253,22 @@ func (s *Store) ListLmsPracticesByModuleID(ctx context.Context, moduleID int64,
|
|||
if i == 0 {
|
||||
total = r.TotalCount
|
||||
}
|
||||
out = append(out, lmsFromListRow(
|
||||
out = append(out, lmsPracticeFromListRow(
|
||||
r.ID, r.QuestionSetID, r.PublishStatus, r.Title,
|
||||
r.CourseID, r.ModuleID, r.LessonID,
|
||||
r.StoryDescription, r.StoryImage, r.QuickTips,
|
||||
r.PersonaID, r.CreatedAt, r.UpdatedAt,
|
||||
))
|
||||
}
|
||||
out, err = s.attachParentsToPractices(ctx, out)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
|
||||
func (s *Store) ListLmsPracticesByLessonID(ctx context.Context, lessonID int64, publishedOnly bool, limit, offset int32) ([]domain.Practice, int64, error) {
|
||||
rows, err := s.queries.ListLmsPracticesByLessonID(ctx, dbgen.ListLmsPracticesByLessonIDParams{
|
||||
LessonID: pgtype.Int8{Int64: lessonID, Valid: true},
|
||||
ParentID: lessonID,
|
||||
PublishedOnly: publishedOnly,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
|
|
@ -219,13 +285,16 @@ func (s *Store) ListLmsPracticesByLessonID(ctx context.Context, lessonID int64,
|
|||
if i == 0 {
|
||||
total = r.TotalCount
|
||||
}
|
||||
out = append(out, lmsFromListRow(
|
||||
out = append(out, lmsPracticeFromListRow(
|
||||
r.ID, r.QuestionSetID, r.PublishStatus, r.Title,
|
||||
r.CourseID, r.ModuleID, r.LessonID,
|
||||
r.StoryDescription, r.StoryImage, r.QuickTips,
|
||||
r.PersonaID, r.CreatedAt, r.UpdatedAt,
|
||||
))
|
||||
}
|
||||
out, err = s.attachParentsToPractices(ctx, out)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
|
||||
|
|
@ -253,21 +322,44 @@ func (s *Store) UpdateLmsPractice(ctx context.Context, id int64, input domain.Up
|
|||
}
|
||||
return domain.Practice{}, err
|
||||
}
|
||||
return lmsPracticeToDomain(p), nil
|
||||
parents, err := s.loadLmsPracticeParents(ctx, id)
|
||||
if err != nil {
|
||||
return domain.Practice{}, err
|
||||
}
|
||||
return lmsPracticeToDomain(p, parents), nil
|
||||
}
|
||||
|
||||
func (s *Store) ReplaceLmsPracticeParents(ctx context.Context, practiceID int64, parents []domain.PracticeParent) error {
|
||||
q, tx, err := s.BeginTx(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
if err := q.DeleteLmsPracticeParentsByPracticeID(ctx, practiceID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.insertLmsPracticeParents(ctx, q, practiceID, parents); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) DeleteLmsPractice(ctx context.Context, id int64) error {
|
||||
return s.queries.DeleteLmsPractice(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Store) CountLmsPracticesByCourseID(ctx context.Context, courseID int64) (int64, error) {
|
||||
return s.queries.CountLmsPracticesByCourseID(ctx, int64PtrToPg8(&courseID))
|
||||
func (s *Store) CountLmsPracticesByParent(ctx context.Context, kind domain.ParentKind, parentID int64) (int64, error) {
|
||||
return s.queries.CountLmsPracticesByParent(ctx, dbgen.CountLmsPracticesByParentParams{
|
||||
ParentKind: string(kind),
|
||||
ParentID: parentID,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) CountLmsPracticesByModuleID(ctx context.Context, moduleID int64) (int64, error) {
|
||||
return s.queries.CountLmsPracticesByModuleID(ctx, int64PtrToPg8(&moduleID))
|
||||
}
|
||||
|
||||
func (s *Store) CountLmsPracticesByLessonID(ctx context.Context, lessonID int64) (int64, error) {
|
||||
return s.queries.CountLmsPracticesByLessonID(ctx, int64PtrToPg8(&lessonID))
|
||||
func (s *Store) CountLmsPracticesByParentExcludingPractice(ctx context.Context, kind domain.ParentKind, parentID, practiceID int64) (int64, error) {
|
||||
return s.queries.CountLmsPracticesByParentExcludingPractice(ctx, dbgen.CountLmsPracticesByParentExcludingPracticeParams{
|
||||
ParentKind: string(kind),
|
||||
ParentID: parentID,
|
||||
PracticeID: practiceID,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,12 +2,10 @@ package repository
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
dbgen "Yimaru-Backend/gen/db"
|
||||
"Yimaru-Backend/internal/domain"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// CompleteLessonForUser records lesson completion for sequential lesson gating and
|
||||
|
|
@ -48,10 +46,10 @@ func (s *Store) CompleteLessonForUser(ctx context.Context, userID, lessonID int6
|
|||
// CompletePracticeForUser records practice completion and cascades practice-based
|
||||
// completion upward when all published practices in scope are complete.
|
||||
func (s *Store) CompletePracticeForUser(ctx context.Context, userID, questionSetID int64) (domain.LMSPracticeCompletionResult, error) {
|
||||
var empty domain.LMSPracticeCompletionResult
|
||||
var result domain.LMSPracticeCompletionResult
|
||||
q, tx, err := s.BeginTx(ctx)
|
||||
if err != nil {
|
||||
return empty, fmt.Errorf("begin tx: %w", err)
|
||||
return result, fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
|
|
@ -59,76 +57,102 @@ func (s *Store) CompletePracticeForUser(ctx context.Context, userID, questionSet
|
|||
UserID: userID,
|
||||
QuestionSetID: questionSetID,
|
||||
}); err != nil {
|
||||
return empty, err
|
||||
return result, err
|
||||
}
|
||||
|
||||
scope, err := q.GetPracticeScopeByQuestionSetID(ctx, questionSetID)
|
||||
parents, err := q.ListLmsPracticeParentsByQuestionSetID(ctx, questionSetID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
// Exam-prep practices are not in lms_practices; completion is tracked in user_practice_progress only.
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return empty, fmt.Errorf("commit: %w", err)
|
||||
}
|
||||
return empty, nil
|
||||
}
|
||||
return empty, err
|
||||
return result, err
|
||||
}
|
||||
var (
|
||||
moduleID *int64
|
||||
courseID int64
|
||||
)
|
||||
switch {
|
||||
case scope.ModuleID.Valid:
|
||||
mid := scope.ModuleID.Int64
|
||||
moduleID = &mid
|
||||
mod, err := q.GetModuleByID(ctx, mid)
|
||||
if err != nil {
|
||||
return empty, err
|
||||
if len(parents) == 0 {
|
||||
// Exam-prep practices are not in lms_practices; completion is tracked in user_practice_progress only.
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return result, fmt.Errorf("commit: %w", err)
|
||||
}
|
||||
courseID = mod.CourseID
|
||||
case scope.LessonID.Valid:
|
||||
lesson, err := q.GetLessonByID(ctx, scope.LessonID.Int64)
|
||||
if err != nil {
|
||||
return empty, err
|
||||
}
|
||||
mid := lesson.ModuleID
|
||||
moduleID = &mid
|
||||
mod, err := q.GetModuleByID(ctx, mid)
|
||||
if err != nil {
|
||||
return empty, err
|
||||
}
|
||||
courseID = mod.CourseID
|
||||
case scope.CourseID.Valid:
|
||||
courseID = scope.CourseID.Int64
|
||||
default:
|
||||
return empty, fmt.Errorf("practice %d is not linked to lesson/module/course", questionSetID)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
crs, err := q.GetCourseByID(ctx, courseID)
|
||||
if err != nil {
|
||||
return empty, err
|
||||
scopes := make(map[cascadeScope]struct{})
|
||||
for _, parent := range parents {
|
||||
scope, err := resolvePracticeCascadeScope(ctx, q, parent.ParentKind, parent.ParentID)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
scopes[scope] = struct{}{}
|
||||
}
|
||||
|
||||
result, err := s.cascadeLMSCompletion(ctx, q, userID, moduleID, crs.ID, crs.ProgramID)
|
||||
if err != nil {
|
||||
return empty, err
|
||||
for scope := range scopes {
|
||||
crs, err := q.GetCourseByID(ctx, scope.courseID)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
part, err := s.cascadeLMSCompletion(ctx, q, userID, scope.moduleID, crs.ID, crs.ProgramID)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result = mergeLMSPracticeCompletionResults(result, part)
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return empty, fmt.Errorf("commit: %w", err)
|
||||
return result, fmt.Errorf("commit: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type cascadeScope struct {
|
||||
moduleID *int64
|
||||
courseID int64
|
||||
}
|
||||
|
||||
func resolvePracticeCascadeScope(ctx context.Context, q *dbgen.Queries, parentKind string, parentID int64) (cascadeScope, error) {
|
||||
switch domain.ParentKind(parentKind) {
|
||||
case domain.ParentKindModule:
|
||||
mod, err := q.GetModuleByID(ctx, parentID)
|
||||
if err != nil {
|
||||
return cascadeScope{}, err
|
||||
}
|
||||
mid := mod.ID
|
||||
return cascadeScope{moduleID: &mid, courseID: mod.CourseID}, nil
|
||||
case domain.ParentKindLesson:
|
||||
lesson, err := q.GetLessonByID(ctx, parentID)
|
||||
if err != nil {
|
||||
return cascadeScope{}, err
|
||||
}
|
||||
mid := lesson.ModuleID
|
||||
mod, err := q.GetModuleByID(ctx, mid)
|
||||
if err != nil {
|
||||
return cascadeScope{}, err
|
||||
}
|
||||
return cascadeScope{moduleID: &mid, courseID: mod.CourseID}, nil
|
||||
case domain.ParentKindCourse:
|
||||
return cascadeScope{courseID: parentID}, nil
|
||||
default:
|
||||
return cascadeScope{}, fmt.Errorf("unsupported practice parent kind %q", parentKind)
|
||||
}
|
||||
}
|
||||
|
||||
func mergeLMSPracticeCompletionResults(a, b domain.LMSPracticeCompletionResult) domain.LMSPracticeCompletionResult {
|
||||
if b.ModuleCompleted != nil {
|
||||
a.ModuleCompleted = b.ModuleCompleted
|
||||
}
|
||||
if b.CourseCompleted != nil {
|
||||
a.CourseCompleted = b.CourseCompleted
|
||||
}
|
||||
if b.ProgramCompleted != nil {
|
||||
a.ProgramCompleted = b.ProgramCompleted
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func (s *Store) cascadeLMSCompletion(ctx context.Context, q *dbgen.Queries, userID int64, moduleID *int64, courseID, programID int64) (domain.LMSPracticeCompletionResult, error) {
|
||||
var result domain.LMSPracticeCompletionResult
|
||||
if moduleID != nil {
|
||||
modulePracticesTotal, err := q.CountPublishedPracticesInModule(ctx, toPgInt8(moduleID))
|
||||
modulePracticesTotal, err := q.CountPublishedPracticesInModule(ctx, *moduleID)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
modulePracticesDone, err := q.CountUserCompletedPublishedPracticesInModule(ctx, dbgen.CountUserCompletedPublishedPracticesInModuleParams{
|
||||
ModuleID: toPgInt8(moduleID),
|
||||
ParentID: *moduleID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
@ -171,12 +195,12 @@ func (s *Store) cascadeLMSCompletion(ctx context.Context, q *dbgen.Queries, user
|
|||
return result, err
|
||||
}
|
||||
|
||||
coursePracticesTotal, err := q.CountPublishedPracticesInCourse(ctx, toPgInt8(&courseID))
|
||||
coursePracticesTotal, err := q.CountPublishedPracticesInCourse(ctx, courseID)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
coursePracticesDone, err := q.CountUserCompletedPublishedPracticesInCourse(ctx, dbgen.CountUserCompletedPublishedPracticesInCourseParams{
|
||||
CourseID: toPgInt8(&courseID),
|
||||
ParentID: courseID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import (
|
|||
|
||||
dbgen "Yimaru-Backend/gen/db"
|
||||
"Yimaru-Backend/internal/domain"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
// GetLMSUserProgressSnapshot returns practice-based completed lesson, module, course,
|
||||
|
|
@ -28,24 +27,13 @@ func (s *Store) GetLMSUserProgressSnapshot(ctx context.Context, userID int64) (d
|
|||
return domain.LMSUserProgress{}, err
|
||||
}
|
||||
return domain.LMSUserProgress{
|
||||
LessonIDs: pgInt8IDsToInt64(lessons),
|
||||
LessonIDs: int64IDsOrEmpty(lessons),
|
||||
ModuleIDs: int64IDsOrEmpty(mods),
|
||||
CourseIDs: int64IDsOrEmpty(courses),
|
||||
ProgramIDs: int64IDsOrEmpty(programs),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func pgInt8IDsToInt64(items []pgtype.Int8) []int64 {
|
||||
out := make([]int64, 0, len(items))
|
||||
for _, item := range items {
|
||||
if !item.Valid {
|
||||
continue
|
||||
}
|
||||
out = append(out, item.Int64)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func int64IDsOrEmpty(items []int64) []int64 {
|
||||
if items == nil {
|
||||
return []int64{}
|
||||
|
|
|
|||
|
|
@ -1,34 +1,6 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func TestPgInt8IDsToInt64ReturnsEmptySlice(t *testing.T) {
|
||||
got := pgInt8IDsToInt64(nil)
|
||||
if got == nil {
|
||||
t.Fatal("expected empty slice, got nil")
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Fatalf("expected empty slice, got len=%d", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPgInt8IDsToInt64FiltersInvalidIDs(t *testing.T) {
|
||||
got := pgInt8IDsToInt64([]pgtype.Int8{
|
||||
{Int64: 10, Valid: true},
|
||||
{Valid: false},
|
||||
{Int64: 20, Valid: true},
|
||||
})
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("expected 2 ids, got %d", len(got))
|
||||
}
|
||||
if got[0] != 10 || got[1] != 20 {
|
||||
t.Fatalf("unexpected ids: %#v", got)
|
||||
}
|
||||
}
|
||||
import "testing"
|
||||
|
||||
func TestInt64IDsOrEmptyReturnsEmptySlice(t *testing.T) {
|
||||
got := int64IDsOrEmpty(nil)
|
||||
|
|
@ -39,3 +11,13 @@ func TestInt64IDsOrEmptyReturnsEmptySlice(t *testing.T) {
|
|||
t.Fatalf("expected empty slice, got len=%d", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt64IDsOrEmptyPreservesIDs(t *testing.T) {
|
||||
got := int64IDsOrEmpty([]int64{10, 20})
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("expected 2 ids, got %d", len(got))
|
||||
}
|
||||
if got[0] != 10 || got[1] != 20 {
|
||||
t.Fatalf("unexpected ids: %#v", got)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1316,7 +1316,7 @@ func (s *Store) ReorderQuestionSets(ctx context.Context, ids []int64, positions
|
|||
})
|
||||
}
|
||||
|
||||
func practiceByQuestionTypeRowToDomain(r dbgen.ListPracticesByQuestionTypeDefinitionRow) domain.PracticeByQuestionTypeDefinition {
|
||||
func practiceByQuestionTypeRowToDomain(r dbgen.ListPracticesByQuestionTypeDefinitionRow, parents []domain.PracticeParent) domain.PracticeByQuestionTypeDefinition {
|
||||
out := domain.PracticeByQuestionTypeDefinition{
|
||||
PracticeKind: r.PracticeKind,
|
||||
PracticeID: r.PracticeID,
|
||||
|
|
@ -1331,30 +1331,11 @@ func practiceByQuestionTypeRowToDomain(r dbgen.ListPracticesByQuestionTypeDefini
|
|||
CreatedAt: r.CreatedAt.Time,
|
||||
UpdatedAt: timePtr(r.UpdatedAt),
|
||||
}
|
||||
switch r.PracticeKind {
|
||||
case "LMS":
|
||||
switch {
|
||||
case r.CourseID.Valid:
|
||||
kind := domain.ParentKindCourse
|
||||
id := r.CourseID.Int64
|
||||
out.ParentKind = &kind
|
||||
out.ParentID = &id
|
||||
case r.ModuleID.Valid:
|
||||
kind := domain.ParentKindModule
|
||||
id := r.ModuleID.Int64
|
||||
out.ParentKind = &kind
|
||||
out.ParentID = &id
|
||||
case r.LessonID.Valid:
|
||||
kind := domain.ParentKindLesson
|
||||
id := r.LessonID.Int64
|
||||
out.ParentKind = &kind
|
||||
out.ParentID = &id
|
||||
}
|
||||
case "EXAM_PREP":
|
||||
if r.ExamPrepLessonID.Valid {
|
||||
id := r.ExamPrepLessonID.Int64
|
||||
out.ExamPrepLessonID = &id
|
||||
}
|
||||
if r.PracticeKind == "LMS" {
|
||||
out.Parents = parents
|
||||
} else if r.ExamPrepLessonID.Valid {
|
||||
id := r.ExamPrepLessonID.Int64
|
||||
out.ExamPrepLessonID = &id
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
|
@ -1382,13 +1363,29 @@ func (s *Store) ListPracticesByQuestionTypeDefinition(
|
|||
if len(rows) == 0 {
|
||||
return []domain.PracticeByQuestionTypeDefinition{}, 0, nil
|
||||
}
|
||||
lmsPracticeIDs := make([]int64, 0)
|
||||
for _, row := range rows {
|
||||
if row.PracticeKind == "LMS" {
|
||||
lmsPracticeIDs = append(lmsPracticeIDs, row.PracticeID)
|
||||
}
|
||||
}
|
||||
parentsByPracticeID := make(map[int64][]domain.PracticeParent)
|
||||
if len(lmsPracticeIDs) > 0 {
|
||||
parentRows, err := s.queries.ListLmsPracticeParentsByPracticeIDs(ctx, lmsPracticeIDs)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
for _, row := range parentRows {
|
||||
parentsByPracticeID[row.PracticeID] = append(parentsByPracticeID[row.PracticeID], lmsPracticeParentFromDB(row))
|
||||
}
|
||||
}
|
||||
out := make([]domain.PracticeByQuestionTypeDefinition, len(rows))
|
||||
var total int64
|
||||
for i, row := range rows {
|
||||
if i == 0 {
|
||||
total = row.TotalCount
|
||||
}
|
||||
out[i] = practiceByQuestionTypeRowToDomain(row)
|
||||
out[i] = practiceByQuestionTypeRowToDomain(row, parentsByPracticeID[row.PracticeID])
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,56 +71,63 @@ func (s *Service) validatePersonaCatalog(ctx context.Context, id int64) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
func (s *Service) resolveParent(ctx context.Context, in domain.CreatePracticeInput) (courseID, moduleID, lessonID *int64, err error) {
|
||||
pid := in.ParentID
|
||||
switch in.ParentKind {
|
||||
case domain.ParentKindCourse:
|
||||
if _, e := s.courses.GetCourseByID(ctx, pid); e != nil {
|
||||
if errors.Is(e, pgx.ErrNoRows) {
|
||||
return nil, nil, nil, courses.ErrCourseNotFound
|
||||
}
|
||||
return nil, nil, nil, e
|
||||
}
|
||||
return &pid, nil, nil, nil
|
||||
case domain.ParentKindModule:
|
||||
if _, e := s.modules.GetModuleByID(ctx, pid); e != nil {
|
||||
if errors.Is(e, pgx.ErrNoRows) {
|
||||
return nil, nil, nil, modules.ErrModuleNotFound
|
||||
}
|
||||
return nil, nil, nil, e
|
||||
}
|
||||
return nil, &pid, nil, nil
|
||||
case domain.ParentKindLesson:
|
||||
if _, e := s.lessons.GetLessonByID(ctx, pid); e != nil {
|
||||
if errors.Is(e, pgx.ErrNoRows) {
|
||||
return nil, nil, nil, lessons.ErrLessonNotFound
|
||||
}
|
||||
return nil, nil, nil, e
|
||||
}
|
||||
return nil, nil, &pid, nil
|
||||
default:
|
||||
return nil, nil, nil, ErrInvalidPracticeParent
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) ensureParentPracticeLimit(ctx context.Context, courseID, moduleID, lessonID *int64) error {
|
||||
var count int64
|
||||
var err error
|
||||
switch {
|
||||
case courseID != nil:
|
||||
count, err = s.practices.CountLmsPracticesByCourseID(ctx, *courseID)
|
||||
case moduleID != nil:
|
||||
count, err = s.practices.CountLmsPracticesByModuleID(ctx, *moduleID)
|
||||
case lessonID != nil:
|
||||
count, err = s.practices.CountLmsPracticesByLessonID(ctx, *lessonID)
|
||||
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
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) validateParents(ctx context.Context, parents []domain.PracticeParent) error {
|
||||
if len(parents) == 0 {
|
||||
return ErrInvalidPracticeParent
|
||||
}
|
||||
if count >= 1 {
|
||||
return ErrPracticeParentLimitExceeded
|
||||
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
|
||||
}
|
||||
|
|
@ -134,14 +141,14 @@ func (s *Service) Create(ctx context.Context, in domain.CreatePracticeInput) (do
|
|||
return domain.Practice{}, err
|
||||
}
|
||||
}
|
||||
courseID, moduleID, lessonID, err := s.resolveParent(ctx, in)
|
||||
if err != nil {
|
||||
parents := in.ResolvedParents()
|
||||
if err := s.validateParents(ctx, parents); err != nil {
|
||||
return domain.Practice{}, err
|
||||
}
|
||||
if err := s.ensureParentPracticeLimit(ctx, courseID, moduleID, lessonID); err != nil {
|
||||
if err := s.ensureParentsAvailable(ctx, parents, 0); err != nil {
|
||||
return domain.Practice{}, err
|
||||
}
|
||||
p, err := s.practices.CreateLmsPractice(ctx, in, courseID, moduleID, lessonID)
|
||||
p, err := s.practices.CreateLmsPractice(ctx, in, parents)
|
||||
if err != nil && repository.IsUniqueViolation(err) {
|
||||
return domain.Practice{}, ErrPracticeParentLimitExceeded
|
||||
}
|
||||
|
|
@ -220,6 +227,21 @@ func (s *Service) Update(ctx context.Context, id int64, input domain.UpdatePract
|
|||
return domain.Practice{}, err
|
||||
}
|
||||
}
|
||||
if input.Parents != nil {
|
||||
parents := domain.CreatePracticeInput{Parents: input.Parents}.ResolvedParents()
|
||||
if err := s.validateParents(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
|
||||
}
|
||||
}
|
||||
p, err := s.practices.UpdateLmsPractice(ctx, id, input)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
|
|
|
|||
|
|
@ -327,14 +327,35 @@ func (a *App) lmsEffectiveTierForPractice(ctx context.Context, practiceID int64)
|
|||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
switch practice.ParentKind {
|
||||
case domain.ParentKindLesson:
|
||||
return a.lmsEffectiveTierForLesson(ctx, practice.ParentID)
|
||||
case domain.ParentKindModule:
|
||||
return a.lmsEffectiveTierForModule(ctx, practice.ParentID)
|
||||
case domain.ParentKindCourse:
|
||||
return a.lmsEffectiveTierForCourse(ctx, practice.ParentID)
|
||||
default:
|
||||
if len(practice.Parents) == 0 {
|
||||
return "", false, nil
|
||||
}
|
||||
var tier domain.ContentAccessTier
|
||||
okAny := false
|
||||
for _, parent := range practice.Parents {
|
||||
var (
|
||||
parentTier domain.ContentAccessTier
|
||||
ok bool
|
||||
err error
|
||||
)
|
||||
switch parent.Kind {
|
||||
case domain.ParentKindLesson:
|
||||
parentTier, ok, err = a.lmsEffectiveTierForLesson(ctx, parent.ID)
|
||||
case domain.ParentKindModule:
|
||||
parentTier, ok, err = a.lmsEffectiveTierForModule(ctx, parent.ID)
|
||||
case domain.ParentKindCourse:
|
||||
parentTier, ok, err = a.lmsEffectiveTierForCourse(ctx, parent.ID)
|
||||
default:
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
okAny = true
|
||||
tier = domain.EffectiveContentAccessTier(tier, parentTier)
|
||||
}
|
||||
return tier, okAny, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ import (
|
|||
// CreatePractice godoc
|
||||
// @Tags practices
|
||||
// @Accept json
|
||||
// @Param body body domain.CreatePracticeInput true "Practice (parent_kind: COURSE | MODULE | LESSON)"
|
||||
// @Param body body domain.CreatePracticeInput true "Practice (parents array and/or legacy parent_kind + parent_id)"
|
||||
// @Router /api/v1/practices [post]
|
||||
func (h *Handler) CreatePractice(c *fiber.Ctx) error {
|
||||
var req domain.CreatePracticeInput
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user