feat: enhance API route naming conventions and structure
- Added comprehensive guidelines for API route naming and structure in `ROUTE_NAMING_BEST_PRACTICES.md`. - Updated `ARCHITECHTURE.md` to reference the new best practices document. - Refactored course-related routes to follow new naming conventions, including changes to endpoint paths and validation schemas. - Removed the deprecated course clone route and integrated cloning functionality directly into the course routes. - Adjusted various API methods to ensure consistent usage of HTTP methods and parameter structures across the application.
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import { BaseApiWithErrors, classroomio } from '$lib/utils/services/api';
|
||||
import { copyCourseModal, copyCourseModalInitialState } from '../utils/store';
|
||||
|
||||
import type { TCourseClone } from '@cio/utils/validation/course';
|
||||
import { ZCourseClone } from '@cio/utils/validation/course';
|
||||
import { currentOrg } from '$lib/utils/store/org';
|
||||
import generateSlug from '$lib/utils/functions/generateSlug';
|
||||
import { get } from 'svelte/store';
|
||||
import { goto } from '$app/navigation';
|
||||
import { mapZodErrorsToTranslations } from '$lib/utils/validation';
|
||||
import { resolve } from '$app/paths';
|
||||
import { snackbar } from '$lib/components/Snackbar/store';
|
||||
import { t } from '$lib/utils/functions/translations';
|
||||
|
||||
/**
|
||||
* API class for course operations
|
||||
*/
|
||||
export class CourseCloneApi extends BaseApiWithErrors {
|
||||
/**
|
||||
* Clones a course
|
||||
* @param courseId The ID of the course to clone
|
||||
* @param title Course title
|
||||
* @param description Course description (optional)
|
||||
* @returns The cloned course data or null on error
|
||||
*/
|
||||
async clone(courseId: string, title: string, description?: string) {
|
||||
// Generate unique slug from the title
|
||||
const slug = generateSlug(title);
|
||||
|
||||
// Prepare data for validation
|
||||
const data: TCourseClone = {
|
||||
title,
|
||||
description,
|
||||
slug,
|
||||
organizationId: get(currentOrg).id
|
||||
};
|
||||
|
||||
// Client-side validation
|
||||
const result = ZCourseClone.safeParse(data);
|
||||
if (!result.success) {
|
||||
this.errors = mapZodErrorsToTranslations(result.error, 'course');
|
||||
return null;
|
||||
}
|
||||
|
||||
await this.execute<(typeof classroomio.course)[':courseId']['clone']['$post']>({
|
||||
requestFn: () =>
|
||||
classroomio.course[':courseId'].clone.$post({
|
||||
param: { courseId },
|
||||
// @ts-expect-error - the json type is not inferred correctly
|
||||
json: result.data
|
||||
}),
|
||||
logContext: 'cloning course',
|
||||
onSuccess: (response) => {
|
||||
this.errors = {};
|
||||
|
||||
copyCourseModal.set(copyCourseModalInitialState);
|
||||
copyCourseModal.update((modal) => ({ ...modal, open: false }));
|
||||
|
||||
// Navigate to the new course
|
||||
goto(resolve(`/courses/${response.course.id}`, {}));
|
||||
|
||||
snackbar.success(t.get('courses.copy_course.success.course_cloned'));
|
||||
},
|
||||
onError: (result) => {
|
||||
if (typeof result === 'string') {
|
||||
snackbar.error(result);
|
||||
return;
|
||||
}
|
||||
if ('error' in result && 'field' in result) {
|
||||
// Field-specific error automatically mapped to this.errors[field]
|
||||
this.errors[result.field as string] = result.error;
|
||||
} else if ('error' in result) {
|
||||
snackbar.error(result.error);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const courseCloneApi = new CourseCloneApi();
|
||||
@@ -0,0 +1 @@
|
||||
export * from './course-clone.svelte';
|
||||
@@ -1,64 +1,23 @@
|
||||
<script lang="ts">
|
||||
import { preventDefault } from '$lib/utils/functions/svelte';
|
||||
|
||||
import { goto } from '$app/navigation';
|
||||
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
|
||||
import Modal from '$lib/components/Modal/index.svelte';
|
||||
import { snackbar } from '$lib/components/Snackbar/store';
|
||||
import TextField from '$lib/components/Form/TextField.svelte';
|
||||
import TextArea from '$lib/components/Form/TextArea.svelte';
|
||||
import { copyCourseModalInitialState, copyCourseModal } from '$lib/features/course/utils/store';
|
||||
import { classroomio } from '$lib/utils/services/api';
|
||||
import generateSlug from '$lib/utils/functions/generateSlug';
|
||||
import { currentOrg } from '$lib/utils/store/org';
|
||||
import { copyCourseModal } from '$lib/features/course/utils/store';
|
||||
import { courseCloneApi } from '$lib/features/course/api';
|
||||
import { t } from '$lib/utils/functions/translations';
|
||||
|
||||
// clone, show spinner and redirect to new course
|
||||
async function createCourse() {
|
||||
// Prevent double submission (Option 1: Early Return Guard)
|
||||
if ($copyCourseModal.isSaving) return;
|
||||
if ($copyCourseModal.isSaving || courseCloneApi.isLoading) return;
|
||||
|
||||
console.log('Create course');
|
||||
$copyCourseModal.isSaving = true;
|
||||
|
||||
try {
|
||||
// Generate unique slug from the title
|
||||
const slug = generateSlug($copyCourseModal.title);
|
||||
|
||||
const response = await classroomio.course.clone.$post({
|
||||
json: {
|
||||
id: $copyCourseModal.id,
|
||||
title: $copyCourseModal.title,
|
||||
description: $copyCourseModal.description,
|
||||
slug,
|
||||
organizationId: $currentOrg.id
|
||||
}
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(result.error || $t('courses.copy_course.error.failed_to_clone'));
|
||||
}
|
||||
|
||||
// Option 3: Close modal and reset state before navigation
|
||||
copyCourseModal.set(copyCourseModalInitialState);
|
||||
|
||||
// Navigate to the new course
|
||||
goto(`/courses/${result.course.id}`);
|
||||
$copyCourseModal.open = false;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
// IIFI of snackbar to Show notification error
|
||||
(() => {
|
||||
snackbar.error();
|
||||
})();
|
||||
|
||||
// Stop loader
|
||||
$copyCourseModal.isSaving = false;
|
||||
}
|
||||
await courseCloneApi.clone($copyCourseModal.id, $copyCourseModal.title, $copyCourseModal.description);
|
||||
}
|
||||
|
||||
// Sync loading state with modal store
|
||||
$effect(() => {
|
||||
$copyCourseModal.isSaving = courseCloneApi.isLoading;
|
||||
});
|
||||
</script>
|
||||
|
||||
<Modal
|
||||
@@ -76,6 +35,7 @@
|
||||
className="mb-4"
|
||||
isRequired={true}
|
||||
autoComplete={false}
|
||||
errorMessage={courseCloneApi.errors.title}
|
||||
/>
|
||||
|
||||
<TextArea
|
||||
@@ -84,8 +44,13 @@
|
||||
placeholder={$t('courses.copy_course.course_description_placeholder')}
|
||||
className="mb-4"
|
||||
rows={4}
|
||||
errorMessage={courseCloneApi.errors.description}
|
||||
/>
|
||||
|
||||
{#if courseCloneApi.errors.general}
|
||||
<div class="mb-4 text-sm text-red-600">{courseCloneApi.errors.general}</div>
|
||||
{/if}
|
||||
|
||||
<div class="mt-5 flex flex-row-reverse items-center">
|
||||
<PrimaryButton
|
||||
className="px-6 py-3"
|
||||
|
||||
Reference in New Issue
Block a user