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.

68 lines
2.2 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 findById(projectId: string, labelId: number): Promise<LabelItem> {
  24. const url = `/projects/${projectId}/${this.baseUrl}s/${labelId}`
  25. const response = await this.request.get(url)
  26. return plainToInstance(LabelItem, response.data)
  27. }
  28. async create(projectId: string, item: LabelItem): Promise<LabelItem> {
  29. const url = `/projects/${projectId}/${this.baseUrl}s`
  30. const response = await this.request.post(url, item.toObject())
  31. return plainToInstance(LabelItem, response.data)
  32. }
  33. async update(projectId: string, item: LabelItem): Promise<LabelItem> {
  34. const url = `/projects/${projectId}/${this.baseUrl}s/${item.id}`
  35. const response = await this.request.patch(url, item.toObject())
  36. return plainToInstance(LabelItem, response.data)
  37. }
  38. async bulkDelete(projectId: string, labelIds: number[]): Promise<void> {
  39. const url = `/projects/${projectId}/${this.baseUrl}s`
  40. await this.request.delete(url, { ids: labelIds })
  41. }
  42. async uploadFile(projectId: string, payload: FormData): Promise<void> {
  43. const url = `/projects/${projectId}/${this.baseUrl}-upload`
  44. const config = {
  45. headers: {
  46. 'Content-Type': 'multipart/form-data'
  47. }
  48. }
  49. try {
  50. await this.request.post(url, payload, config)
  51. } catch(e: any) {
  52. const data = e.response.data
  53. if ('detail' in data) {
  54. throw new Error(data.detail)
  55. } else {
  56. throw new Error('Text field is required')
  57. }
  58. }
  59. }
  60. }