-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
accounts.go
169 lines (146 loc) · 4.1 KB
/
accounts.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package admin
import (
"context"
"errors"
"net/http"
"strconv"
"github.com/dhax/go-base/auth/pwdless"
"github.com/dhax/go-base/database"
validation "github.com/go-ozzo/ozzo-validation"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)
// The list of error types returned from account resource.
var (
ErrAccountValidation = errors.New("account validation error")
)
// AccountStore defines database operations for account management.
type AccountStore interface {
List(*database.AccountFilter) ([]pwdless.Account, int, error)
Create(*pwdless.Account) error
Get(id int) (*pwdless.Account, error)
Update(*pwdless.Account) error
Delete(*pwdless.Account) error
}
// AccountResource implements account management handler.
type AccountResource struct {
Store AccountStore
}
// NewAccountResource creates and returns an account resource.
func NewAccountResource(store AccountStore) *AccountResource {
return &AccountResource{
Store: store,
}
}
func (rs *AccountResource) router() *chi.Mux {
r := chi.NewRouter()
r.Get("/", rs.list)
r.Post("/", rs.create)
r.Route("/{accountID}", func(r chi.Router) {
r.Use(rs.accountCtx)
r.Get("/", rs.get)
r.Put("/", rs.update)
r.Delete("/", rs.delete)
})
return r
}
func (rs *AccountResource) accountCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "accountID"))
if err != nil {
render.Render(w, r, ErrBadRequest)
return
}
account, err := rs.Store.Get(id)
if err != nil {
render.Render(w, r, ErrNotFound)
return
}
ctx := context.WithValue(r.Context(), ctxAccount, account)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
type accountRequest struct {
*pwdless.Account
}
func (d *accountRequest) Bind(r *http.Request) error {
return nil
}
type accountResponse struct {
*pwdless.Account
}
func newAccountResponse(a *pwdless.Account) *accountResponse {
resp := &accountResponse{Account: a}
return resp
}
type accountListResponse struct {
Accounts *[]pwdless.Account `json:"accounts"`
Count int `json:"count"`
}
func newAccountListResponse(a *[]pwdless.Account, count int) *accountListResponse {
resp := &accountListResponse{
Accounts: a,
Count: count,
}
return resp
}
func (rs *AccountResource) list(w http.ResponseWriter, r *http.Request) {
f, err := database.NewAccountFilter(r.URL.Query())
if err != nil {
render.Render(w, r, ErrRender(err))
return
}
al, count, err := rs.Store.List(f)
if err != nil {
render.Render(w, r, ErrRender(err))
return
}
render.Respond(w, r, newAccountListResponse(&al, count))
}
func (rs *AccountResource) create(w http.ResponseWriter, r *http.Request) {
data := &accountRequest{}
if err := render.Bind(r, data); err != nil {
render.Render(w, r, ErrInvalidRequest(err))
return
}
if err := rs.Store.Create(data.Account); err != nil {
switch err := err.(type) {
case validation.Errors:
render.Render(w, r, ErrValidation(ErrAccountValidation, err))
return
}
render.Render(w, r, ErrInvalidRequest(err))
return
}
render.Respond(w, r, newAccountResponse(data.Account))
}
func (rs *AccountResource) get(w http.ResponseWriter, r *http.Request) {
acc := r.Context().Value(ctxAccount).(*pwdless.Account)
render.Respond(w, r, newAccountResponse(acc))
}
func (rs *AccountResource) update(w http.ResponseWriter, r *http.Request) {
acc := r.Context().Value(ctxAccount).(*pwdless.Account)
data := &accountRequest{Account: acc}
if err := render.Bind(r, data); err != nil {
render.Render(w, r, ErrInvalidRequest(err))
return
}
if err := rs.Store.Update(acc); err != nil {
switch err := err.(type) {
case validation.Errors:
render.Render(w, r, ErrValidation(ErrAccountValidation, err))
return
}
render.Render(w, r, ErrInvalidRequest(err))
return
}
render.Respond(w, r, newAccountResponse(acc))
}
func (rs *AccountResource) delete(w http.ResponseWriter, r *http.Request) {
acc := r.Context().Value(ctxAccount).(*pwdless.Account)
if err := rs.Store.Delete(acc); err != nil {
render.Render(w, r, ErrInvalidRequest(err))
return
}
render.Respond(w, r, http.NoBody)
}