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.

101 lines
2.9 KiB

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