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.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. import { LabelItem } from '@/models/label'
  2. import { LabelItemListRepository } from '@/repositories/label/interface'
  3. export class LabelDTO {
  4. id: number
  5. text: string
  6. prefixKey: string | null
  7. suffixKey: string | null
  8. backgroundColor: string
  9. textColor: string
  10. constructor(item: LabelItem) {
  11. this.id = item.id
  12. this.text = item.text
  13. this.prefixKey = item.prefixKey
  14. this.suffixKey = item.suffixKey
  15. this.backgroundColor = item.backgroundColor
  16. this.textColor = '#ffffff'
  17. }
  18. }
  19. export class LabelApplicationService {
  20. constructor(
  21. private readonly repository: LabelItemListRepository
  22. ) {}
  23. public async list(id: string): Promise<LabelDTO[]> {
  24. const items = await this.repository.list(id)
  25. return items.map(item => new LabelDTO(item))
  26. }
  27. public create(projectId: string, item: LabelDTO): void {
  28. const label = new LabelItem(0, item.text, item.prefixKey, item.suffixKey, item.backgroundColor, item.textColor)
  29. this.repository.create(projectId, label)
  30. }
  31. public update(projectId: string, item: LabelDTO): void {
  32. const label = new LabelItem(item.id, item.text, item.prefixKey, item.suffixKey, item.backgroundColor, item.textColor)
  33. this.repository.update(projectId, label)
  34. }
  35. public bulkDelete(projectId: string, items: LabelDTO[]): Promise<void> {
  36. const ids = items.map(item => item.id)
  37. return this.repository.bulkDelete(projectId, ids)
  38. }
  39. public async export(projectId: string) {
  40. const items = await this.repository.list(projectId)
  41. const labels = items.map(item => new LabelDTO(item))
  42. const url = window.URL.createObjectURL(new Blob([JSON.stringify(labels, null, 2)]))
  43. const link = document.createElement('a')
  44. link.href = url
  45. link.setAttribute('download', `label_config.json`)
  46. document.body.appendChild(link)
  47. link.click()
  48. }
  49. async upload(projectId: string, file: File) {
  50. const formData = new FormData()
  51. formData.append('file', file)
  52. const config = {
  53. headers: {
  54. 'Content-Type': 'multipart/form-data'
  55. }
  56. }
  57. await this.repository.uploadFile(projectId, formData)
  58. }
  59. }