/
account.go
400 lines (366 loc) · 12.5 KB
/
account.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package api
import (
"encoding/json"
"net/http"
"github.com/Edbeer/paymentapi/pkg/utils"
"github.com/Edbeer/paymentapi/types"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/opentracing/opentracing-go"
)
// createAccount godoc
// @Summary Create new account
// @Description register new account, returns account
// @Tags Account
// @Accept json
// @Produce json
// @Param input body types.RequestCreate true "create account info"
// @Success 200 {object} types.Account
// @Failure 400 {object} api.ApiError
// @Failure 404 {object} api.ApiError
// @Failure 500 {object} api.ApiError
// @Router /account [post]
func (s *JSONApiServer) createAccount(w http.ResponseWriter, r *http.Request) error {
span, ctx := opentracing.StartSpanFromContext(r.Context(), "Account.createAccount")
defer span.Finish()
req := &types.RequestCreate{}
if err := json.NewDecoder(r.Body).Decode(req); err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
defer r.Body.Close()
// validate request
if err := utils.ValidateCreateRequest(req); err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
account, err := s.storage.CreateAccount(ctx, req)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
// jwt-token
tokenString, err := utils.CreateJWT(account)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
w.Header().Add("x-jwt-token", tokenString)
// refreshToken
refreshToken, err := s.redisStorage.CreateSession(ctx, &types.Session{
UserID: account.ID,
}, 86400)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: "error"})
}
// cookie
cookie := &http.Cookie{
Name: "refresh-token",
Value: refreshToken,
Path: "/",
RawExpires: "",
MaxAge: 86400,
Secure: false,
HttpOnly: true,
SameSite: 0,
}
http.SetCookie(w, cookie)
return WriteJSON(w, http.StatusOK, account)
}
// getAccount godoc
// @Summary Get all accounts
// @Description get all accounts, returns accounts
// @Tags Account
// @Produce json
// @Success 200 {object} []types.Account
// @Failure 400 {object} api.ApiError
// @Failure 404 {object} api.ApiError
// @Failure 500 {object} api.ApiError
// @Router /account [get]
func (s *JSONApiServer) getAccount(w http.ResponseWriter, r *http.Request) error {
span, ctx := opentracing.StartSpanFromContext(r.Context(), "Account.getAccount")
defer span.Finish()
accounts, err := s.storage.GetAccount(ctx)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
return WriteJSON(w, http.StatusOK, accounts)
}
// getAccountByID godoc
// @Summary Get account by id
// @Description get account by id, returns account
// @Tags Account
// @Produce json
// @Param id path string true "get account by id info"
// @Success 200 {object} types.Account
// @Failure 400 {object} api.ApiError
// @Failure 404 {object} api.ApiError
// @Failure 500 {object} api.ApiError
// @Router /account/{id} [get]
func (s *JSONApiServer) getAccountByID(w http.ResponseWriter, r *http.Request) error {
span, ctx := opentracing.StartSpanFromContext(r.Context(), "Account.createAccount")
defer span.Finish()
uuid, err := GetUUID(r)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
account, err := s.storage.GetAccountByID(ctx, uuid)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
return WriteJSON(w, http.StatusOK, account)
}
// updateAccount godoc
// @Summary Update account
// @Description update account, returns updated account
// @Tags Account
// @Accept json
// @Produce json
// @Param id path string true "update account info"
// @Param input body types.RequestUpdate true "update account info"
// @Success 200 {object} types.Account
// @Failure 400 {object} api.ApiError
// @Failure 404 {object} api.ApiError
// @Failure 500 {object} api.ApiError
// @Router /account/{id} [put]
func (s *JSONApiServer) updateAccount(w http.ResponseWriter, r *http.Request) error {
span, ctx := opentracing.StartSpanFromContext(r.Context(), "Account.updateAccount")
defer span.Finish()
uuid, err := GetUUID(r)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
reqUpd := &types.RequestUpdate{}
if err := json.NewDecoder(r.Body).Decode(reqUpd); err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
defer r.Body.Close()
// validate request
if err := utils.ValidateUpdateRequest(reqUpd); err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
account, err := s.storage.UpdateAccount(ctx, reqUpd, uuid)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
return WriteJSON(w, http.StatusOK, account)
}
// deleteAccount godoc
// @Summary Delete account
// @Description delete account, returns status
// @Tags Account
// @Produce json
// @Param id path string true "delete account info"
// @Success 200 {integer} 200
// @Failure 400 {object} api.ApiError
// @Failure 404 {object} api.ApiError
// @Failure 500 {object} api.ApiError
// @Router /account/{id} [delete]
func (s *JSONApiServer) deleteAccount(w http.ResponseWriter, r *http.Request) error {
span, ctx := opentracing.StartSpanFromContext(r.Context(), "Account.deleteAccount")
defer span.Finish()
uuid, err := GetUUID(r)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
if err := s.storage.DeleteAccount(ctx, uuid); err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
return WriteJSON(w, http.StatusOK, "account was deleted")
}
// depositAccount godoc
// @Summary Deposit money
// @Description deposit money to account, returns account
// @Tags Account
// @Accept json
// @Produce json
// @Param input body types.RequestDeposit true "deposit account info"
// @Success 200 {object} types.Account
// @Failure 400 {object} api.ApiError
// @Failure 404 {object} api.ApiError
// @Failure 500 {object} api.ApiError
// @Router /account/deposit [post]
func (s *JSONApiServer) depositAccount(w http.ResponseWriter, r *http.Request) error {
span, ctx := opentracing.StartSpanFromContext(r.Context(), "Account.depositAccount")
defer span.Finish()
reqDep := &types.RequestDeposit{}
if err := json.NewDecoder(r.Body).Decode(reqDep); err != nil {
return WriteJSON(w, http.StatusBadRequest, "account doesn't exist")
}
// validate request
if err := utils.ValidateDepositRequest(reqDep); err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
acc, err := s.storage.GetAccountByCard(ctx, reqDep.CardNumber)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, "account doesn't exist")
}
acc.Balance = acc.Balance + reqDep.Balance
reqDep.Balance = acc.Balance
updatedAccount, err := s.storage.DepositAccount(ctx, reqDep)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, "account doesn't exist")
}
return WriteJSON(w, http.StatusOK, updatedAccount)
}
// getStatement godoc
// @Summary Get account statement
// @Description get account statement, returns statement
// @Tags Account
// @Produce json
// @Param id path string true "get statement info"
// @Success 200 {object} types.Account.Statement
// @Failure 400 {object} api.ApiError
// @Failure 404 {object} api.ApiError
// @Failure 500 {object} api.ApiError
// @Router /account/statement/{id} [get]
func (s *JSONApiServer) getStatement(w http.ResponseWriter, r *http.Request) error {
span, ctx := opentracing.StartSpanFromContext(r.Context(), "Account.getStatement")
defer span.Finish()
uuid, err := GetUUID(r)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
statement, err := s.storage.GetAccountStatement(ctx, uuid)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
return WriteJSON(w, http.StatusOK, statement)
}
// signIn godoc
// @Summary Login
// @Description log in to your account, returns account
// @Tags Account
// @Accept json
// @Produce json
// @Param input body types.LoginRequest true "login account info"
// @Success 200 {object} types.Account
// @Failure 400 {object} api.ApiError
// @Failure 404 {object} api.ApiError
// @Failure 500 {object} api.ApiError
// @Router /account/sign-in [post]
func (s *JSONApiServer) signIn(w http.ResponseWriter, r *http.Request) error {
span, ctx := opentracing.StartSpanFromContext(r.Context(), "Account.signIn")
defer span.Finish()
req := &types.LoginRequest{}
if err := json.NewDecoder(r.Body).Decode(req); err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
account, err := s.storage.GetAccountByID(ctx, req.ID)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
// jwt-token
tokenString, err := utils.CreateJWT(account)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
w.Header().Add("x-jwt-token", tokenString)
// refreshToken
refreshToken, err := s.redisStorage.CreateSession(ctx, &types.Session{
UserID: account.ID,
}, 86400)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
// cookie
cookie := &http.Cookie{
Name: "refresh-token",
Value: refreshToken,
Path: "/",
RawExpires: "",
MaxAge: 86400,
Secure: false,
HttpOnly: true,
SameSite: 0,
}
http.SetCookie(w, cookie)
return WriteJSON(w, http.StatusOK, account)
}
// signOut godoc
// @Summary Logout
// @Description log out of your account, returns status
// @Tags Account
// @Produce json
// @Success 200 {integer} 200
// @Failure 400 {object} api.ApiError
// @Failure 404 {object} api.ApiError
// @Failure 500 {object} api.ApiError
// @Router /account/sign-out [post]
func (s *JSONApiServer) signOut(w http.ResponseWriter, r *http.Request) error {
span, ctx := opentracing.StartSpanFromContext(r.Context(), "Account.signOut")
defer span.Finish()
cookie, err := r.Cookie("refresh-token")
if err != nil {
if err == http.ErrNoCookie {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: "Cookie doesn't exist"})
}
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
if err := s.redisStorage.DeleteSession(ctx, cookie.Value); err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
return WriteJSON(w, http.StatusOK, "LogOut")
}
// refreshTokens godoc
// @Summary Refresh tokens
// @Description refresh access and refresh tokens, returns tokens
// @Tags Account
// @Accept json
// @Produce json
// @Param input body types.RefreshRequest true "refresh tokens account info"
// @Success 200 {object} types.RefreshResponse
// @Failure 400 {object} api.ApiError
// @Failure 404 {object} api.ApiError
// @Failure 500 {object} api.ApiError
// @Router /account/refresh [post]
func (s *JSONApiServer) refreshTokens(w http.ResponseWriter, r *http.Request) error {
span, ctx := opentracing.StartSpanFromContext(r.Context(), "Account.refreshTokens")
defer span.Finish()
req := &types.RefreshRequest{}
if err := json.NewDecoder(r.Body).Decode(req); err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
uid, err := s.redisStorage.GetUserID(ctx, req.RefreshToken)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
account, err := s.storage.GetAccountByID(ctx, uid)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
// jwt-token
tokenString, err := utils.CreateJWT(account)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
w.Header().Add("x-jwt-token", tokenString)
// refreshToken
refreshToken, err := s.redisStorage.CreateSession(ctx, &types.Session{
UserID: account.ID,
}, 86400)
if err != nil {
return WriteJSON(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
// cookie
cookie := &http.Cookie{
Name: "refresh-token",
Value: refreshToken,
Path: "/",
RawExpires: "",
MaxAge: 86400,
Secure: false,
HttpOnly: true,
SameSite: 0,
}
http.SetCookie(w, cookie)
return WriteJSON(w, http.StatusOK, &types.RefreshResponse{
RefreshToken: refreshToken,
AccessToken: tokenString,
})
}
// Get id from url
func GetUUID(r *http.Request) (uuid.UUID, error) {
id := mux.Vars(r)["id"]
uid, err := uuid.Parse(id)
if err != nil {
return uuid.Nil, err
}
return uid, nil
}