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.3 KiB

2 years ago
2 years ago
2 years ago
  1. import ApiService from '@/services/api.service'
  2. import { CommentRepository, SearchOption } from '@/domain/models/comment/commentRepository'
  3. import { CommentItem, CommentItemList } from '@/domain/models/comment/comment'
  4. function toModel(item: { [key: string]: any }): CommentItem {
  5. return new CommentItem(
  6. item.id,
  7. item.user,
  8. item.username,
  9. item.example,
  10. item.text,
  11. item.created_at
  12. )
  13. }
  14. function toPayload(item: CommentItem): { [key: string]: any } {
  15. return {
  16. id: item.id,
  17. user: item.user,
  18. text: item.text
  19. }
  20. }
  21. export class APICommentRepository implements CommentRepository {
  22. constructor(private readonly request = ApiService) {}
  23. async listAll(
  24. projectId: string,
  25. { limit = '10', offset = '0', q = '' }: SearchOption
  26. ): Promise<CommentItemList> {
  27. const url = `/projects/${projectId}/comments?q=${q}&limit=${limit}&offset=${offset}`
  28. const response = await this.request.get(url)
  29. return new CommentItemList(
  30. response.data.count,
  31. response.data.next,
  32. response.data.previous,
  33. response.data.results.map((item: { [key: string]: any }) => toModel(item))
  34. )
  35. }
  36. async list(projectId: string, exampleId: number): Promise<CommentItem[]> {
  37. const url = `/projects/${projectId}/comments?example=${exampleId}&limit=100`
  38. const response = await this.request.get(url)
  39. return response.data.results.map((item: { [key: string]: any }) => toModel(item))
  40. }
  41. async create(projectId: string, exampleId: number, text: string): Promise<CommentItem> {
  42. const url = `/projects/${projectId}/comments?example=${exampleId}`
  43. const response = await this.request.post(url, {
  44. projectId,
  45. exampleId,
  46. text
  47. })
  48. return toModel(response.data)
  49. }
  50. async update(projectId: string, item: CommentItem): Promise<CommentItem> {
  51. const url = `/projects/${projectId}/comments/${item.id}`
  52. const payload = toPayload(item)
  53. const response = await this.request.put(url, payload)
  54. return toModel(response.data)
  55. }
  56. async delete(projectId: string, commentId: number): Promise<void> {
  57. const url = `/projects/${projectId}/comments/${commentId}`
  58. await this.request.delete(url)
  59. }
  60. async deleteBulk(projectId: string, items: number[]): Promise<void> {
  61. const url = `/projects/${projectId}/comments`
  62. await this.request.delete(url, { ids: items })
  63. }
  64. }