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.

62 lines
2.0 KiB

  1. import { plainToInstance } from 'class-transformer'
  2. import ApiService from '@/services/api.service'
  3. import { LabelRepository } from '~/domain/models/label/labelRepository'
  4. import { LabelItem } from '~/domain/models/label/label'
  5. export interface LabelItemResponse {
  6. id: number,
  7. text: string,
  8. prefix_key: string,
  9. suffix_key: string,
  10. background_color: string,
  11. text_color: string
  12. }
  13. export class APILabelRepository implements LabelRepository {
  14. constructor(
  15. private readonly baseUrl = 'label',
  16. private readonly request = ApiService
  17. ) {}
  18. async list(projectId: string): Promise<LabelItem[]> {
  19. const url = `/projects/${projectId}/${this.baseUrl}s`
  20. const response = await this.request.get(url)
  21. return response.data.map((item: any) => plainToInstance(LabelItem, item))
  22. }
  23. async create(projectId: string, item: LabelItem): Promise<LabelItem> {
  24. const url = `/projects/${projectId}/${this.baseUrl}s`
  25. const response = await this.request.post(url, item.toObject())
  26. return plainToInstance(LabelItem, response.data)
  27. }
  28. async update(projectId: string, item: LabelItem): Promise<LabelItem> {
  29. const url = `/projects/${projectId}/${this.baseUrl}s/${item.id}`
  30. const response = await this.request.patch(url, item.toObject())
  31. return plainToInstance(LabelItem, response.data)
  32. }
  33. async bulkDelete(projectId: string, labelIds: number[]): Promise<void> {
  34. const url = `/projects/${projectId}/${this.baseUrl}s`
  35. await this.request.delete(url, { ids: labelIds })
  36. }
  37. async uploadFile(projectId: string, payload: FormData): Promise<void> {
  38. const url = `/projects/${projectId}/${this.baseUrl}-upload`
  39. const config = {
  40. headers: {
  41. 'Content-Type': 'multipart/form-data'
  42. }
  43. }
  44. try {
  45. await this.request.post(url, payload, config)
  46. } catch(e: any) {
  47. const data = e.response.data
  48. if ('detail' in data) {
  49. throw new Error(data.detail)
  50. } else {
  51. throw new Error('Text field is required')
  52. }
  53. }
  54. }
  55. }