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.

52 lines
1.7 KiB

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