Align header, footer, brand, homepage course preview, faculty links, and the course catalog with the documented changes. Co-authored-by: Cursor <cursoragent@cursor.com>
126 lines
3.0 KiB
TypeScript
126 lines
3.0 KiB
TypeScript
import { query } from "@/lib/db";
|
|
import {
|
|
getContentRow,
|
|
listContentRows,
|
|
type AdminListOpts,
|
|
} from "@/lib/data/admin/helpers";
|
|
|
|
export interface AdminCourseCategory {
|
|
id: string;
|
|
slug: string;
|
|
label: string;
|
|
sortOrder: number;
|
|
showInFilter: boolean;
|
|
published: boolean;
|
|
}
|
|
|
|
export interface AdminCourseTag {
|
|
id: string;
|
|
slug: string;
|
|
label: string;
|
|
sortOrder: number;
|
|
showInNav: boolean;
|
|
showInFilter: boolean;
|
|
published: boolean;
|
|
}
|
|
|
|
function mapCategory(row: Record<string, unknown>): AdminCourseCategory {
|
|
return {
|
|
id: String(row.id),
|
|
slug: String(row.slug),
|
|
label: String(row.label),
|
|
sortOrder: Number(row.sort_order ?? 0),
|
|
showInFilter: Boolean(row.show_in_filter),
|
|
published: Boolean(row.published),
|
|
};
|
|
}
|
|
|
|
function mapTag(row: Record<string, unknown>): AdminCourseTag {
|
|
return {
|
|
id: String(row.id),
|
|
slug: String(row.slug),
|
|
label: String(row.label),
|
|
sortOrder: Number(row.sort_order ?? 0),
|
|
showInNav: Boolean(row.show_in_nav),
|
|
showInFilter: Boolean(row.show_in_filter),
|
|
published: Boolean(row.published),
|
|
};
|
|
}
|
|
|
|
export function listAdminCourseCategories(opts?: AdminListOpts) {
|
|
return listContentRows(
|
|
"course_categories",
|
|
["label", "slug"],
|
|
mapCategory,
|
|
opts,
|
|
"sort_order ASC, label ASC",
|
|
);
|
|
}
|
|
|
|
export function getAdminCourseCategoryById(id: string) {
|
|
return getContentRow("course_categories", "id", id, mapCategory);
|
|
}
|
|
|
|
export function listAdminCourseTags(opts?: AdminListOpts) {
|
|
return listContentRows(
|
|
"course_tags",
|
|
["label", "slug"],
|
|
mapTag,
|
|
opts,
|
|
"sort_order ASC, label ASC",
|
|
);
|
|
}
|
|
|
|
export function getAdminCourseTagById(id: string) {
|
|
return getContentRow("course_tags", "id", id, mapTag);
|
|
}
|
|
|
|
/** Replace category links for a course. */
|
|
export async function setCourseCategories(
|
|
courseId: string,
|
|
categoryIds: string[],
|
|
): Promise<void> {
|
|
await query(`DELETE FROM course_category_links WHERE course_id = $1`, [
|
|
courseId,
|
|
]);
|
|
for (const categoryId of categoryIds) {
|
|
await query(
|
|
`INSERT INTO course_category_links (course_id, category_id)
|
|
VALUES ($1, $2) ON CONFLICT DO NOTHING`,
|
|
[courseId, categoryId],
|
|
);
|
|
}
|
|
}
|
|
|
|
/** Replace tag links for a course. */
|
|
export async function setCourseTags(
|
|
courseId: string,
|
|
tagIds: string[],
|
|
): Promise<void> {
|
|
await query(`DELETE FROM course_tag_links WHERE course_id = $1`, [courseId]);
|
|
for (const tagId of tagIds) {
|
|
await query(
|
|
`INSERT INTO course_tag_links (course_id, tag_id)
|
|
VALUES ($1, $2) ON CONFLICT DO NOTHING`,
|
|
[courseId, tagId],
|
|
);
|
|
}
|
|
}
|
|
|
|
/** Replace faculty (instructor) links for a course. */
|
|
export async function setCourseFaculty(
|
|
courseId: string,
|
|
facultyIds: string[],
|
|
): Promise<void> {
|
|
await query(`DELETE FROM course_faculty_links WHERE course_id = $1`, [
|
|
courseId,
|
|
]);
|
|
for (const facultyId of facultyIds) {
|
|
await query(
|
|
`INSERT INTO course_faculty_links (course_id, faculty_id)
|
|
VALUES ($1, $2) ON CONFLICT DO NOTHING`,
|
|
[courseId, facultyId],
|
|
);
|
|
}
|
|
}
|