-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
43 lines (36 loc) · 1.12 KB
/
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
package calendar
import (
"sync"
"time"
)
// Repository is a storage interface which can be implemented by multiple backend
// (in-memory map, sql database, in-memory cache, file system, ...)
// It allows standard CRUD operation on calendars
type Repository interface {
Get(id int64) (Calendar, bool, error)
Create(calendar Calendar) (int64, error)
Update(calendar Calendar) error
Delete(id int64) error
GetAll() (map[int64]Calendar, error)
GetAllModifiedFrom(from time.Time) (map[int64]Calendar, error)
GetSituationCalendar(id int64) (Calendar, bool, error)
}
var (
_globalRepositoryMu sync.RWMutex
_globalRepository Repository
)
// R is used to access the global repository singleton
func R() Repository {
_globalRepositoryMu.RLock()
defer _globalRepositoryMu.RUnlock()
repository := _globalRepository
return repository
}
// ReplaceGlobals affect a new repository to the global repository singleton
func ReplaceGlobals(repository Repository) func() {
_globalRepositoryMu.Lock()
defer _globalRepositoryMu.Unlock()
prev := _globalRepository
_globalRepository = repository
return func() { ReplaceGlobals(prev) }
}