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.

52 lines
1.8 KiB

2 years ago
2 years ago
2 years ago
  1. import ApiService from '@/services/api.service'
  2. import { MemberRepository } from '@/domain/models/member/memberRepository'
  3. import { MemberItem } from '@/domain/models/member/member'
  4. function toModel(item: { [key: string]: any }): MemberItem {
  5. return new MemberItem(item.id, item.user, item.role, item.username, item.rolename)
  6. }
  7. function toPayload(item: MemberItem): { [key: string]: any } {
  8. return {
  9. id: item.id,
  10. user: item.user,
  11. role: item.role,
  12. username: item.username,
  13. rolename: item.rolename
  14. }
  15. }
  16. export class APIMemberRepository implements MemberRepository {
  17. constructor(private readonly request = ApiService) {}
  18. async list(projectId: string): Promise<MemberItem[]> {
  19. const url = `/projects/${projectId}/members`
  20. const response = await this.request.get(url)
  21. return response.data.map((item: { [key: string]: any }) => toModel(item))
  22. }
  23. async create(projectId: string, item: MemberItem): Promise<MemberItem> {
  24. const url = `/projects/${projectId}/members`
  25. const payload = toPayload(item)
  26. const response = await this.request.post(url, payload)
  27. return toModel(response.data)
  28. }
  29. async update(projectId: string, item: MemberItem): Promise<MemberItem> {
  30. const url = `/projects/${projectId}/members/${item.id}`
  31. const payload = toPayload(item)
  32. const response = await this.request.patch(url, payload)
  33. return toModel(response.data)
  34. }
  35. async bulkDelete(projectId: string, memberIds: number[]): Promise<void> {
  36. const url = `/projects/${projectId}/members`
  37. await this.request.delete(url, { ids: memberIds })
  38. }
  39. async fetchMyRole(projectId: string): Promise<MemberItem> {
  40. const url = `/projects/${projectId}/my-role`
  41. const response = await this.request.get(url)
  42. return toModel(response.data)
  43. }
  44. }