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.

70 lines
2.0 KiB

  1. import { ExampleDTO, ExampleListDTO } from './exampleData'
  2. import { ExampleRepository, SearchOption } from '~/domain/models/example/exampleRepository'
  3. import { ExampleItem } from '~/domain/models/example/example'
  4. export class ExampleApplicationService {
  5. constructor(
  6. private readonly repository: ExampleRepository
  7. ) {}
  8. public async list(projectId: string, options: SearchOption): Promise<ExampleListDTO> {
  9. try {
  10. const item = await this.repository.list(projectId, options)
  11. return new ExampleListDTO(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<ExampleListDTO> {
  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: ExampleDTO): Promise<ExampleDTO> {
  28. try {
  29. const doc = this.toModel(item)
  30. const response = await this.repository.create(projectId, doc)
  31. return new ExampleDTO(response)
  32. } catch(e) {
  33. throw new Error(e.response.data.detail)
  34. }
  35. }
  36. public async update(projectId: string, item: ExampleDTO): 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: ExampleDTO[]): Promise<void> {
  45. const ids = items.map(item => item.id)
  46. return this.repository.bulkDelete(projectId, ids)
  47. }
  48. public async approve(projectId: string, docId: number, approved: boolean): Promise<void> {
  49. await this.repository.approve(projectId, docId, approved)
  50. }
  51. private toModel(item: ExampleDTO): ExampleItem {
  52. return new ExampleItem(
  53. item.id,
  54. item.text,
  55. item.meta,
  56. item.annotationApprover,
  57. item.commentCount,
  58. item.fileUrl
  59. )
  60. }
  61. }