2
0

invite.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // UpdateInvite updates an invitation in the DB
  71. func (repo *InviteRepository) UpdateInvite(
  72. invite *models.Invite,
  73. ) (*models.Invite, error) {
  74. if !repo.canQuery {
  75. return nil, errors.New("Cannot write database")
  76. }
  77. if int(invite.ID-1) >= len(repo.invites) || repo.invites[invite.ID-1] == nil {
  78. return nil, gorm.ErrRecordNotFound
  79. }
  80. index := int(invite.ID - 1)
  81. repo.invites[index] = invite
  82. return invite, nil
  83. }
  84. // DeleteInvite removes a registry from the db
  85. func (repo *InviteRepository) DeleteInvite(
  86. invite *models.Invite,
  87. ) error {
  88. if !repo.canQuery {
  89. return errors.New("Cannot write database")
  90. }
  91. if int(invite.ID-1) >= len(repo.invites) || repo.invites[invite.ID-1] == nil {
  92. return gorm.ErrRecordNotFound
  93. }
  94. index := int(invite.ID - 1)
  95. repo.invites[index] = nil
  96. return nil
  97. }