/
transfer.go
95 lines (88 loc) · 3.01 KB
/
transfer.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
package routing
import (
"encoding/json"
"net/http"
"github.com/go-chi/chi"
"github.com/rafael-sousa/stn-accounts/pkg/controller/rest/jwt"
"github.com/rafael-sousa/stn-accounts/pkg/controller/rest/middleware"
"github.com/rafael-sousa/stn-accounts/pkg/controller/rest/response"
"github.com/rafael-sousa/stn-accounts/pkg/model/dto"
"github.com/rafael-sousa/stn-accounts/pkg/model/types"
"github.com/rafael-sousa/stn-accounts/pkg/service"
"github.com/rs/zerolog/log"
)
type transferHandler struct {
transferSrv *service.Transfer
}
// Transfers handles the requests related to entity.Transfer
func Transfers(transferSrv *service.Transfer, jwtHandler *jwt.Handler) func(chi.Router) {
h := transferHandler{transferSrv: transferSrv}
return func(r chi.Router) {
r.Use(middleware.NewAuthenticated(jwtHandler))
r.Get("/", h.get)
r.Post("/", h.post)
}
}
// @ID get-transfer
// @tags v1
// @Summary Gets the list of tranfers for the current authenticated user
// @Accept json
// @Produce json
// @Success 200 {array} dto.TransferView
// @Failure 400 {object} body.JSONError
// @Failure 404 {object} body.JSONError
// @Failure 500 {object} body.JSONError
// @Router /transfers [get]
// @Security ApiKeyAuth
func (h *transferHandler) get(w http.ResponseWriter, r *http.Request) {
id, ok := r.Context().Value(middleware.CtxAccountID).(int64)
if !ok {
response.WriteErr(w, r, types.NewErr(types.InternalErr, "unable to get account id from request context", nil))
return
}
transfers, err := (*h.transferSrv).Fetch(r.Context(), id)
if err != nil {
response.WriteErr(w, r, err)
return
}
if err = response.WriteSuccess(w, r, transfers, nil); err != nil {
log.Error().Caller().Err(err).Msg("unable to encode the transfers into the response")
response.WriteErr(w, r, err)
}
}
// @ID post-transfer
// @tags v1
// @Summary Creates a new transfer
// @Accept json
// @Produce json
// @Param req body dto.TransferCreation required "Transfer Creation Request"
// @Header 201 {string} Location "/transfers/1"
// @Success 201 {object} dto.TransferView
// @Failure 400 {object} body.JSONError
// @Failure 404 {object} body.JSONError
// @Failure 500 {object} body.JSONError
// @Router /transfers [post]
// @Security ApiKeyAuth
func (h *transferHandler) post(w http.ResponseWriter, r *http.Request) {
id, ok := r.Context().Value(middleware.CtxAccountID).(int64)
if !ok {
response.WriteErr(w, r, types.NewErr(types.InternalErr, "unable to get account id from request context", nil))
return
}
var transferCreation dto.TransferCreation
err := json.NewDecoder(r.Body).Decode(&transferCreation)
if err != nil {
log.Error().Caller().Err(err).Msg("unable to decode request body as transfer creation")
response.WriteErr(w, r, err)
return
}
view, err := (*h.transferSrv).Create(r.Context(), id, transferCreation)
if err != nil {
response.WriteErr(w, r, err)
return
}
if err = response.WriteSuccess(w, r, view, view.ID); err != nil {
log.Error().Caller().Err(err).Msg("unable to encode transfer into response")
response.WriteErr(w, r, err)
}
}