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.

58 lines
2.0 KiB

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