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.

105 lines
3.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. import { ProjectReadItem, ProjectWriteItem, CurrentUsersRole, ProjectType } from '@/models/project'
  2. import { ProjectItemListRepository } from '@/repositories/project/interface'
  3. import { FormatItem } from '@/models/format'
  4. export class FormatDTO {
  5. example: string
  6. type: string
  7. text: string
  8. extension: string
  9. constructor(item: FormatItem) {
  10. this.example = item.example
  11. this.type = item.type
  12. this.text = item.text
  13. this.extension = item.extension
  14. }
  15. }
  16. export class ProjectDTO {
  17. id: number
  18. name: string
  19. description: string
  20. guideline: string
  21. current_users_role: CurrentUsersRole
  22. projectType: ProjectType
  23. updatedAt: string
  24. enableRandomizeDocOrder: boolean
  25. enableShareAnnotation: boolean
  26. pageLink: string
  27. downloadFormats: FormatDTO[]
  28. uploadFormats: FormatDTO[]
  29. constructor(item: ProjectReadItem) {
  30. this.id = item.id
  31. this.name = item.name
  32. this.description = item.description
  33. this.guideline = item.guideline
  34. this.current_users_role = item.current_users_role
  35. this.projectType = item.project_type
  36. this.updatedAt = item.updated_at
  37. this.enableRandomizeDocOrder = item.randomize_document_order
  38. this.enableShareAnnotation = item.collaborative_annotation
  39. this.pageLink = item.annotationPageLink
  40. this.downloadFormats = item.downloadFormats.map(f => new FormatDTO(f))
  41. this.uploadFormats = item.uploadFormats.map(f => new FormatDTO(f))
  42. }
  43. }
  44. export type ProjectWriteDTO = Pick<ProjectDTO, 'id' | 'name' | 'description' | 'guideline' | 'projectType' | 'enableRandomizeDocOrder' | 'enableShareAnnotation'>
  45. export class ProjectApplicationService {
  46. constructor(
  47. private readonly repository: ProjectItemListRepository
  48. ) {}
  49. public async list(): Promise<ProjectDTO[]> {
  50. try {
  51. const items = await this.repository.list()
  52. return items.map(item => new ProjectDTO(item))
  53. } catch(e) {
  54. throw new Error(e.response.data.detail)
  55. }
  56. }
  57. public async findById(id: string): Promise<ProjectDTO> {
  58. const item = await this.repository.findById(id)
  59. const response = new ProjectDTO(item)
  60. return new ProjectDTO(item)
  61. }
  62. public async create(item: ProjectWriteDTO): Promise<ProjectDTO> {
  63. try {
  64. const project = this.toWriteModel(item)
  65. const response = await this.repository.create(project)
  66. return new ProjectDTO(response)
  67. } catch(e) {
  68. throw new Error(e.response.data.detail)
  69. }
  70. }
  71. public async update(item: ProjectWriteDTO): Promise<void> {
  72. try {
  73. const project = this.toWriteModel(item)
  74. await this.repository.update(project)
  75. } catch(e) {
  76. throw new Error(e.response.data.detail)
  77. }
  78. }
  79. public bulkDelete(items: ProjectDTO[]): Promise<void> {
  80. const ids = items.map(item => item.id)
  81. return this.repository.bulkDelete(ids)
  82. }
  83. private toWriteModel(item: ProjectWriteDTO): ProjectWriteItem {
  84. return new ProjectWriteItem(
  85. item.id,
  86. item.name,
  87. item.description,
  88. item.guideline,
  89. item.projectType,
  90. item.enableRandomizeDocOrder,
  91. item.enableShareAnnotation
  92. )
  93. }
  94. }