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.

63 lines
2.0 KiB

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