Files
Aut.ManagementFaculty/src/lib/data/courses.ts
T

67 lines
2.1 KiB
TypeScript

import { query } from "@/lib/db";
import type { Course } from "@/lib/types";
import { rowToCourse, type CourseRow } from "@/lib/db/mappers";
import { COURSE_WITH_TAXONOMY_SELECT } from "@/lib/data/course-taxonomy-sql";
import { ARCHIVE_TAG_ID } from "@/lib/course-catalog";
/** Past offerings tagged `tag-archive` — catalog block of their own; excluded from home. */
const isArchiveCourseSql = `EXISTS (
SELECT 1 FROM course_tag_links ctl
WHERE ctl.course_id = c.id AND ctl.tag_id = '${ARCHIVE_TAG_ID}'
)`;
const catalogOrderSql = `CASE WHEN ${isArchiveCourseSql} THEN 1 ELSE 0 END ASC,
CASE WHEN c.featured THEN 0 ELSE 1 END ASC,
c.sort_order ASC,
c.title ASC`;
/** Published course URLs for the sitemap, including archived offerings. */
export async function getCoursesForSitemap(): Promise<
{ slug: string; updatedAt: Date }[]
> {
const res = await query<{ slug: string; updated_at: Date }>(
`SELECT c.slug, c.updated_at
FROM courses c
WHERE c.published = true AND c.slug <> ''
ORDER BY c.updated_at DESC`,
);
return res.rows.map((row) => ({
slug: row.slug,
updatedAt: row.updated_at,
}));
}
export async function getCourses(): Promise<Course[]> {
const res = await query<CourseRow>(
`${COURSE_WITH_TAXONOMY_SELECT}
WHERE c.published = true
ORDER BY ${catalogOrderSql}`,
);
return res.rows.map(rowToCourse);
}
/** Priority-ordered published courses for home preview (archive excluded). */
export async function getLatestCourses(limit = 6): Promise<Course[]> {
const res = await query<CourseRow>(
`${COURSE_WITH_TAXONOMY_SELECT}
WHERE c.published = true
AND NOT ${isArchiveCourseSql}
ORDER BY CASE WHEN c.featured THEN 0 ELSE 1 END ASC,
c.sort_order ASC,
c.title ASC
LIMIT $1`,
[limit],
);
return res.rows.map(rowToCourse);
}
export async function getCourseById(id: string): Promise<Course | null> {
const res = await query<CourseRow>(
`${COURSE_WITH_TAXONOMY_SELECT}
WHERE c.published = true AND (c.id = $1 OR c.slug = $1)
LIMIT 1`,
[id],
);
return res.rows[0] ? rowToCourse(res.rows[0]) : null;
}