edit: response.message
This commit is contained in:
parent
d7f824f353
commit
7de5457170
21 changed files with 227 additions and 127 deletions
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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> {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
});
|
||||
},
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue