You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.7 KiB

  1. import ApiService from '@/services/api.service'
  2. import { DocumentRepository, SearchOption } from '@/domain/models/document/documentRepository'
  3. import { DocumentItem, DocumentItemList } from '~/domain/models/document/document'
  4. export class APIDocumentRepository implements DocumentRepository {
  5. constructor(
  6. private readonly request = ApiService
  7. ) {}
  8. async list(projectId: string, { limit = '10', offset = '0', q = '', isChecked = '', filterName = '' }: SearchOption): Promise<DocumentItemList> {
  9. const url = `/projects/${projectId}/docs?limit=${limit}&offset=${offset}&q=${q}&${filterName}=${isChecked}`
  10. const response = await this.request.get(url)
  11. return DocumentItemList.valueOf(response.data)
  12. }
  13. async create(projectId: string, item: DocumentItem): Promise<DocumentItem> {
  14. const url = `/projects/${projectId}/docs`
  15. const response = await this.request.post(url, item.toObject())
  16. return DocumentItem.valueOf(response.data)
  17. }
  18. async update(projectId: string, item: DocumentItem): Promise<DocumentItem> {
  19. const url = `/projects/${projectId}/docs/${item.id}`
  20. const response = await this.request.patch(url, item.toObject())
  21. return DocumentItem.valueOf(response.data)
  22. }
  23. async bulkDelete(projectId: string, ids: number[]): Promise<void> {
  24. const url = `/projects/${projectId}/docs`
  25. await this.request.delete(url, { ids })
  26. }
  27. async deleteAll(projectId: string): Promise<void> {
  28. const url = `/projects/${projectId}/docs`
  29. await this.request.delete(url)
  30. }
  31. async uploadFile(projectId: string, payload: FormData): Promise<void> {
  32. const url = `/projects/${projectId}/docs/upload`
  33. const config = {
  34. headers: {
  35. 'Content-Type': 'multipart/form-data'
  36. }
  37. }
  38. await this.request.post(url, payload, config)
  39. }
  40. async exportFile(projectId: string, format: string, onlyApproved: boolean): Promise<any> {
  41. const headers = { 'Content-Type': '', 'Accept': ''}
  42. if (format === 'csv') {
  43. headers.Accept = 'text/csv; charset=utf-8'
  44. headers['Content-Type'] = 'text/csv; charset=utf-8'
  45. } else if (format === 'txt') {
  46. headers.Accept = 'text/plain; charset=utf-8'
  47. headers['Content-Type'] = 'text/plain; charset=utf-8'
  48. } else {
  49. headers.Accept = 'application/json'
  50. headers['Content-Type'] = 'application/json'
  51. }
  52. const config = {
  53. responseType: 'blob',
  54. params: {
  55. q: format,
  56. onlyApproved
  57. },
  58. headers
  59. }
  60. const url = `/projects/${projectId}/docs/download`
  61. return await this.request.get(url, config)
  62. }
  63. async approve(projectId: string, docId: number, approved: boolean): Promise<void> {
  64. const url = `/projects/${projectId}/docs/${docId}/approve-labels`
  65. await this.request.post(url, { approved })
  66. }
  67. }