invite.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package store
  2. import (
  3. "context"
  4. "fmt"
  5. "git.linuxforward.com/byom/byom-core/common"
  6. "gorm.io/gorm"
  7. )
  8. //CRUD methods for the invite
  9. // CreateInvite creates a new invite
  10. func (s *DataStore) CreateInvite(ctx context.Context, i *common.Invite) error {
  11. result := s.db.Create(i)
  12. if result.Error != nil {
  13. return fmt.Errorf("create invite: %w", result.Error)
  14. }
  15. return nil
  16. }
  17. // GetInvite gets an invite by ID
  18. func (s *DataStore) GetInvite(ctx context.Context, id string) (*common.Invite, error) {
  19. var invite common.Invite
  20. result := s.db.First(&invite, id)
  21. if result.Error != nil {
  22. return nil, fmt.Errorf("get invite: %w", result.Error)
  23. }
  24. return &invite, nil
  25. }
  26. // UpdateInvite updates an invite
  27. func (s *DataStore) UpdateInvite(ctx context.Context, i *common.Invite) error {
  28. result := s.db.Save(i)
  29. if result.Error != nil {
  30. return fmt.Errorf("update invite: %w", result.Error)
  31. }
  32. return nil
  33. }
  34. // DeleteInvite deletes an invite
  35. func (s *DataStore) DeleteInvite(ctx context.Context, id string) error {
  36. result := s.db.Delete(&common.Invite{}, id)
  37. if result.Error != nil {
  38. return fmt.Errorf("delete invite: %w", result.Error)
  39. }
  40. return nil
  41. }
  42. // GetInvites gets all invites
  43. func (s *DataStore) GetInvites(ctx context.Context) ([]common.Invite, error) {
  44. var invites []common.Invite
  45. result := s.db.Find(&invites)
  46. if result.Error != nil {
  47. return nil, fmt.Errorf("get invites: %w", result.Error)
  48. }
  49. return invites, nil
  50. }
  51. // UpdateInviteStatusTx updates the status of an invite in a transaction
  52. func (s *DataStore) UpdateInviteStatusTx(ctx context.Context, tx *gorm.DB, i *common.Invite) error {
  53. result := tx.Model(&common.Invite{}).Where("token = ?", i.Token).Update("status", i.Status)
  54. if result.Error != nil {
  55. return fmt.Errorf("update invite status: %w", result.Error)
  56. }
  57. return nil
  58. }