changes
This commit is contained in:
1 parent
a99af426db
commit
8886aa8640
91 files changed
+2823
-773
No files matched your search
@@ -0,0 +1,108 @@
|
||||
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],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user