12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package store
- import (
- "context"
- "fmt"
- "git.linuxforward.com/byom/byom-core/common"
- "gorm.io/gorm"
- )
- //CRUD methods for the invite
- // CreateInvite creates a new invite
- func (s *DataStore) CreateInvite(ctx context.Context, i *common.Invite) error {
- result := s.db.Create(i)
- if result.Error != nil {
- return fmt.Errorf("create invite: %w", result.Error)
- }
- return nil
- }
- // GetInvite gets an invite by ID
- func (s *DataStore) GetInvite(ctx context.Context, id string) (*common.Invite, error) {
- var invite common.Invite
- result := s.db.First(&invite, id)
- if result.Error != nil {
- return nil, fmt.Errorf("get invite: %w", result.Error)
- }
- return &invite, nil
- }
- // UpdateInvite updates an invite
- func (s *DataStore) UpdateInvite(ctx context.Context, i *common.Invite) error {
- result := s.db.Save(i)
- if result.Error != nil {
- return fmt.Errorf("update invite: %w", result.Error)
- }
- return nil
- }
- // DeleteInvite deletes an invite
- func (s *DataStore) DeleteInvite(ctx context.Context, id string) error {
- result := s.db.Delete(&common.Invite{}, id)
- if result.Error != nil {
- return fmt.Errorf("delete invite: %w", result.Error)
- }
- return nil
- }
- // GetInvites gets all invites
- func (s *DataStore) GetInvites(ctx context.Context) ([]common.Invite, error) {
- var invites []common.Invite
- result := s.db.Find(&invites)
- if result.Error != nil {
- return nil, fmt.Errorf("get invites: %w", result.Error)
- }
- return invites, nil
- }
- // UpdateInviteStatusTx updates the status of an invite in a transaction
- func (s *DataStore) UpdateInviteStatusTx(ctx context.Context, tx *gorm.DB, i *common.Invite) error {
- result := tx.Model(&common.Invite{}).Where("token = ?", i.Token).Update("status", i.Status)
- if result.Error != nil {
- return fmt.Errorf("update invite status: %w", result.Error)
- }
- return nil
- }
|