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.

69 lines
2.2 KiB

  1. import { plainToInstance } from 'class-transformer'
  2. import { ExampleDTO, ExampleListDTO } from './exampleData'
  3. import { ExampleRepository, SearchOption } from '~/domain/models/example/exampleRepository'
  4. import { ExampleItem } from '~/domain/models/example/example'
  5. export class ExampleApplicationService {
  6. constructor(
  7. private readonly repository: ExampleRepository
  8. ) {}
  9. public async list(projectId: string, options: SearchOption): Promise<ExampleListDTO> {
  10. try {
  11. const item = await this.repository.list(projectId, options)
  12. return new ExampleListDTO(item)
  13. } catch(e: any) {
  14. throw new Error(e.response.data.detail)
  15. }
  16. }
  17. public async fetchOne(projectId: string, page: string, q: string, isChecked: string): Promise<ExampleListDTO> {
  18. const offset = (parseInt(page, 10) - 1).toString()
  19. const options: SearchOption = {
  20. limit: '1',
  21. offset,
  22. q,
  23. isChecked,
  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: any) {
  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: any) {
  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 findById(projectId: string, exampleId: number): Promise<ExampleDTO> {
  49. const response = await this.repository.findById(projectId, exampleId)
  50. return new ExampleDTO(response)
  51. }
  52. public async confirm(projectId: string, exampleId: number): Promise<void> {
  53. await this.repository.confirm(projectId, exampleId)
  54. }
  55. private toModel(item: ExampleDTO): ExampleItem {
  56. // Todo: annotationApprover, commentCount, fileUrl and isConfirmed
  57. // is not copied correctly.
  58. return plainToInstance(ExampleItem, item)
  59. }
  60. }