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.

49 lines
1.9 KiB

2 years ago
2 years ago
2 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(private readonly request = ApiService) {}
  7. async listAll(
  8. projectId: string,
  9. { limit = '10', offset = '0', q = '' }: SearchOption
  10. ): Promise<CommentItemList> {
  11. const url = `/projects/${projectId}/comments?q=${q}&limit=${limit}&offset=${offset}`
  12. const response = await this.request.get(url)
  13. return plainToInstance(CommentItemList, response.data)
  14. }
  15. async list(projectId: string, exampleId: number): Promise<CommentItem[]> {
  16. const url = `/projects/${projectId}/comments?example=${exampleId}&limit=100`
  17. const response = await this.request.get(url)
  18. return response.data.results.map((item: any) => plainToInstance(CommentItem, item))
  19. }
  20. async create(projectId: string, exampleId: number, text: string): Promise<CommentItem> {
  21. const url = `/projects/${projectId}/comments?example=${exampleId}`
  22. const response = await this.request.post(url, {
  23. projectId,
  24. exampleId,
  25. text
  26. })
  27. return plainToInstance(CommentItem, response.data)
  28. }
  29. async update(projectId: string, item: CommentItem): Promise<CommentItem> {
  30. const url = `/projects/${projectId}/comments/${item.id}`
  31. const response = await this.request.put(url, item.toObject())
  32. return plainToInstance(CommentItem, response.data)
  33. }
  34. async delete(projectId: string, commentId: number): Promise<void> {
  35. const url = `/projects/${projectId}/comments/${commentId}`
  36. await this.request.delete(url)
  37. }
  38. async deleteBulk(projectId: string, items: number[]): Promise<void> {
  39. const url = `/projects/${projectId}/comments`
  40. await this.request.delete(url, { ids: items })
  41. }
  42. }