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.

45 lines
2.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. import { plainToInstance } from 'class-transformer'
  2. import ApiService from '@/services/api.service'
  3. import { CommentRepository, SearchOption } from '@/domain/models/comment/commentRepository'
  4. import { CommentItem, CommentItemList } from '~/domain/models/comment/comment'
  5. export class APICommentRepository implements CommentRepository {
  6. constructor(
  7. private readonly request = ApiService
  8. ) {}
  9. async listAll(projectId: string, { limit = '10', offset = '0', q = '' }: SearchOption): Promise<CommentItemList> {
  10. const url = `/projects/${projectId}/comments?q=${q}&limit=${limit}&offset=${offset}`
  11. const response = await this.request.get(url)
  12. return plainToInstance(CommentItemList, response.data)
  13. }
  14. async list(projectId: string, exampleId: number): Promise<CommentItem[]> {
  15. const url = `/projects/${projectId}/examples/${exampleId}/comments`
  16. const response = await this.request.get(url)
  17. return response.data.map((item: any) => plainToInstance(CommentItem, item))
  18. }
  19. async create(projectId: string, exampleId: number, text: string): Promise<CommentItem> {
  20. const url = `/projects/${projectId}/examples/${exampleId}/comments`
  21. const response = await this.request.post(url, { projectId, exampleId, text })
  22. return plainToInstance(CommentItem, response.data)
  23. }
  24. async update(projectId: string, exampleId: number, item: CommentItem): Promise<CommentItem> {
  25. const url = `/projects/${projectId}/examples/${exampleId}/comments/${item.id}`
  26. const response = await this.request.put(url, item.toObject())
  27. return plainToInstance(CommentItem, response.data)
  28. }
  29. async delete(projectId: string, exampleId: number, commentId: number): Promise<void> {
  30. const url = `/projects/${projectId}/examples/${exampleId}/comments/${commentId}`
  31. const response = await this.request.delete(url)
  32. }
  33. async deleteBulk(projectId: string, items: number[]): Promise<void> {
  34. const url = `/projects/${projectId}/comments`
  35. await this.request.delete(url, { ids: items })
  36. }
  37. }