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.

73 lines
2.4 KiB

  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(
  7. private readonly repository: LabelRepository
  8. ) {}
  9. public async list(id: string): Promise<LabelDTO[]> {
  10. const items = await this.repository.list(id)
  11. return items.map(item => new LabelDTO(item))
  12. }
  13. public async findById(projectId: string, labelId: number): Promise<LabelDTO> {
  14. const item = await this.repository.findById(projectId, labelId)
  15. return new LabelDTO(item)
  16. }
  17. public async create(projectId: string, item: CreateLabelCommand): Promise<LabelDTO> {
  18. // Todo: use auto mapping.
  19. const label = new LabelItem()
  20. label.text = item.text
  21. label.prefixKey = item.prefixKey
  22. label.suffixKey = item.suffixKey
  23. label.backgroundColor = item.backgroundColor
  24. label.textColor = item.textColor
  25. const created = await this.repository.create(projectId, label)
  26. return new LabelDTO(created)
  27. }
  28. public async update(projectId: string, item: LabelDTO): Promise<LabelDTO> {
  29. // Todo: use auto mapping.
  30. const label = new LabelItem()
  31. label.id = item.id
  32. label.text = item.text
  33. label.prefixKey = item.prefixKey
  34. label.suffixKey = item.suffixKey
  35. label.backgroundColor = item.backgroundColor
  36. label.textColor = item.textColor
  37. const updated = await this.repository.update(projectId, label)
  38. return new LabelDTO(updated)
  39. }
  40. public bulkDelete(projectId: string, items: LabelDTO[]): Promise<void> {
  41. const ids = items.map(item => item.id)
  42. return this.repository.bulkDelete(projectId, ids)
  43. }
  44. public async export(projectId: string) {
  45. const items = await this.repository.list(projectId)
  46. const labels = items.map(item => new LabelDTO(item))
  47. const url = window.URL.createObjectURL(new Blob([JSON.stringify(labels, null, 2)]))
  48. const link = document.createElement('a')
  49. link.href = url
  50. link.setAttribute('download', `label_config.json`)
  51. document.body.appendChild(link)
  52. link.click()
  53. }
  54. async upload(projectId: string, file: File) {
  55. const formData = new FormData()
  56. formData.append('file', file)
  57. const config = {
  58. headers: {
  59. 'Content-Type': 'multipart/form-data'
  60. }
  61. }
  62. await this.repository.uploadFile(projectId, formData)
  63. }
  64. }