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.

65 lines
2.2 KiB

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