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.

64 lines
1.8 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) {
  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. const response = new ProjectDTO(item)
  19. return new ProjectDTO(item)
  20. }
  21. public async create(item: ProjectWriteDTO): Promise<ProjectDTO> {
  22. try {
  23. const project = this.toWriteModel(item)
  24. const response = await this.repository.create(project)
  25. return new ProjectDTO(response)
  26. } catch(e) {
  27. throw new Error(e.response.data.detail)
  28. }
  29. }
  30. public async update(item: ProjectWriteDTO): Promise<void> {
  31. try {
  32. const project = this.toWriteModel(item)
  33. await this.repository.update(project)
  34. } catch(e) {
  35. throw new Error(e.response.data.detail)
  36. }
  37. }
  38. public bulkDelete(items: ProjectDTO[]): Promise<void> {
  39. const ids = items.map(item => item.id)
  40. return this.repository.bulkDelete(ids)
  41. }
  42. private toWriteModel(item: ProjectWriteDTO): ProjectWriteItem {
  43. return new ProjectWriteItem(
  44. item.id,
  45. item.name,
  46. item.description,
  47. item.guideline,
  48. item.projectType,
  49. item.enableRandomOrder,
  50. item.enableShareAnnotation,
  51. item.singleClassClassification,
  52. item.allowOverlapping,
  53. item.graphemeMode
  54. )
  55. }
  56. }