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.

88 lines
2.8 KiB

  1. import { DocumentDTO, DocumentListDTO } from './documentData'
  2. import { DocumentRepository, SearchOption } from '~/domain/models/document/documentRepository'
  3. import { DocumentItem } from '~/domain/models/document/document'
  4. export class DocumentApplicationService {
  5. constructor(
  6. private readonly repository: DocumentRepository
  7. ) {}
  8. public async list(projectId: string, options: SearchOption): Promise<DocumentListDTO> {
  9. try {
  10. const item = await this.repository.list(projectId, options)
  11. return new DocumentListDTO(item)
  12. } catch(e) {
  13. throw new Error(e.response.data.detail)
  14. }
  15. }
  16. public async fetchOne(projectId: string, page: string, q: string, isChecked: string, filterName: string): Promise<DocumentListDTO> {
  17. const offset = (parseInt(page, 10) - 1).toString()
  18. const options: SearchOption = {
  19. limit: '1',
  20. offset,
  21. q,
  22. isChecked,
  23. filterName
  24. }
  25. return await this.list(projectId, options)
  26. }
  27. public async create(projectId: string, item: DocumentDTO): Promise<DocumentDTO> {
  28. try {
  29. const doc = this.toModel(item)
  30. const response = await this.repository.create(projectId, doc)
  31. return new DocumentDTO(response)
  32. } catch(e) {
  33. throw new Error(e.response.data.detail)
  34. }
  35. }
  36. public async update(projectId: string, item: DocumentDTO): Promise<void> {
  37. try {
  38. const doc = this.toModel(item)
  39. await this.repository.update(projectId, doc)
  40. } catch(e) {
  41. throw new Error(e.response.data.detail)
  42. }
  43. }
  44. public bulkDelete(projectId: string, items: DocumentDTO[]): Promise<void> {
  45. const ids = items.map(item => item.id)
  46. return this.repository.bulkDelete(projectId, ids)
  47. }
  48. public async download(
  49. projectId: string, filename: string, format: any, onlyApproved: boolean
  50. ): Promise<void> {
  51. const response = await this.repository.exportFile(projectId, format.type, onlyApproved)
  52. const url = window.URL.createObjectURL(new Blob([response.data]))
  53. const link = document.createElement('a')
  54. link.href = url
  55. link.setAttribute('download', `${filename}.${format.extension}`)
  56. document.body.appendChild(link)
  57. link.click()
  58. }
  59. public async upload(projectId: string, file: File, format: string): Promise<void> {
  60. const formData = new FormData()
  61. formData.append('file', file)
  62. formData.append('format', format)
  63. const response = await this.repository.uploadFile(projectId, formData)
  64. }
  65. public async approve(projectId: string, docId: number, approved: boolean): Promise<void> {
  66. await this.repository.approve(projectId, docId, approved)
  67. }
  68. private toModel(item: DocumentDTO): DocumentItem {
  69. return new DocumentItem(
  70. item.id,
  71. item.text,
  72. item.meta,
  73. item.annotationApprover,
  74. item.commentCount
  75. )
  76. }
  77. }