edit: response.message

This commit is contained in:
Missez 2026-02-02 09:31:22 +07:00
parent d7f824f353
commit 7de5457170
21 changed files with 227 additions and 127 deletions

View file

@ -17,14 +17,14 @@
<span>Dashboard</span>
</NuxtLink>
<NuxtLink
<!-- <NuxtLink
to="/admin/courses"
class="flex items-center gap-3 px-4 py-3 rounded-lg hover:bg-primary-100 transition mb-2"
active-class="bg-primary-500 text-white hover:bg-primary-600"
>
<q-icon name="school" size="24px" />
<span>ดการหลกสตร</span>
</NuxtLink>
</NuxtLink> -->
<NuxtLink
to="/admin/courses/pending"

View file

@ -224,7 +224,7 @@ const fetchCategories = async () => {
} catch (error) {
$q.notify({
type: 'negative',
message: 'ไม่สามารถโหลดข้อมูลหมวดหมู่ได้',
message: (error as any).data?.message || 'ไม่สามารถโหลดข้อมูลหมวดหมู่ได้',
position: 'top'
});
} finally {
@ -270,32 +270,32 @@ const handleSave = async () => {
saving.value = true;
try {
if (isEditing.value && editingId.value) {
await adminService.updateCategory(editingId.value, {
const response = await adminService.updateCategory(editingId.value, {
id: editingId.value,
...form.value
});
$q.notify({
type: 'positive',
message: 'แก้ไขหมวดหมู่สำเร็จ',
message: response.message || 'แก้ไขหมวดหมู่สำเร็จ',
position: 'top'
});
} else {
await adminService.createCategory({
const response = await adminService.createCategory({
...form.value,
created_by: parseInt(authStore.user?.id || '0')
});
$q.notify({
type: 'positive',
message: 'เพิ่มหมวดหมู่สำเร็จ',
message: response.message || 'เพิ่มหมวดหมู่สำเร็จ',
position: 'top'
});
}
showModal.value = false;
fetchCategories();
} catch (error) {
} catch (error: any) {
$q.notify({
type: 'negative',
message: 'เกิดข้อผิดพลาด กรุณาลองใหม่',
message: error.data?.message || 'เกิดข้อผิดพลาด กรุณาลองใหม่',
position: 'top'
});
} finally {
@ -311,17 +311,17 @@ const confirmDelete = (category: CategoryResponse) => {
persistent: true
}).onOk(async () => {
try {
await adminService.deleteCategory(category.id);
const response = await adminService.deleteCategory(category.id);
$q.notify({
type: 'positive',
message: 'ลบหมวดหมู่สำเร็จ',
message: response.message || 'ลบหมวดหมู่สำเร็จ',
position: 'top'
});
fetchCategories();
} catch (error) {
} catch (error: any) {
$q.notify({
type: 'negative',
message: 'เกิดข้อผิดพลาดในการลบ',
message: error.data?.message || 'เกิดข้อผิดพลาดในการลบ',
position: 'top'
});
}

View file

@ -285,7 +285,7 @@ const fetchCourse = async () => {
const courseId = Number(route.params.id);
course.value = await adminService.getCourseForReview(courseId);
} catch (err) {
error.value = 'ไม่สามารถโหลดข้อมูลคอร์สได้';
error.value = (err as any).data?.message || 'ไม่สามารถโหลดข้อมูลคอร์สได้';
} finally {
loading.value = false;
}
@ -395,17 +395,17 @@ const confirmApprove = () => {
}).onOk(async () => {
actionLoading.value = true;
try {
await adminService.approveCourse(course.value!.id);
const response = await adminService.approveCourse(course.value!.id);
$q.notify({
type: 'positive',
message: 'อนุมัติคอร์สสำเร็จ',
message: response.message || 'อนุมัติคอร์สสำเร็จ',
position: 'top'
});
router.push('/admin/courses/pending');
} catch (err) {
} catch (err: any) {
$q.notify({
type: 'negative',
message: 'เกิดข้อผิดพลาดในการอนุมัติคอร์ส',
message: err.data?.message || 'เกิดข้อผิดพลาดในการอนุมัติคอร์ส',
position: 'top'
});
} finally {
@ -419,18 +419,18 @@ const confirmReject = async () => {
actionLoading.value = true;
try {
await adminService.rejectCourse(course.value.id, rejectReason.value);
const response = await adminService.rejectCourse(course.value.id, rejectReason.value);
$q.notify({
type: 'positive',
message: 'ปฏิเสธคอร์สสำเร็จ',
message: response.message || 'ปฏิเสธคอร์สสำเร็จ',
position: 'top'
});
showRejectModal.value = false;
router.push('/admin/courses/pending');
} catch (err) {
} catch (err: any) {
$q.notify({
type: 'negative',
message: 'เกิดข้อผิดพลาดในการปฏิเสธคอร์ส',
message: err.data?.message || 'เกิดข้อผิดพลาดในการปฏิเสธคอร์ส',
position: 'top'
});
} finally {

View file

@ -174,7 +174,7 @@ const fetchPendingCourses = async () => {
} catch (error) {
$q.notify({
type: 'negative',
message: 'ไม่สามารถโหลดข้อมูลคอร์สได้',
message: (error as any).data?.message || 'ไม่สามารถโหลดข้อมูลคอร์สได้',
position: 'top'
});
} finally {

View file

@ -5,13 +5,13 @@
<div class="flex justify-between items-center">
<div>
<h1 class="text-2xl font-bold text-gray-900">
สวสด, {{ authStore.user?.fullName || 'ผู้ดูแลระบบ' }}
สวสด, {{ authStore.user?.firstName }} {{ authStore.user?.lastName }}
</h1>
<p class="text-gray-600 mt-2">นดอนรบกลบสระบบ</p>
</div>
<div class="flex items-center gap-4">
<div class="text-right">
<div class="text-sm font-semibold text-gray-900">{{ authStore.user?.fullName || 'ผู้ดูแลระบบ' }}</div>
<div class="text-sm font-semibold text-gray-900">{{ authStore.user?.firstName }} {{ authStore.user?.lastName }}</div>
<div class="text-xs text-gray-500">{{ authStore.user?.role || 'ADMIN' }}</div>
</div>
<div
@ -23,7 +23,7 @@
<!-- User Info Header -->
<q-item class="bg-primary-50">
<q-item-section>
<q-item-label class="text-weight-bold">{{ authStore.user?.fullName }}</q-item-label>
<q-item-label class="text-weight-bold">{{ authStore.user?.firstName }} {{ authStore.user?.lastName }}</q-item-label>
<q-item-label caption>{{ authStore.user?.email }}</q-item-label>
</q-item-section>
</q-item>
@ -85,8 +85,8 @@
</div>
<!-- Recent Courses -->
<div class="card">
<h2 class="text-xl font-semibold mb-4">หลกสตรลาส</h2>
<div class="space-y-4">
<h2 class="text-xl font-semibold mb-4">Dashboard</h2>
<!-- <div class="space-y-4">
<div class="flex items-center gap-4 p-4 bg-gray-50 rounded-lg">
<div class="w-16 h-16 bg-primary-100 rounded-lg flex items-center justify-center">
<q-icon name="code" size="32px" class="text-primary-600" />
@ -97,7 +97,7 @@
</div>
<q-btn flat color="primary" label="ดูรายละเอียด" />
</div>
</div>
</div> -->
</div>
</div>
</template>

View file

@ -11,11 +11,11 @@
icon="download"
@click="exportExcel"
/>
<q-btn
<!-- <q-btn
color="primary"
label="+ เพิ่มผู้ใช้ใหม่"
@click="showAddModal = true"
/>
/> -->
</div>
</div>
@ -279,7 +279,7 @@ const fetchUsers = async () => {
} catch (error) {
$q.notify({
type: 'negative',
message: 'ไม่สามารถโหลดข้อมูลผู้ใช้ได้',
message: (error as any).data?.message || 'ไม่สามารถโหลดข้อมูลผู้ใช้ได้',
position: 'top'
});
} finally {
@ -326,7 +326,7 @@ const changeRole = (user: AdminUserResponse) => {
message: `เลือก Role ใหม่สำหรับ ${user.profile.first_name}`,
options: {
type: 'radio',
model: roleIds[user.role.code],
model: roleIds[user.role.code] as any,
items: [
{ label: 'Instructor', value: 1 },
{ label: 'Student', value: 2 },
@ -337,17 +337,17 @@ const changeRole = (user: AdminUserResponse) => {
persistent: true
}).onOk(async (roleId: number) => {
try {
await adminService.updateUserRole(user.id, roleId);
const response = await adminService.updateUserRole(user.id, roleId);
$q.notify({
type: 'positive',
message: 'เปลี่ยน Role สำเร็จ',
message: response.message || 'เปลี่ยน Role สำเร็จ',
position: 'top'
});
fetchUsers();
} catch (error) {
} catch (error: any) {
$q.notify({
type: 'negative',
message: 'เกิดข้อผิดพลาดในการเปลี่ยน Role',
message: error.data?.message || 'เกิดข้อผิดพลาดในการเปลี่ยน Role',
position: 'top'
});
}
@ -362,17 +362,17 @@ const confirmDelete = (user: AdminUserResponse) => {
persistent: true
}).onOk(async () => {
try {
await adminService.deleteUser(user.id);
const response = await adminService.deleteUser(user.id);
$q.notify({
type: 'positive',
message: 'ลบผู้ใช้สำเร็จ',
message: response.message || 'ลบผู้ใช้สำเร็จ',
position: 'top'
});
fetchUsers();
} catch (error) {
} catch (error: any) {
$q.notify({
type: 'negative',
message: 'เกิดข้อผิดพลาดในการลบผู้ใช้',
message: error.data?.message || 'เกิดข้อผิดพลาดในการลบผู้ใช้',
position: 'top'
});
}

View file

@ -430,7 +430,7 @@ const saveLesson = async () => {
navigateTo(`/instructor/courses/${courseId}/structure`);
} catch (error) {
console.error('Failed to save lesson:', error);
$q.notify({ type: 'negative', message: 'ไม่สามารถบันทึกได้', position: 'top' });
$q.notify({ type: 'negative', message: (error as any).data?.error?.message || (error as any).data?.message || 'ไม่สามารถบันทึกได้', position: 'top' });
} finally {
saving.value = false;
}

View file

@ -62,6 +62,20 @@
autogrow
rows="3"
/>
<q-card-section class="flex justify-end gap-2">
<q-btn
flat
label="ยกเลิก"
@click="navigateTo(`/instructor/courses/${courseId}/structure`)"
/>
<q-btn
color="primary"
label="บันทึก"
icon="save"
:loading="saving"
@click="saveLesson"
/>
</q-card-section>
</div>
</q-card-section>
@ -182,20 +196,7 @@
<!-- Actions -->
<q-separator />
<q-card-section class="flex justify-end gap-2">
<q-btn
flat
label="ยกเลิก"
@click="navigateTo(`/instructor/courses/${courseId}/structure`)"
/>
<q-btn
color="primary"
label="บันทึก"
icon="save"
:loading="saving"
@click="saveLesson"
/>
</q-card-section>
</q-card>
</div>

View file

@ -58,7 +58,8 @@
<!-- Status Badges -->
<div class="flex gap-2">
<q-badge v-if="course.is_free" color="purple">เผยแพร่</q-badge>
<q-badge v-if="course.is_free" color="purple">ฟรี</q-badge>
<q-badge v-else color="purple">เสยเง</q-badge>
<q-badge :color="getStatusColor(course.status)">
{{ getStatusLabel(course.status) }}
</q-badge>

View file

@ -204,6 +204,7 @@
:rules="[val => !!val || 'กรุณากรอกชื่อบทเรียน']"
lazy-rules="ondemand"
hide-bottom-space
class="mb-4"
/>
<q-input
v-model="lessonForm.title.en"

View file

@ -197,6 +197,7 @@ const handleSubmit = async () => {
const response = await instructorService.createCourse(form.value);
$q.notify({
type: 'positive',
message: response.message,
@ -206,9 +207,9 @@ const handleSubmit = async () => {
// Redirect to course edit page
// Note: Assuming response.data contains the created course with ID
if (response.data && response.data.id) {
router.push(`/instructor/courses/${response.data.id}/edit`);
await navigateTo(`/instructor/courses/${response.data.id}`);
} else {
navigateTo('/instructor/courses');
await navigateTo('/instructor/courses');
}
} catch (error: any) {
$q.notify({

View file

@ -100,9 +100,9 @@
<div class="flex justify-between items-start mb-2">
<h3 class="font-semibold text-gray-900 line-clamp-2">{{ course.title.th }}</h3>
<q-badge :color="getStatusColor(course.status)" class="ml-2">
{{ getStatusLabel(course.status) }}
</q-badge>
</div>
{{ getStatusLabel(course.status) }}
</q-badge>
</div>
<p class="text-sm text-gray-500 line-clamp-2 mb-3">
{{ course.description.th }}
@ -277,18 +277,18 @@ const confirmDelete = (course: CourseResponse) => {
persistent: true
}).onOk(async () => {
try {
await instructorService.deleteCourse(course.id);
const response = await instructorService.deleteCourse(course.id);
$q.notify({
type: 'positive',
message: 'ลบหลักสูตรสำเร็จ',
message: response.message || 'ลบหลักสูตรสำเร็จ',
position: 'top'
});
// Refresh list
fetchCourses();
} catch (error) {
} catch (error: any) {
$q.notify({
type: 'negative',
message: 'ไม่สามารถลบหลักสูตรได้',
message: error.data?.message || 'ไม่สามารถลบหลักสูตรได้',
position: 'top'
});
}

View file

@ -125,7 +125,7 @@
:alt="course.title"
class="w-full h-full object-cover"
/>
<span v-else class="text-3xl">{{ course.icon }}</span>
<q-icon v-else :name="course.icon" class="text-3xl" />
</div>
<div class="flex-1">
<div class="font-semibold text-gray-900">{{ course.title }}</div>

View file

@ -350,12 +350,19 @@ const getRoleLabel = (role: string) => {
return labels[role] || role;
};
const formatDate = (date: string) => {
return new Date(date).toLocaleDateString('th-TH', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
const formatDate = (date: string, includeTime = true) => {
const options: Intl.DateTimeFormatOptions = {
day: 'numeric',
month: 'short',
year: '2-digit'
};
if (includeTime) {
options.hour = '2-digit';
options.minute = '2-digit';
}
return new Date(date).toLocaleDateString('th-TH', options);
};
// Avatar upload
@ -398,21 +405,21 @@ const handleAvatarUpload = async (event: Event) => {
uploadingAvatar.value = true;
try {
await userService.uploadAvatar(file);
const response = await userService.uploadAvatar(file);
// Re-fetch profile to get presigned URL from backend
await fetchProfile();
$q.notify({
type: 'positive',
message: 'อัพโหลดรูปโปรไฟล์สำเร็จ',
message: response.message || 'อัพโหลดรูปโปรไฟล์สำเร็จ',
position: 'top'
});
} catch (error) {
} catch (error: any) {
console.error('Failed to upload avatar:', error);
$q.notify({
type: 'negative',
message: 'เกิดข้อผิดพลาดในการอัพโหลดรูป',
message: error.data?.message || 'เกิดข้อผิดพลาดในการอัพโหลดรูป',
position: 'top'
});
} finally {
@ -426,7 +433,7 @@ const handleUpdateProfile = async () => {
try {
// Call real API to update profile
await userService.updateProfile({
const response = await userService.updateProfile({
first_name: editForm.value.firstName,
last_name: editForm.value.lastName,
phone: editForm.value.phone || null
@ -437,7 +444,7 @@ const handleUpdateProfile = async () => {
$q.notify({
type: 'positive',
message: 'อัพเดทโปรไฟล์สำเร็จ',
message: response.message || 'อัพเดทโปรไฟล์สำเร็จ',
position: 'top'
});
@ -445,7 +452,7 @@ const handleUpdateProfile = async () => {
} catch (error: any) {
$q.notify({
type: 'negative',
message: 'เกิดข้อผิดพลาด กรุณาลองใหม่อีกครั้ง',
message: error.data?.message || 'เกิดข้อผิดพลาด กรุณาลองใหม่อีกครั้ง',
position: 'top'
});
} finally {
@ -458,14 +465,14 @@ const handleChangePassword = async () => {
try {
// Call real API to change password
await userService.changePassword(
const response = await userService.changePassword(
passwordForm.value.currentPassword,
passwordForm.value.newPassword
);
$q.notify({
type: 'positive',
message: 'เปลี่ยนรหัสผ่านสำเร็จ',
message: response.message || 'เปลี่ยนรหัสผ่านสำเร็จ',
position: 'top'
});
@ -478,9 +485,9 @@ const handleChangePassword = async () => {
} catch (error: any) {
$q.notify({
type: 'negative',
message: error.response?.status === 401
message: error.data?.message || (error.response?.status === 401
? 'รหัสผ่านปัจจุบันไม่ถูกต้อง'
: 'เกิดข้อผิดพลาดในการเปลี่ยนรหัสผ่าน',
: 'เกิดข้อผิดพลาดในการเปลี่ยนรหัสผ่าน'),
position: 'top'
});
} finally {

View file

@ -157,11 +157,11 @@ const forgotLoading = ref(false);
const handleLogin = async () => {
loading.value = true;
try {
await authStore.login(email.value, password.value);
const response = await authStore.login(email.value, password.value);
$q.notify({
type: 'positive',
message: 'เข้าสู่ระบบสำเร็จ',
message: response.message || 'เข้าสู่ระบบสำเร็จ',
position: 'top'
});
// Redirect based on role
@ -184,11 +184,11 @@ const handleLogin = async () => {
const handleForgotPassword = async () => {
forgotLoading.value = true;
try {
await authService.forgotPassword(forgotEmail.value);
const response = await authService.forgotPassword(forgotEmail.value);
$q.notify({
type: 'positive',
message: 'ส่งลิงก์รีเซ็ตรหัสผ่านไปยังอีเมลของคุณแล้ว',
message: response.message || 'ส่งลิงก์รีเซ็ตรหัสผ่านไปยังอีเมลของคุณแล้ว',
position: 'top'
});
@ -197,7 +197,7 @@ const handleForgotPassword = async () => {
} catch (error: any) {
$q.notify({
type: 'negative',
message: error.message || 'เกิดข้อผิดพลาด กรุณาลองใหม่',
message: error.data?.message || 'เกิดข้อผิดพลาด กรุณาลองใหม่',
position: 'top'
});
} finally {

View file

@ -25,6 +25,12 @@ export interface AdminUserResponse {
};
}
export interface ApiResponse<T> {
code: number;
message: string;
data: T;
}
export interface UsersListResponse {
code: number;
message: string;
@ -368,17 +374,21 @@ export const adminService = {
return response;
},
async updateUserRole(userId: number, roleId: number): Promise<void> {
async updateUserRole(userId: number, roleId: number): Promise<ApiResponse<void>> {
const config = useRuntimeConfig();
const useMockData = config.public.useMockData as boolean;
if (useMockData) {
await new Promise(resolve => setTimeout(resolve, 500));
return;
return {
code: 200,
message: 'User role updated successfully (Mock)',
data: undefined
};
}
const token = getAuthToken();
await $fetch(`/api/admin/usermanagement/role/${userId}`, {
const response = await $fetch<ApiResponse<void>>(`/api/admin/usermanagement/role/${userId}`, {
method: 'PUT',
baseURL: config.public.apiBaseUrl as string,
headers: {
@ -389,25 +399,33 @@ export const adminService = {
role_id: roleId
}
});
return response;
},
async deleteUser(userId: number): Promise<void> {
async deleteUser(userId: number): Promise<ApiResponse<void>> {
const config = useRuntimeConfig();
const useMockData = config.public.useMockData as boolean;
if (useMockData) {
await new Promise(resolve => setTimeout(resolve, 500));
return;
return {
code: 200,
message: 'User deleted successfully (Mock)',
data: undefined
};
}
const token = getAuthToken();
await $fetch(`/api/admin/usermanagement/users/${userId}`, {
const response = await $fetch<ApiResponse<void>>(`/api/admin/usermanagement/users/${userId}`, {
method: 'DELETE',
baseURL: config.public.apiBaseUrl as string,
headers: {
Authorization: `Bearer ${token}`
}
});
return response;
},
// ============ Pending Courses ============
@ -451,17 +469,21 @@ export const adminService = {
return response.data;
},
async approveCourse(courseId: number, comment?: string): Promise<void> {
async approveCourse(courseId: number, comment?: string): Promise<ApiResponse<void>> {
const config = useRuntimeConfig();
const useMockData = config.public.useMockData as boolean;
if (useMockData) {
await new Promise(resolve => setTimeout(resolve, 500));
return;
return {
code: 200,
message: 'Course approved successfully (Mock)',
data: undefined
};
}
const token = getAuthToken();
await $fetch(`/api/admin/courses/${courseId}/approve`, {
const response = await $fetch<ApiResponse<void>>(`/api/admin/courses/${courseId}/approve`, {
method: 'POST',
baseURL: config.public.apiBaseUrl as string,
headers: {
@ -469,19 +491,25 @@ export const adminService = {
},
body: { comment: comment || '' }
});
return response;
},
async rejectCourse(courseId: number, comment: string): Promise<void> {
async rejectCourse(courseId: number, comment: string): Promise<ApiResponse<void>> {
const config = useRuntimeConfig();
const useMockData = config.public.useMockData as boolean;
if (useMockData) {
await new Promise(resolve => setTimeout(resolve, 500));
return;
return {
code: 200,
message: 'Course rejected successfully (Mock)',
data: undefined
};
}
const token = getAuthToken();
await $fetch(`/api/admin/courses/${courseId}/reject`, {
const response = await $fetch<ApiResponse<void>>(`/api/admin/courses/${courseId}/reject`, {
method: 'POST',
baseURL: config.public.apiBaseUrl as string,
headers: {
@ -489,6 +517,8 @@ export const adminService = {
},
body: { comment }
});
return response;
},
// ============ Categories ============
@ -512,17 +542,21 @@ export const adminService = {
return response.data.categories;
},
async createCategory(data: CreateCategoryRequest): Promise<CategoryResponse> {
async createCategory(data: CreateCategoryRequest): Promise<ApiResponse<CategoryResponse>> {
const config = useRuntimeConfig();
const useMockData = config.public.useMockData as boolean;
if (useMockData) {
await new Promise(resolve => setTimeout(resolve, 500));
return { ...MOCK_CATEGORIES[0], id: Date.now() };
return {
code: 200,
message: 'Category created successfully (Mock)',
data: { ...MOCK_CATEGORIES[0], id: Date.now() }
};
}
const token = getAuthToken();
const response = await $fetch<CategoryResponse>('/api/admin/categories', {
const response = await $fetch<ApiResponse<CategoryResponse>>('/api/admin/categories', {
method: 'POST',
baseURL: config.public.apiBaseUrl as string,
headers: {
@ -534,17 +568,21 @@ export const adminService = {
return response;
},
async updateCategory(id: number, data: UpdateCategoryRequest): Promise<CategoryResponse> {
async updateCategory(id: number, data: UpdateCategoryRequest): Promise<ApiResponse<CategoryResponse>> {
const config = useRuntimeConfig();
const useMockData = config.public.useMockData as boolean;
if (useMockData) {
await new Promise(resolve => setTimeout(resolve, 500));
return { ...MOCK_CATEGORIES[0], id };
return {
code: 200,
message: 'Category updated successfully (Mock)',
data: { ...MOCK_CATEGORIES[0], id }
};
}
const token = getAuthToken();
const response = await $fetch<CategoryResponse>(`/api/admin/categories/${id}`, {
const response = await $fetch<ApiResponse<CategoryResponse>>(`/api/admin/categories/${id}`, {
method: 'PUT',
baseURL: config.public.apiBaseUrl as string,
headers: {
@ -556,23 +594,29 @@ export const adminService = {
return response;
},
async deleteCategory(id: number): Promise<void> {
async deleteCategory(id: number): Promise<ApiResponse<void>> {
const config = useRuntimeConfig();
const useMockData = config.public.useMockData as boolean;
if (useMockData) {
await new Promise(resolve => setTimeout(resolve, 500));
return;
return {
code: 200,
message: 'Category deleted successfully (Mock)',
data: undefined
};
}
const token = getAuthToken();
await $fetch(`/api/admin/categories/${id}`, {
const response = await $fetch<ApiResponse<void>>(`/api/admin/categories/${id}`, {
method: 'DELETE',
baseURL: config.public.apiBaseUrl as string,
headers: {
Authorization: `Bearer ${token}`
}
});
return response;
}
};

View file

@ -46,6 +46,13 @@ export interface LoginResponse {
role: string;
avatarUrl?: string | null;
};
message?: string;
}
export interface ApiResponse<T> {
code: number;
message: string;
data: T;
}
// Mock data for development
@ -135,14 +142,23 @@ export const authService = {
lastName: response.user.profile.last_name,
role: response.user.role.code,
avatarUrl: response.user.profile.avatar_url
}
},
message: 'เข้าสู่ระบบสำเร็จ' // Note: Backend usually returns message too, but we can default it or use backend's
};
} catch (error: any) {
// Re-throw custom errors (like STUDENT role block)
if (error.message && !error.response) {
throw error;
}
// Handle API errors
const apiError = error.data?.error || error.data;
const errorMessage = apiError?.message || error.message;
if (errorMessage) {
throw new Error(errorMessage);
}
if (error.response?.status === 401) {
throw new Error('อีเมลหรือรหัสผ่านไม่ถูกต้อง');
}
@ -161,22 +177,27 @@ export const authService = {
userCookie.value = null;
},
async forgotPassword(email: string): Promise<void> {
async forgotPassword(email: string): Promise<ApiResponse<void>> {
const config = useRuntimeConfig();
const useMockData = config.public.useMockData as boolean;
if (useMockData) {
// Mock: simulate sending email
await new Promise(resolve => setTimeout(resolve, 1000));
return;
return {
code: 200,
message: 'ส่งลิงก์รีเซ็ตรหัสผ่านไปยังอีเมลของคุณแล้ว (Mock)',
data: undefined
};
}
// Real API
await $fetch('/api/auth/reset-request', {
const response = await $fetch<ApiResponse<void>>('/api/auth/reset-request', {
method: 'POST',
baseURL: config.public.apiBaseUrl as string,
body: { email }
});
return response;
},
async resetPassword(token: string, password: string): Promise<void> {

View file

@ -214,14 +214,18 @@ export const instructorService = {
}
// Clean data - remove empty thumbnail_url
const cleanedData = { ...data };
if (!cleanedData.thumbnail_url) {
delete cleanedData.thumbnail_url;
}
const formData = new FormData();
formData.append('data', JSON.stringify(cleanedData));
return await authRequest<ApiResponse<CourseResponse>>('/api/instructors/courses', {
method: 'POST',
body: cleanedData
body: formData
});
},

View file

@ -25,6 +25,12 @@ export interface UserProfileResponse {
};
}
export interface ApiResponse<T> {
code: number;
message: string;
data: T;
}
// Request body for PUT /api/user/me
export interface UpdateProfileRequest {
prefix?: {
@ -132,7 +138,7 @@ export const userService = {
return response;
},
async updateProfile(data: UpdateProfileRequest): Promise<UserProfileResponse> {
async updateProfile(data: UpdateProfileRequest): Promise<ApiResponse<UserProfileResponse>> {
const config = useRuntimeConfig();
const useMockData = config.public.useMockData as boolean;
@ -140,11 +146,15 @@ export const userService = {
await new Promise(resolve => setTimeout(resolve, 500));
// In mock mode, just return the current profile with updates
const profile = await this.mockGetProfile();
return { ...profile, profile: { ...profile.profile, ...data } };
return {
code: 200,
message: 'Update profile successfully',
data: { ...profile, profile: { ...profile.profile, ...data } }
};
}
const token = getAuthToken();
const response = await $fetch<UserProfileResponse>('/api/user/me', {
const response = await $fetch<ApiResponse<UserProfileResponse>>('/api/user/me', {
method: 'PUT',
baseURL: config.public.apiBaseUrl as string,
headers: {
@ -156,18 +166,22 @@ export const userService = {
return response;
},
async changePassword(oldPassword: string, newPassword: string): Promise<void> {
async changePassword(oldPassword: string, newPassword: string): Promise<ApiResponse<null>> {
const config = useRuntimeConfig();
const useMockData = config.public.useMockData as boolean;
if (useMockData) {
await new Promise(resolve => setTimeout(resolve, 500));
// In mock mode, just simulate success
return;
return {
code: 200,
message: 'Change password successfully',
data: null
};
}
const token = getAuthToken();
await $fetch('/api/user/change-password', {
const response = await $fetch<ApiResponse<null>>('/api/user/change-password', {
method: 'POST',
baseURL: config.public.apiBaseUrl as string,
headers: {
@ -178,23 +192,29 @@ export const userService = {
newPassword
}
});
return response;
},
async uploadAvatar(file: File): Promise<{ avatar_url: string; id: number }> {
async uploadAvatar(file: File): Promise<ApiResponse<{ avatar_url: string; id: number }>> {
const config = useRuntimeConfig();
const useMockData = config.public.useMockData as boolean;
if (useMockData) {
await new Promise(resolve => setTimeout(resolve, 500));
// Return mock URL
return { avatar_url: URL.createObjectURL(file), id: 1 };
return {
code: 200,
message: 'Upload avatar successfully',
data: { avatar_url: URL.createObjectURL(file), id: 1 }
};
}
const token = getAuthToken();
const formData = new FormData();
formData.append('file', file);
const response = await $fetch<{ code: number; message: string; data: { avatar_url: string; id: number } }>('/api/user/upload-avatar', {
const response = await $fetch<ApiResponse<{ avatar_url: string; id: number }>>('/api/user/upload-avatar', {
method: 'POST',
baseURL: config.public.apiBaseUrl as string,
headers: {
@ -203,6 +223,6 @@ export const userService = {
body: formData
});
return response.data;
return response;
}
};

View file

@ -50,7 +50,7 @@ export const useAuthStore = defineStore('auth', {
refreshTokenCookie.value = response.refreshToken;
userCookie.value = JSON.stringify(this.user);
return { token: this.token, user: this.user };
return response;
} catch (error: any) {
throw error;
}

View file

@ -52,7 +52,7 @@ export const useInstructorStore = defineStore('instructor', {
title: course.title.th,
students: 0, // TODO: Get from API
lessons: 0, // TODO: Get from course detail API
icon: ['📘', '📗', '📙', '📕', '📒'][index % 5],
icon: 'book',
thumbnail: course.thumbnail_url || null
}));
} catch (error) {