feat: improve fetch course by slug query

This commit is contained in:
Rotimi Best
2024-06-16 05:01:35 +01:00
parent e8a79e5995
commit fecdaa116d
2 changed files with 52 additions and 31 deletions
@@ -13,21 +13,26 @@ import type {
import { STATUS } from '$lib/utils/constants/course'; import { STATUS } from '$lib/utils/constants/course';
import type { PostgrestSingleResponse } from '@supabase/supabase-js'; import type { PostgrestSingleResponse } from '@supabase/supabase-js';
export async function fetchCourse(courseId?: Course['id'], slug?: Course['slug']) { const SLUG_QUERY = `
const match: { slug?: string; id?: string; status?: string } = {}; id,
title,
type,
description,
overview,
logo,
is_published,
slug,
cost,
currency,
metadata,
is_certificate_downloadable,
certificate_theme,
lessons:lesson(
id, title, public, lesson_at, call_url, is_unlocked, order, created_at
)
`;
if (slug) { const ID_QUERY = `
match.slug = slug;
} else {
match.id = courseId;
}
match.status = STATUS[STATUS.ACTIVE];
const response: PostgrestSingleResponse<Course | null> = await supabase
.from('course')
.select(
`
id, id,
title, title,
type, type,
@@ -47,15 +52,29 @@ export async function fetchCourse(courseId?: Course['id'], slug?: Course['slug']
is_certificate_downloadable, is_certificate_downloadable,
certificate_theme, certificate_theme,
lessons:lesson( lessons:lesson(
id, title,public, lesson_at, call_url, is_unlocked, order, created_at, id, title,public, lesson_at, is_unlocked, order, created_at,
note, videos, slide_url, call_url, totalExercises:exercise(count), totalComments:lesson_comment(count), note, videos, slide_url, call_url, totalExercises:exercise(count), totalComments:lesson_comment(count),
profile:teacher_id(id, avatar_url, fullname), profile:teacher_id(id, avatar_url, fullname),
lesson_completion(id, profile_id, is_complete) lesson_completion(id, profile_id, is_complete)
), ),
attendance:group_attendance(*), attendance:group_attendance(*),
polls:apps_poll(status) polls:apps_poll(status)
` `;
)
export async function fetchCourse(courseId?: Course['id'], slug?: Course['slug']) {
const match: { slug?: string; id?: string; status?: string } = {};
if (slug) {
match.slug = slug;
} else {
match.id = courseId;
}
match.status = STATUS[STATUS.ACTIVE];
const response: PostgrestSingleResponse<Course | null> = await supabase
.from('course')
.select(slug ? SLUG_QUERY : ID_QUERY)
.match(match) .match(match)
.single(); .single();
+3 -1
View File
@@ -53,7 +53,9 @@ export const load = async ({ url, cookies }): Promise<LoadOutput> => {
if (isLocalHost && tempSiteName) { if (isLocalHost && tempSiteName) {
console.log('setting sitename temp'); console.log('setting sitename temp');
cookies.set('_orgSiteName', tempSiteName); cookies.set('_orgSiteName', tempSiteName, {
path: '/'
});
} }
const _orgSiteName = cookies.get('_orgSiteName'); const _orgSiteName = cookies.get('_orgSiteName');