feat: Introduce core e-learning features with new pages for course details, dashboard, authentication, browsing, and learning, supported by a useCourse composable.

This commit is contained in:
supalerk-ar66 2026-01-23 09:47:32 +07:00
parent c982ab2c05
commit 0eb9b522f6
6 changed files with 109 additions and 38 deletions

View file

@ -62,11 +62,20 @@ interface EnrolledCourseResponse {
limit: number
}
// ==========================================
// Composable: useCourse
// หน้าที่: จัดการ Logic ทุกอย่างเกี่ยวกับคอร์สเรียน
// - ดึงข้อมูลคอร์ส (Public & Protected)
// - ลงทะเบียนเรียน (Enroll)
// - ติดตามความคืบหน้าการเรียน (Progress tracking)
// ==========================================
export const useCourse = () => {
const config = useRuntimeConfig()
const API_BASE_URL = config.public.apiBase as string
const { token } = useAuth()
// ฟังก์ชันดึงรายชื่อคอร์สทั้งหมด (Catalog)
// Endpoint: GET /courses
const fetchCourses = async () => {
try {
const data = await $fetch<CourseResponse>(`${API_BASE_URL}/courses`, {
@ -90,6 +99,8 @@ export const useCourse = () => {
}
}
// ฟังก์ชันดึงรายละเอียดคอร์สตาม ID
// Endpoint: GET /courses/:id
const fetchCourseById = async (id: number) => {
try {
const data = await $fetch<CourseResponse>(`${API_BASE_URL}/courses/${id}`, {
@ -130,6 +141,8 @@ export const useCourse = () => {
}
}
// ฟังก์ชันลงทะเบียนเรียน
// Endpoint: POST /students/courses/:id/enroll
const enrollCourse = async (courseId: number) => {
try {
const data = await $fetch<{ code: number; message: string; data: any }>(`${API_BASE_URL}/students/courses/${courseId}/enroll`, {
@ -197,6 +210,8 @@ export const useCourse = () => {
}
}
// ฟังก์ชันดึงข้อมูลสำหรับการเรียน (Chapters, Lessons, Progress)
// Endpoint: GET /students/courses/:id/learn
const fetchCourseLearningInfo = async (courseId: number) => {
try {
const data = await $fetch<{ code: number; message: string; data: any }>(`${API_BASE_URL}/students/courses/${courseId}/learn`, {
@ -220,6 +235,8 @@ export const useCourse = () => {
}
}
// ฟังก์ชันดึงเนื้อหาบทเรียน (Video, Content)
// Endpoint: GET /students/courses/:cid/lessons/:lid
const fetchLessonContent = async (courseId: number, lessonId: number) => {
try {
const data = await $fetch<{ code: number; message: string; data: any; progress?: any }>(`${API_BASE_URL}/students/courses/${courseId}/lessons/${lessonId}`, {
@ -269,6 +286,8 @@ export const useCourse = () => {
}
}
// ฟังก์ชันบันทึกเวลาที่ดูวิดีโอ (Video Progress)
// Endpoint: POST /students/lessons/:id/progress
const saveVideoProgress = async (lessonId: number, progressSeconds: number, durationSeconds: number) => {
try {
const data = await $fetch<{ code: number; message: string; data: any }>(`${API_BASE_URL}/students/lessons/${lessonId}/progress`, {
@ -320,6 +339,8 @@ export const useCourse = () => {
}
}
// ฟังก์ชันบันทึกว่าเรียนจบบทเรียนแล้ว (Mark Complete)
// Endpoint: POST /students/courses/:cid/lessons/:lid/complete
const markLessonComplete = async (courseId: number, lessonId: number) => {
try {
const data = await $fetch<{ code: number; message: string; data: any }>(`${API_BASE_URL}/students/courses/${courseId}/lessons/${lessonId}/complete`, {