82 lines
2.3 KiB
TypeScript
82 lines
2.3 KiB
TypeScript
|
|
import { PagedResultDto } from '@/proxy'
|
||
|
|
import { NoteDto, NoteFileDto } from '@/proxy/note/models'
|
||
|
|
import apiService from '@/services/api.service'
|
||
|
|
import { AxiosError } from 'axios'
|
||
|
|
|
||
|
|
class NoteService {
|
||
|
|
async getList(params?: any): Promise<PagedResultDto<NoteDto>> {
|
||
|
|
const response = await apiService.fetchData<PagedResultDto<NoteDto>>({
|
||
|
|
url: '/api/app/note',
|
||
|
|
method: 'GET',
|
||
|
|
params,
|
||
|
|
})
|
||
|
|
return response.data
|
||
|
|
}
|
||
|
|
|
||
|
|
async get(id: string): Promise<NoteDto> {
|
||
|
|
const response = await apiService.fetchData<NoteDto>({
|
||
|
|
url: `/api/app/note/${id}`,
|
||
|
|
method: 'GET',
|
||
|
|
})
|
||
|
|
return response.data
|
||
|
|
}
|
||
|
|
|
||
|
|
async create(data: FormData): Promise<any> {
|
||
|
|
try {
|
||
|
|
const response = await apiService.fetchData({
|
||
|
|
url: '/api/app/note',
|
||
|
|
method: 'POST',
|
||
|
|
data, // FormData olduğu için headers belirtmeye gerek yok
|
||
|
|
})
|
||
|
|
return response.data
|
||
|
|
} catch (error) {
|
||
|
|
if (error instanceof AxiosError) {
|
||
|
|
console.error('Error creating note:', error.response?.data)
|
||
|
|
return error.response?.data
|
||
|
|
} else {
|
||
|
|
console.error('Unexpected error:', error)
|
||
|
|
throw error
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async update(id: string, data: NoteDto): Promise<NoteDto> {
|
||
|
|
const response = await apiService.fetchData<NoteDto>({
|
||
|
|
url: `/api/app/note/${id}`,
|
||
|
|
method: 'PUT',
|
||
|
|
data: data as any,
|
||
|
|
})
|
||
|
|
return response.data
|
||
|
|
}
|
||
|
|
|
||
|
|
async delete(id: string): Promise<void> {
|
||
|
|
await apiService.fetchData({
|
||
|
|
url: `/api/app/note/${id}`,
|
||
|
|
method: 'DELETE',
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
async downloadFile(savedFileName: string, fileName: string, fileType: string) {
|
||
|
|
const response = await apiService.fetchData<NoteFileDto>({
|
||
|
|
url: `/api/app/note/download?savedFileName=${savedFileName}`,
|
||
|
|
method: 'GET',
|
||
|
|
})
|
||
|
|
|
||
|
|
const base64 = response.data.fileBase64
|
||
|
|
const binary = atob(base64) // base64 -> binary string
|
||
|
|
const bytes = new Uint8Array(binary.length)
|
||
|
|
|
||
|
|
for (let i = 0; i < binary.length; i++) {
|
||
|
|
bytes[i] = binary.charCodeAt(i)
|
||
|
|
}
|
||
|
|
|
||
|
|
const blob = new Blob([bytes], { type: response.data.fileType })
|
||
|
|
const link = document.createElement('a')
|
||
|
|
link.href = URL.createObjectURL(blob)
|
||
|
|
link.download = response.data.fileName || fileName
|
||
|
|
link.click()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export const noteService = new NoteService()
|