-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts_endpoint.go
66 lines (57 loc) · 1.58 KB
/
accounts_endpoint.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
package main
import (
"context"
"database/sql"
"github.com/c-pro/wallet-test/models"
"github.com/go-kit/kit/endpoint"
"github.com/shopspring/decimal"
)
type getAccountsResponse struct {
Accounts []models.Account `json:"Accounts,omitempty"`
}
type getAccountRequest struct {
AccountID int64
}
type errorResponse struct {
Error string `json:"Error,omitempty"`
Code int `json:"-"`
}
type createAccountRequest struct {
Name string
CurrencyID int64
Amount decimal.Decimal
}
func makeGetAccountsEndpoint(svc AccountService) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
accounts, err := svc.GetAccounts()
if err != nil {
return errorResponse{err.Error(), 500}, nil
}
return getAccountsResponse{accounts}, nil
}
}
func makeGetAccountEndpoint(svc AccountService) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(getAccountRequest)
account, err := svc.GetAccount(req.AccountID)
if err != nil {
if err == sql.ErrNoRows {
return errorResponse{"Account not found", 404}, nil
}
return errorResponse{err.Error(), 500}, nil
}
return account, nil
}
}
func makeCreateAccountEndpoint(svc AccountService) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(createAccountRequest)
err := svc.CreateAccount(models.Account{Name: req.Name,
CurrencyID: req.CurrencyID,
Amount: req.Amount})
if err != nil {
return errorResponse{err.Error(), 500}, err
}
return struct{}{}, nil
}
}