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.

74 lines
2.3 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  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(private readonly repository: ExampleRepository) {}
  7. public async list(projectId: string, options: SearchOption): Promise<ExampleListDTO> {
  8. try {
  9. const item = await this.repository.list(projectId, options)
  10. return new ExampleListDTO(item)
  11. } catch (e: any) {
  12. throw new Error(e.response.data.detail)
  13. }
  14. }
  15. public async fetchOne(
  16. projectId: string,
  17. page: string,
  18. q: string,
  19. isChecked: string,
  20. ordering: string
  21. ): Promise<ExampleListDTO> {
  22. const offset = (parseInt(page, 10) - 1).toString()
  23. const options: SearchOption = {
  24. limit: '1',
  25. offset,
  26. q,
  27. isChecked,
  28. ordering
  29. }
  30. return await this.list(projectId, options)
  31. }
  32. public async create(projectId: string, item: ExampleDTO): Promise<ExampleDTO> {
  33. try {
  34. const doc = this.toModel(item)
  35. const response = await this.repository.create(projectId, doc)
  36. return new ExampleDTO(response)
  37. } catch (e: any) {
  38. throw new Error(e.response.data.detail)
  39. }
  40. }
  41. public async update(projectId: string, item: ExampleDTO): Promise<void> {
  42. try {
  43. const doc = this.toModel(item)
  44. await this.repository.update(projectId, doc)
  45. } catch (e: any) {
  46. throw new Error(e.response.data.detail)
  47. }
  48. }
  49. public bulkDelete(projectId: string, items: ExampleDTO[]): Promise<void> {
  50. const ids = items.map((item) => item.id)
  51. return this.repository.bulkDelete(projectId, ids)
  52. }
  53. public async findById(projectId: string, exampleId: number): Promise<ExampleDTO> {
  54. const response = await this.repository.findById(projectId, exampleId)
  55. return new ExampleDTO(response)
  56. }
  57. public async confirm(projectId: string, exampleId: number): Promise<void> {
  58. await this.repository.confirm(projectId, exampleId)
  59. }
  60. private toModel(item: ExampleDTO): ExampleItem {
  61. // Todo: annotationApprover, commentCount, fileUrl and isConfirmed
  62. // is not copied correctly.
  63. return plainToInstance(ExampleItem, item)
  64. }
  65. }