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.

95 lines
2.9 KiB

2 years ago
  1. import ApiService from '@/services/api.service'
  2. import { ProjectRepository, SearchQuery } from '@/domain/models/project/projectRepository'
  3. import { ProjectReadItem, ProjectWriteItem, ProjectItemList } from '@/domain/models/project/project'
  4. function toModel(item: { [key: string]: any }): ProjectReadItem {
  5. return new ProjectReadItem(
  6. item.id,
  7. item.name,
  8. item.description,
  9. item.guideline,
  10. item.users,
  11. item.tags,
  12. item.project_type,
  13. item.created_at,
  14. item.updated_at,
  15. item.author,
  16. item.random_order,
  17. item.collaborative_annotation,
  18. item.single_class_classification,
  19. item.resourcetype,
  20. item.allow_overlapping,
  21. item.grapheme_mode,
  22. item.use_relation,
  23. item.is_text_project,
  24. item.can_define_label,
  25. item.can_define_relation,
  26. item.can_define_span,
  27. item.can_define_category
  28. )
  29. }
  30. function toPayload(item: ProjectWriteItem): { [key: string]: any } {
  31. return {
  32. id: item.id,
  33. name: item.name,
  34. description: item.description,
  35. guideline: item.guideline,
  36. project_type: item.projectType,
  37. random_order: item.randomOrder,
  38. collaborative_annotation: item.collaborativeAnnotation,
  39. single_class_classification: item.exclusiveCategories,
  40. allow_overlapping: item.allowOverlapping,
  41. grapheme_mode: item.graphemeMode,
  42. use_relation: item.useRelation,
  43. tags: item.tags.map((tag) => ({ text: tag })),
  44. resourcetype: item.resourceType
  45. }
  46. }
  47. export class APIProjectRepository implements ProjectRepository {
  48. constructor(private readonly request = ApiService) {}
  49. async list(query: SearchQuery): Promise<ProjectItemList> {
  50. const fieldMapper = {
  51. name: 'name',
  52. createdAt: 'created_at',
  53. projectType: 'project_type',
  54. author: 'created_by'
  55. }
  56. const sortBy = fieldMapper[query.sortBy]
  57. const ordering = query.sortDesc ? `-${sortBy}` : `${sortBy}`
  58. const url = `/projects?limit=${query.limit}&offset=${query.offset}&q=${query.q}&ordering=${ordering}`
  59. const response = await this.request.get(url)
  60. return new ProjectItemList(
  61. response.data.count,
  62. response.data.next,
  63. response.data.previous,
  64. response.data.results.map((project: { [key: string]: any }) => toModel(project))
  65. )
  66. }
  67. async findById(id: string): Promise<ProjectReadItem> {
  68. const url = `/projects/${id}`
  69. const response = await this.request.get(url)
  70. return toModel(response.data)
  71. }
  72. async create(item: ProjectWriteItem): Promise<ProjectReadItem> {
  73. const url = `/projects`
  74. const payload = toPayload(item)
  75. const response = await this.request.post(url, payload)
  76. return toModel(response.data)
  77. }
  78. async update(item: ProjectWriteItem): Promise<void> {
  79. const url = `/projects/${item.id}`
  80. const payload = toPayload(item)
  81. await this.request.patch(url, payload)
  82. }
  83. async bulkDelete(projectIds: number[]): Promise<void> {
  84. const url = `/projects`
  85. await this.request.delete(url, { ids: projectIds })
  86. }
  87. }