-
Notifications
You must be signed in to change notification settings - Fork 1
/
space_repository.go
117 lines (98 loc) · 2.61 KB
/
space_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package stream
import (
"context"
"errors"
"fmt"
"sync"
"github.com/google/uuid"
"gorm.io/gorm"
)
var ErrSpaceNotFound = errors.New("reading unknown space in store")
type SpaceRepository struct {
locker *sync.RWMutex
store storage
}
func NewSpaceRepository(store storage) *SpaceRepository {
return &SpaceRepository{
&sync.RWMutex{},
store,
}
}
func (r *SpaceRepository) Add(ctx context.Context, space *Space) (string, error) {
r.locker.Lock()
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
r.locker.Unlock()
cancel()
}()
if len(space.Identifier) == 0 {
space.Identifier = uuid.NewString()
}
result := tx.Create(space)
if result.Error != nil || result.RowsAffected != 1 {
return "", fmt.Errorf("adding live stream: %w", result.Error)
}
return space.Identifier, nil
}
func (r *SpaceRepository) GetByIdentifier(ctx context.Context, identifier string) (*Space, error) {
r.locker.Lock()
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
r.locker.Unlock()
cancel()
}()
var space Space
result := tx.Where("identifier = ?", identifier).First(&space)
if result.Error != nil {
err := fmt.Errorf("finding space by space identifier %s: %w", identifier, result.Error)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, errors.Join(err, ErrSpaceNotFound)
}
return nil, err
}
return &space, nil
}
func (r *SpaceRepository) CreateWithIdentifier(ctx context.Context, identifier string) (*Space, error) {
r.locker.Lock()
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
r.locker.Unlock()
cancel()
}()
space := &Space{Identifier: identifier}
result := tx.Create(space)
if result.Error != nil {
return nil, fmt.Errorf("creating space for name %s: %w", space.Identifier, result.Error)
}
return space, nil
}
func (r *SpaceRepository) Delete(ctx context.Context, identifier string) error {
r.locker.Lock()
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
r.locker.Unlock()
cancel()
}()
result := tx.Where("identifier = ?", identifier).Delete(&Space{})
if result.Error != nil {
return fmt.Errorf("deleting space by space id %s: %w", identifier, result.Error)
}
return nil
}
func (r *SpaceRepository) Len(ctx context.Context) int64 {
r.locker.RLock()
tx, cancel := r.getStoreWithContext(ctx)
defer func() {
r.locker.RUnlock()
cancel()
}()
var count int64
tx.Model(&Space{}).Count(&count)
return count
}
func (r *SpaceRepository) getStoreWithContext(ctx context.Context) (*gorm.DB, context.CancelFunc) {
ctx, cancel := context.WithTimeout(ctx, queryTimeOut)
db := r.store.GetDatabase()
tx := db.WithContext(ctx)
return tx, cancel
}