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.

79 lines
2.5 KiB

2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. import ApiService from '@/services/api.service'
  2. import { ExampleRepository, SearchOption } from '@/domain/models/example/exampleRepository'
  3. import { ExampleItem, ExampleItemList } from '@/domain/models/example/example'
  4. function toModel(item: { [key: string]: any }): ExampleItem {
  5. return new ExampleItem(
  6. item.id,
  7. item.text,
  8. item.meta,
  9. item.annotation_approver,
  10. item.comment_count,
  11. item.filename,
  12. item.is_confirmed,
  13. item.upload_name
  14. )
  15. }
  16. function toPayload(item: ExampleItem): { [key: string]: any } {
  17. return {
  18. id: item.id,
  19. text: item.text,
  20. meta: item.meta,
  21. annotation_approver: item.annotationApprover,
  22. comment_count: item.commentCount
  23. }
  24. }
  25. export class APIExampleRepository implements ExampleRepository {
  26. constructor(private readonly request = ApiService) {}
  27. async list(
  28. projectId: string,
  29. { limit = '10', offset = '0', q = '', isChecked = '' }: SearchOption
  30. ): Promise<ExampleItemList> {
  31. const url = `/projects/${projectId}/examples?limit=${limit}&offset=${offset}&q=${q}&confirmed=${isChecked}`
  32. const response = await this.request.get(url)
  33. return new ExampleItemList(
  34. response.data.count,
  35. response.data.next,
  36. response.data.previous,
  37. response.data.results.map((item: { [key: string]: any }) => toModel(item))
  38. )
  39. }
  40. async create(projectId: string, item: ExampleItem): Promise<ExampleItem> {
  41. const url = `/projects/${projectId}/examples`
  42. const payload = toPayload(item)
  43. const response = await this.request.post(url, payload)
  44. return toModel(response.data)
  45. }
  46. async update(projectId: string, item: ExampleItem): Promise<ExampleItem> {
  47. const url = `/projects/${projectId}/examples/${item.id}`
  48. const payload = toPayload(item)
  49. const response = await this.request.patch(url, payload)
  50. return toModel(response.data)
  51. }
  52. async bulkDelete(projectId: string, ids: number[]): Promise<void> {
  53. const url = `/projects/${projectId}/examples`
  54. await this.request.delete(url, { ids })
  55. }
  56. async deleteAll(projectId: string): Promise<void> {
  57. const url = `/projects/${projectId}/examples`
  58. await this.request.delete(url)
  59. }
  60. async findById(projectId: string, exampleId: number): Promise<ExampleItem> {
  61. const url = `/projects/${projectId}/examples/${exampleId}`
  62. const response = await this.request.get(url)
  63. return toModel(response.data)
  64. }
  65. async confirm(projectId: string, exampleId: number): Promise<void> {
  66. const url = `/projects/${projectId}/examples/${exampleId}/states`
  67. await this.request.post(url, {})
  68. }
  69. }