From 0cc813d22419dfc47e1398ca7e104047838e06ee Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Tue, 14 Apr 2026 05:23:24 -0700 Subject: [PATCH] fix course creation linkage to sub-categories Allow course creation payloads to include sub_category_id and persist it so newly created language paths appear in unified hierarchy views. Made-with: Cursor --- gen/db/compat_course_management.go | 55 +++++++++++++++++++ .../web_server/handlers/hierarchy_handler.go | 46 +++++++++++----- 2 files changed, 88 insertions(+), 13 deletions(-) diff --git a/gen/db/compat_course_management.go b/gen/db/compat_course_management.go index d639ad6..0688beb 100644 --- a/gen/db/compat_course_management.go +++ b/gen/db/compat_course_management.go @@ -2,6 +2,8 @@ package dbgen import ( "context" + + "github.com/jackc/pgx/v5/pgtype" ) func (q *Queries) GetSubModuleByIDCompat(ctx context.Context, id int64) (SubModule, error) { @@ -111,3 +113,56 @@ 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 +} diff --git a/internal/web_server/handlers/hierarchy_handler.go b/internal/web_server/handlers/hierarchy_handler.go index 81c8314..7bbdb93 100644 --- a/internal/web_server/handlers/hierarchy_handler.go +++ b/internal/web_server/handlers/hierarchy_handler.go @@ -24,12 +24,13 @@ type createCourseCategoryReq struct { } type createCourseReq struct { - CategoryID int64 `json:"category_id"` - Title string `json:"title"` - Description *string `json:"description"` - Thumbnail *string `json:"thumbnail"` + CategoryID int64 `json:"category_id"` + SubCategoryID *int64 `json:"sub_category_id"` + Title string `json:"title"` + Description *string `json:"description"` + Thumbnail *string `json:"thumbnail"` IntroVideoURL *string `json:"intro_video_url"` - IsActive *bool `json:"is_active"` + IsActive *bool `json:"is_active"` } type updateCourseReq struct { @@ -238,14 +239,33 @@ func (h *Handler) CreateCourse(c *fiber.Ctx) error { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: "category_id and title are required"}) } - created, err := h.analyticsDB.CreateCourse(c.Context(), dbgen.CreateCourseParams{ - CategoryID: req.CategoryID, - Title: req.Title, - Description: toText(req.Description), - Thumbnail: toText(req.Thumbnail), - IntroVideoUrl: toText(req.IntroVideoURL), - Column6: boolOrNil(req.IsActive), - }) + isActive := true + if req.IsActive != nil { + isActive = *req.IsActive + } + description := "" + if req.Description != nil { + description = *req.Description + } + thumbnail := "" + if req.Thumbnail != nil { + thumbnail = *req.Thumbnail + } + introVideoURL := "" + if req.IntroVideoURL != nil { + introVideoURL = *req.IntroVideoURL + } + + created, err := h.analyticsDB.CreateCourseCompat( + c.Context(), + req.CategoryID, + req.SubCategoryID, + req.Title, + description, + thumbnail, + introVideoURL, + isActive, + ) if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: "Failed to create course", Error: err.Error()}) }