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.

63 lines
1.7 KiB

  1. import { ProjectDTO, ProjectWriteDTO } from './projectData'
  2. import { ProjectRepository } from '~/domain/models/project/projectRepository'
  3. import { ProjectWriteItem } from '~/domain/models/project/project'
  4. export class ProjectApplicationService {
  5. constructor(
  6. private readonly repository: ProjectRepository
  7. ) {}
  8. public async list(): Promise<ProjectDTO[]> {
  9. try {
  10. const items = await this.repository.list()
  11. return items.map(item => new ProjectDTO(item))
  12. } catch(e: any) {
  13. throw new Error(e.response.data.detail)
  14. }
  15. }
  16. public async findById(id: string): Promise<ProjectDTO> {
  17. const item = await this.repository.findById(id)
  18. return new ProjectDTO(item)
  19. }
  20. public async create(item: ProjectWriteDTO): Promise<ProjectDTO> {
  21. try {
  22. const project = this.toWriteModel(item)
  23. const response = await this.repository.create(project)
  24. return new ProjectDTO(response)
  25. } catch(e: any) {
  26. throw new Error(e.response.data.detail)
  27. }
  28. }
  29. public async update(item: ProjectWriteDTO): Promise<void> {
  30. try {
  31. const project = this.toWriteModel(item)
  32. await this.repository.update(project)
  33. } catch(e: any) {
  34. throw new Error(e.response.data.detail)
  35. }
  36. }
  37. public bulkDelete(items: ProjectDTO[]): Promise<void> {
  38. const ids = items.map(item => item.id)
  39. return this.repository.bulkDelete(ids)
  40. }
  41. private toWriteModel(item: ProjectWriteDTO): ProjectWriteItem {
  42. return new ProjectWriteItem(
  43. item.id,
  44. item.name,
  45. item.description,
  46. item.guideline,
  47. item.projectType,
  48. item.enableRandomOrder,
  49. item.enableShareAnnotation,
  50. item.singleClassClassification,
  51. item.allowOverlapping,
  52. item.graphemeMode
  53. )
  54. }
  55. }