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.

79 lines
2.3 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): Promise<ExampleListDTO> {
  17. const offset = (parseInt(page, 10) - 1).toString()
  18. const options: SearchOption = {
  19. limit: '1',
  20. offset,
  21. q,
  22. isChecked,
  23. }
  24. return await this.list(projectId, options)
  25. }
  26. public async create(projectId: string, item: ExampleDTO): Promise<ExampleDTO> {
  27. try {
  28. const doc = this.toModel(item)
  29. const response = await this.repository.create(projectId, doc)
  30. return new ExampleDTO(response)
  31. } catch(e) {
  32. throw new Error(e.response.data.detail)
  33. }
  34. }
  35. public async update(projectId: string, item: ExampleDTO): Promise<void> {
  36. try {
  37. const doc = this.toModel(item)
  38. await this.repository.update(projectId, doc)
  39. } catch(e) {
  40. throw new Error(e.response.data.detail)
  41. }
  42. }
  43. public bulkDelete(projectId: string, items: ExampleDTO[]): Promise<void> {
  44. const ids = items.map(item => item.id)
  45. return this.repository.bulkDelete(projectId, ids)
  46. }
  47. public async findById(projectId: string, exampleId: number): Promise<ExampleDTO> {
  48. const response = await this.repository.findById(projectId, exampleId)
  49. return new ExampleDTO(response)
  50. }
  51. public async approve(projectId: string, docId: number, approved: boolean): Promise<void> {
  52. await this.repository.approve(projectId, docId, approved)
  53. }
  54. public async confirm(projectId: string, exampleId: number): Promise<void> {
  55. await this.repository.confirm(projectId, exampleId)
  56. }
  57. private toModel(item: ExampleDTO): ExampleItem {
  58. return new ExampleItem(
  59. item.id,
  60. item.text,
  61. item.meta,
  62. item.annotationApprover,
  63. item.commentCount,
  64. item.fileUrl,
  65. item.isConfirmed
  66. )
  67. }
  68. }