Add a module delete API route and handler so level/module removal actions remove modules directly instead of only deleting sub-modules. Made-with: Cursor
184 lines
4.1 KiB
Go
184 lines
4.1 KiB
Go
package dbgen
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func (q *Queries) GetSubModuleByIDCompat(ctx context.Context, id int64) (SubModule, error) {
|
|
row := q.db.QueryRow(ctx, `
|
|
SELECT id, module_id, title, description, display_order, is_active, created_at, legacy_sub_course_id
|
|
FROM sub_modules
|
|
WHERE id = $1
|
|
`, id)
|
|
var i SubModule
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ModuleID,
|
|
&i.Title,
|
|
&i.Description,
|
|
&i.DisplayOrder,
|
|
&i.IsActive,
|
|
&i.CreatedAt,
|
|
&i.LegacySubCourseID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
func (q *Queries) UpdateSubModuleCompat(ctx context.Context, id int64, title string, description string, isActive bool) error {
|
|
_, err := q.db.Exec(ctx, `
|
|
UPDATE sub_modules
|
|
SET
|
|
title = $1,
|
|
description = NULLIF($2, ''),
|
|
is_active = $3
|
|
WHERE id = $4
|
|
`, title, description, isActive, id)
|
|
return err
|
|
}
|
|
|
|
func (q *Queries) DeleteSubModuleCompat(ctx context.Context, id int64) error {
|
|
_, err := q.db.Exec(ctx, `DELETE FROM sub_modules WHERE id = $1`, id)
|
|
return err
|
|
}
|
|
|
|
func (q *Queries) DeleteModuleCompat(ctx context.Context, id int64) error {
|
|
_, err := q.db.Exec(ctx, `DELETE FROM modules WHERE id = $1`, id)
|
|
return err
|
|
}
|
|
|
|
func (q *Queries) UpdateSubModuleVideoCompat(ctx context.Context, id int64, title string, description string, videoURL string) error {
|
|
_, err := q.db.Exec(ctx, `
|
|
UPDATE sub_module_videos
|
|
SET
|
|
title = $1,
|
|
description = NULLIF($2, ''),
|
|
video_url = $3
|
|
WHERE id = $4
|
|
`, title, description, videoURL, id)
|
|
return err
|
|
}
|
|
|
|
func (q *Queries) DeleteSubModuleVideoCompat(ctx context.Context, id int64) error {
|
|
_, err := q.db.Exec(ctx, `DELETE FROM sub_module_videos WHERE id = $1`, id)
|
|
return err
|
|
}
|
|
|
|
func (q *Queries) UpdatePracticeCompat(ctx context.Context, id int64, title string, description string, persona string) error {
|
|
_, err := q.db.Exec(ctx, `
|
|
UPDATE question_sets
|
|
SET
|
|
title = $1,
|
|
description = NULLIF($2, ''),
|
|
persona = NULLIF($3, ''),
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $4
|
|
`, title, description, persona, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = q.db.Exec(ctx, `
|
|
UPDATE sub_module_practices
|
|
SET
|
|
title = $1,
|
|
description = NULLIF($2, '')
|
|
WHERE question_set_id = $3
|
|
`, title, description, id)
|
|
return err
|
|
}
|
|
|
|
func (q *Queries) UpdatePracticeStatusCompat(ctx context.Context, id int64, isActive bool) error {
|
|
status := "ARCHIVED"
|
|
if isActive {
|
|
status = "PUBLISHED"
|
|
}
|
|
|
|
_, err := q.db.Exec(ctx, `
|
|
UPDATE question_sets
|
|
SET
|
|
status = $1,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $2
|
|
`, status, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = q.db.Exec(ctx, `
|
|
UPDATE sub_module_practices
|
|
SET is_active = $1
|
|
WHERE question_set_id = $2
|
|
`, isActive, id)
|
|
return err
|
|
}
|
|
|
|
func (q *Queries) DeletePracticeCompat(ctx context.Context, id int64) error {
|
|
_, err := q.db.Exec(ctx, `DELETE FROM question_sets WHERE id = $1`, id)
|
|
return err
|
|
}
|
|
|
|
func (q *Queries) CreateCourseCompat(
|
|
ctx context.Context,
|
|
categoryID int64,
|
|
subCategoryID *int64,
|
|
title string,
|
|
description string,
|
|
thumbnail string,
|
|
introVideoURL string,
|
|
isActive bool,
|
|
) (Course, error) {
|
|
row := q.db.QueryRow(ctx, `
|
|
INSERT INTO courses (
|
|
category_id,
|
|
sub_category_id,
|
|
title,
|
|
description,
|
|
thumbnail,
|
|
intro_video_url,
|
|
is_active
|
|
)
|
|
VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
NULLIF($4, ''),
|
|
NULLIF($5, ''),
|
|
NULLIF($6, ''),
|
|
$7
|
|
)
|
|
RETURNING id, category_id, title, description, is_active, thumbnail, intro_video_url, display_order, sub_category_id
|
|
`, categoryID, subCategoryID, title, description, thumbnail, introVideoURL, isActive)
|
|
|
|
var i Course
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.CategoryID,
|
|
&i.Title,
|
|
&i.Description,
|
|
&i.IsActive,
|
|
&i.Thumbnail,
|
|
&i.IntroVideoUrl,
|
|
&i.DisplayOrder,
|
|
&i.SubCategoryID,
|
|
)
|
|
if err != nil {
|
|
return Course{}, err
|
|
}
|
|
if !i.SubCategoryID.Valid {
|
|
i.SubCategoryID = pgtype.Int8{Valid: false}
|
|
}
|
|
return i, nil
|
|
}
|
|
|
|
func (q *Queries) DeleteCourseSubCategoryCompat(ctx context.Context, subCategoryID int64) error {
|
|
_, err := q.db.Exec(ctx, `DELETE FROM courses WHERE sub_category_id = $1`, subCategoryID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = q.db.Exec(ctx, `DELETE FROM course_sub_categories WHERE id = $1`, subCategoryID)
|
|
return err
|
|
}
|