-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
155 lines (136 loc) · 2.97 KB
/
manager.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package mock
import (
"context"
"errors"
"fmt"
"io"
"github.com/induzo/crud"
"github.com/induzo/gohttperror"
"github.com/rs/xid"
)
var (
// ErrNotFound is used for Get and GetList when no entity is found
ErrNotFound = errors.New("entity not found")
// ErrBadRequest is used when the request is malformed or wrong
ErrBadRequest = errors.New("bad request")
// ErrForbidden is used when the request is malformed or wrong
ErrForbidden = errors.New("forbidden")
)
// Mgr is a mock for the mgr interface
type Mgr struct {
WantCreateError bool
WantDeleteError bool
WantGetError bool
WantGetListError bool
WantUpdateError bool
WantPartialUpdateError bool
EntityList map[xid.ID]*Entity
}
func NewMgr() *Mgr {
return &Mgr{
EntityList: make(map[xid.ID]*Entity),
}
}
func (m *Mgr) NewEmptyEntity() interface{} {
return &Entity{}
}
func (m *Mgr) Create(
ctx context.Context,
e interface{},
pl io.Reader,
) (interface{}, error) {
if m.WantCreateError {
return nil, fmt.Errorf("Error create")
}
ec, ok := e.(*Entity)
if !ok {
return nil,
fmt.Errorf("Mgr Create: impossible to cast e to Entity")
}
ec.ID = xid.New()
m.EntityList[ec.ID] = ec
return ec, nil
}
func (m *Mgr) Delete(ctx context.Context, id xid.ID) error {
if m.WantDeleteError {
return fmt.Errorf("Error delete")
}
if _, ok := m.EntityList[id]; !ok {
return ErrNotFound
}
delete(m.EntityList, id)
return nil
}
func (m *Mgr) Get(ctx context.Context, id xid.ID) (interface{}, error) {
if m.WantGetError {
return nil, fmt.Errorf("Error get")
}
if ent, ok := m.EntityList[id]; ok {
return ent, nil
}
return nil, ErrNotFound
}
func (m *Mgr) GetList(
context.Context,
crud.ListModifiers,
) (interface{}, error) {
if m.WantGetListError {
return nil, fmt.Errorf("Error getlist")
}
if len(m.EntityList) == 0 {
return nil, ErrNotFound
}
v := make([]*Entity, 0, len(m.EntityList))
for _, value := range m.EntityList {
v = append(v, value)
}
return v, nil
}
func (m *Mgr) Update(
ctx context.Context,
id xid.ID,
newE interface{},
pl io.Reader,
) (interface{}, error) {
if m.WantUpdateError {
return nil, fmt.Errorf("Error update")
}
_, ok := m.EntityList[id]
if !ok {
return nil, ErrNotFound
}
newEC, okC := newE.(*Entity)
if !okC {
return ErrBadRequest, nil
}
m.EntityList[id] = newEC
return newEC, nil
}
func (m *Mgr) PartialUpdate(
ctx context.Context,
id xid.ID,
pud crud.PartialUpdateData,
pl io.Reader,
) error {
if m.WantPartialUpdateError {
return fmt.Errorf("Error partial update")
}
if _, ok := pud["status_id"]; !ok {
return ErrBadRequest
}
if _, ok := m.EntityList[id]; !ok {
return ErrNotFound
}
m.EntityList[id].StatusID = int(pud["status_id"].(float64))
return nil
}
func (m *Mgr) MapErrorToHTTPError(e error) *gohttperror.ErrResponse {
switch e {
case ErrNotFound:
return gohttperror.ErrNotFound
case ErrForbidden:
return gohttperror.ErrForbidden(e)
default:
return gohttperror.ErrInternal(e)
}
}