invite.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package test
  2. import (
  3. "errors"
  4. "github.com/porter-dev/porter/internal/models"
  5. "github.com/porter-dev/porter/internal/repository"
  6. "gorm.io/gorm"
  7. )
  8. // InviteRepository uses gorm.DB for querying the database
  9. type InviteRepository struct {
  10. canQuery bool
  11. invites []*models.Invite
  12. }
  13. // NewInviteRepository returns a InviteRepository which uses
  14. // gorm.DB for querying the database
  15. func NewInviteRepository(canQuery bool) repository.InviteRepository {
  16. return &InviteRepository{canQuery, []*models.Invite{}}
  17. }
  18. // CreateInvite creates a new invite
  19. func (repo *InviteRepository) CreateInvite(invite *models.Invite) (*models.Invite, error) {
  20. if !repo.canQuery {
  21. return nil, errors.New("Cannot write database")
  22. }
  23. repo.invites = append(repo.invites, invite)
  24. invite.ID = uint(len(repo.invites))
  25. return invite, nil
  26. }
  27. // ReadInvite gets an invite specified by a unique id
  28. func (repo *InviteRepository) ReadInvite(projectID, id uint) (*models.Invite, error) {
  29. if !repo.canQuery {
  30. return nil, errors.New("Cannot read from database")
  31. }
  32. if int(id-1) >= len(repo.invites) || repo.invites[id-1] == nil {
  33. return nil, gorm.ErrRecordNotFound
  34. }
  35. index := int(id - 1)
  36. return repo.invites[index], nil
  37. }
  38. // ReadInviteByToken gets an invite specified by a unique token
  39. func (repo *InviteRepository) ReadInviteByToken(token string) (*models.Invite, error) {
  40. if !repo.canQuery {
  41. return nil, errors.New("Cannot read from database")
  42. }
  43. var res *models.Invite
  44. for _, invite := range repo.invites {
  45. if token == invite.Token {
  46. res = invite
  47. }
  48. }
  49. if res == nil {
  50. return nil, gorm.ErrRecordNotFound
  51. }
  52. return res, nil
  53. }
  54. // ListInvitesByProjectID finds all invites
  55. // for a given project id
  56. func (repo *InviteRepository) ListInvitesByProjectID(
  57. projectID uint,
  58. ) ([]*models.Invite, error) {
  59. if !repo.canQuery {
  60. return nil, errors.New("Cannot read from database")
  61. }
  62. res := make([]*models.Invite, 0)
  63. for _, invite := range repo.invites {
  64. if invite != nil && invite.ProjectID == projectID {
  65. res = append(res, invite)
  66. }
  67. }
  68. return res, nil
  69. }
  70. // ListInvitesByEmail finds all invites
  71. // for a given email
  72. func (repo *InviteRepository) ListInvitesByEmail(
  73. email string,
  74. ) ([]*models.Invite, error) {
  75. return nil, errors.New("Cannot read from database")
  76. }
  77. // UpdateInvite updates an invitation in the DB
  78. func (repo *InviteRepository) UpdateInvite(
  79. invite *models.Invite,
  80. ) (*models.Invite, error) {
  81. if !repo.canQuery {
  82. return nil, errors.New("Cannot write database")
  83. }
  84. if int(invite.ID-1) >= len(repo.invites) || repo.invites[invite.ID-1] == nil {
  85. return nil, gorm.ErrRecordNotFound
  86. }
  87. index := int(invite.ID - 1)
  88. repo.invites[index] = invite
  89. return invite, nil
  90. }
  91. // DeleteInvite removes a registry from the db
  92. func (repo *InviteRepository) DeleteInvite(
  93. invite *models.Invite,
  94. ) error {
  95. if !repo.canQuery {
  96. return errors.New("Cannot write database")
  97. }
  98. if int(invite.ID-1) >= len(repo.invites) || repo.invites[invite.ID-1] == nil {
  99. return gorm.ErrRecordNotFound
  100. }
  101. index := int(invite.ID - 1)
  102. repo.invites[index] = nil
  103. return nil
  104. }