-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_repository.go
123 lines (103 loc) · 2.6 KB
/
sample_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
118
119
120
121
122
123
package examples
import (
"context"
"errors"
"strconv"
"github.com/deluan/rest"
)
// ***********************************
// * Sample Repository and Model
// ***********************************
type ReadWriteRepository interface {
rest.Repository
rest.Persistable
}
// NewSampleRepository returns a new SampleRepository
func NewSampleRepository(ctx context.Context, logger ...rest.Logger) *SampleRepository {
repo := SampleRepository{Context: ctx}
repo.data = make(map[string]SampleModel)
return &repo
}
type SampleModel struct {
ID string
Name string
Age int
}
// SampleRepository is a simple in-memory repository implementation. NOTE: This repository does not handle QueryOptions
type SampleRepository struct {
Context context.Context
Error error
data map[string]SampleModel
seq int64
}
func (r *SampleRepository) Count(options ...rest.QueryOptions) (int64, error) {
return int64(len(r.data)), r.Error
}
func (r *SampleRepository) Read(id string) (interface{}, error) {
if r.Error != nil {
return nil, r.Error
}
if data, ok := r.data[id]; ok {
return data, nil
}
return nil, rest.ErrNotFound
}
func (r *SampleRepository) ReadAll(options ...rest.QueryOptions) (interface{}, error) {
if r.Error != nil {
return nil, r.Error
}
dataSet := make([]SampleModel, 0)
for _, v := range r.data {
dataSet = append(dataSet, v)
}
return dataSet, nil
}
func (r *SampleRepository) EntityName() string {
return "sample"
}
func (r *SampleRepository) NewInstance() interface{} {
return &SampleModel{}
}
func NewPersistableSampleRepository(ctx context.Context, logger ...rest.Logger) *PersistableSampleRepository {
repo := PersistableSampleRepository{}
repo.Context = ctx
repo.data = make(map[string]SampleModel)
return &repo
}
type PersistableSampleRepository struct {
SampleRepository
}
func (r *PersistableSampleRepository) Save(entity interface{}) (string, error) {
if r.Error != nil {
return "", r.Error
}
rec := entity.(*SampleModel)
r.seq = r.seq + 1
rec.ID = strconv.FormatInt(r.seq, 10)
if _, ok := r.data[rec.ID]; ok {
return "", errors.New("record already exists")
}
r.data[rec.ID] = *rec
return rec.ID, nil
}
func (r *PersistableSampleRepository) Update(id string, entity interface{}, cols ...string) error {
if r.Error != nil {
return r.Error
}
rec := entity.(*SampleModel)
if _, ok := r.data[rec.ID]; !ok {
return rest.ErrNotFound
}
r.data[rec.ID] = *rec
return nil
}
func (r *PersistableSampleRepository) Delete(id string) error {
if r.Error != nil {
return r.Error
}
if _, ok := r.data[id]; !ok {
return rest.ErrNotFound
}
delete(r.data, id)
return nil
}