교육과정, 강의등록 완료1
This commit is contained in:
@@ -46,10 +46,13 @@ class ApiService {
|
||||
/**
|
||||
* 기본 헤더 생성
|
||||
*/
|
||||
private getDefaultHeaders(): Record<string, string> {
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
private getDefaultHeaders(isFormData: boolean = false): Record<string, string> {
|
||||
const headers: Record<string, string> = {};
|
||||
|
||||
// FormData인 경우 Content-Type을 설정하지 않음 (브라우저가 자동으로 설정)
|
||||
if (!isFormData) {
|
||||
headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
|
||||
const token = this.getToken();
|
||||
if (token) {
|
||||
@@ -98,16 +101,20 @@ class ApiService {
|
||||
|
||||
const url = endpoint.startsWith('http') ? endpoint : `${this.baseURL}${endpoint}`;
|
||||
|
||||
// FormData 여부 확인
|
||||
const isFormData = body instanceof FormData;
|
||||
|
||||
const requestOptions: RequestInit = {
|
||||
method,
|
||||
headers: {
|
||||
...this.getDefaultHeaders(),
|
||||
...this.getDefaultHeaders(isFormData),
|
||||
...headers,
|
||||
},
|
||||
};
|
||||
|
||||
if (body && method !== 'GET') {
|
||||
requestOptions.body = JSON.stringify(body);
|
||||
// FormData인 경우 그대로 사용, 아닌 경우 JSON으로 변환
|
||||
requestOptions.body = isFormData ? body : JSON.stringify(body);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -277,11 +284,12 @@ class ApiService {
|
||||
* 과목 수정
|
||||
*/
|
||||
async updateSubject(subjectId: string, subjectData: {
|
||||
courseName: string;
|
||||
instructorName: string;
|
||||
title: string;
|
||||
instructor: string;
|
||||
imageKey?: string | null;
|
||||
}) {
|
||||
return this.request(`/subjects/${subjectId}`, {
|
||||
method: 'PUT',
|
||||
method: 'PATCH',
|
||||
body: subjectData,
|
||||
});
|
||||
}
|
||||
@@ -347,12 +355,109 @@ class ApiService {
|
||||
return this.request('/lessons');
|
||||
}
|
||||
|
||||
/**
|
||||
* 강좌 리스트 조회
|
||||
*/
|
||||
async getLectures() {
|
||||
return this.request('/lectures', {
|
||||
method: 'GET',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 특정 강좌 조회
|
||||
*/
|
||||
async getLecture(id: string | number) {
|
||||
return this.request(`/lectures/${id}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 강좌(lecture) 생성
|
||||
*/
|
||||
async createLecture(lectureData: {
|
||||
subjectId: number;
|
||||
title: string;
|
||||
objective: string;
|
||||
videoUrl?: string;
|
||||
webglUrl?: string;
|
||||
csvKey?: string;
|
||||
}) {
|
||||
return this.request('/lectures', {
|
||||
method: 'POST',
|
||||
body: lectureData,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 리소스 조회
|
||||
*/
|
||||
async getResources() {
|
||||
return this.request('/resources');
|
||||
}
|
||||
|
||||
// ===== 파일 업로드 관련 API =====
|
||||
|
||||
/**
|
||||
* 단일 파일 업로드
|
||||
* @param file 업로드할 파일 (File 객체 또는 Blob)
|
||||
*/
|
||||
async uploadFile(file: File | Blob) {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
|
||||
return this.request('/uploads-api/file', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 다중 파일 업로드
|
||||
* @param files 업로드할 파일 배열 (File[] 또는 Blob[])
|
||||
*/
|
||||
async uploadFiles(files: (File | Blob)[]) {
|
||||
const formData = new FormData();
|
||||
files.forEach((file, index) => {
|
||||
formData.append('files', file);
|
||||
});
|
||||
|
||||
return this.request('/uploads-api/files', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 파일 다운로드 (이미지 URL 가져오기)
|
||||
* @param fileKey 파일 키
|
||||
* @returns 파일 URL (Blob URL), 파일이 없으면 null 반환
|
||||
*/
|
||||
async getFile(fileKey: string): Promise<string | null> {
|
||||
const url = `${this.baseURL}/api/files/${fileKey}`;
|
||||
const token = this.getToken();
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
...(token && { Authorization: `Bearer ${token}` }),
|
||||
},
|
||||
});
|
||||
|
||||
// 404 에러는 이미지가 없는 것으로 간주하고 null 반환
|
||||
if (response.status === 404) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`파일을 가져오는데 실패했습니다. (${response.status})`);
|
||||
}
|
||||
|
||||
// 이미지 파일이므로 Blob으로 변환하여 URL 생성
|
||||
const blob = await response.blob();
|
||||
return URL.createObjectURL(blob);
|
||||
}
|
||||
}
|
||||
|
||||
// 기본 API 서비스 인스턴스 생성
|
||||
|
||||
Reference in New Issue
Block a user