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.

60 lines
2.2 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import ApiService from '@/services/api.service'
  2. export abstract class AnnotationRepository<T> {
  3. labelName = 'dummy'
  4. constructor(readonly request = ApiService) {}
  5. public async list(projectId: string, exampleId: number): Promise<T[]> {
  6. const url = this.baseUrl(projectId, exampleId)
  7. const response = await this.request.get(url)
  8. return response.data.map((item: { [key: string]: any }) => this.toModel(item))
  9. }
  10. public async find(projectId: string, exampleId: number, labelId: number): Promise<T> {
  11. const url = `${this.baseUrl(projectId, exampleId)}/${labelId}`
  12. const response = await this.request.get(url)
  13. return this.toModel(response.data)
  14. }
  15. public async create(projectId: string, exampleId: number, item: T): Promise<void> {
  16. const url = this.baseUrl(projectId, exampleId)
  17. const payload = this.toPayload(item)
  18. await this.request.post(url, payload)
  19. }
  20. public async update(projectId: string, exampleId: number, labelId: number, item: T): Promise<T> {
  21. const url = `${this.baseUrl(projectId, exampleId)}/${labelId}`
  22. const payload = this.toPayload(item)
  23. const response = await this.request.patch(url, payload)
  24. return this.toModel(response.data)
  25. }
  26. public async delete(projectId: string, exampleId: number, labelId: number): Promise<void> {
  27. const url = `${this.baseUrl(projectId, exampleId)}/${labelId}`
  28. await this.request.delete(url)
  29. }
  30. public async clear(projectId: string, exampleId: number): Promise<void> {
  31. const url = this.baseUrl(projectId, exampleId)
  32. await this.request.delete(url)
  33. }
  34. async bulkDelete(projectId: string, exampleId: number, ids: number[]): Promise<void> {
  35. const url = `${this.baseUrl(projectId, exampleId)}/${this.labelName}`
  36. await this.request.delete(url, { ids })
  37. }
  38. public async autoLabel(projectId: string, exampleId: number): Promise<void> {
  39. const url = `/projects/${projectId}/auto-labeling?example=${exampleId}`
  40. await this.request.post(url, {})
  41. }
  42. protected baseUrl(projectId: string, exampleId: number): string {
  43. return `/projects/${projectId}/examples/${exampleId}/${this.labelName}`
  44. }
  45. protected abstract toModel(item: { [key: string]: any }): T
  46. protected abstract toPayload(item: T): { [key: string]: any }
  47. }