-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
77 lines (69 loc) · 1.9 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
package web
import (
"encoding/json"
"net/http"
"github.com/go-chi/chi"
"github.com/miya-masa/go-clean-webapp/interface/presenter"
"github.com/miya-masa/go-clean-webapp/usecase"
)
type AccountHandler struct {
Usecase usecase.AccountInputPort
Presenter *presenter.AccountPresenter
}
func NewAccountHandler(uc usecase.AccountInputPort, pr *presenter.AccountPresenter) *AccountHandler {
return &AccountHandler{
Usecase: uc,
Presenter: pr,
}
}
func (u *AccountHandler) List(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
view := &AccountJsonView{w: w, successCode: http.StatusOK}
accounts, err := u.Usecase.List(ctx)
if err != nil {
logger.Println(err)
view.ErrorView(err)
return
}
view.ViewModels(u.Presenter.ToViewModels(accounts))
}
func (u *AccountHandler) Get(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
accountUUID := chi.URLParam(r, "accountUUID")
view := &AccountJsonView{w: w, successCode: http.StatusOK}
account, err := u.Usecase.Find(ctx, accountUUID)
if err != nil {
logger.Println(err)
view.ErrorView(err)
return
}
view.View(u.Presenter.ToViewModel(account))
}
func (u *AccountHandler) Post(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
view := &AccountJsonView{w: w, successCode: http.StatusCreated}
in := &usecase.AccountStoreInput{}
if err := json.NewDecoder(r.Body).Decode(in); err != nil {
logger.Println(err)
view.ErrorView(err)
return
}
account, err := u.Usecase.Store(ctx, in)
if err != nil {
logger.Println(err)
view.ErrorView(err)
return
}
view.View(u.Presenter.ToViewModel(account))
}
func (u *AccountHandler) Delete(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
view := &AccountJsonView{w: w, successCode: http.StatusNoContent}
_, err := u.Usecase.Delete(ctx, chi.URLParam(r, "accountUUID"))
if err != nil {
logger.Println(err)
view.ErrorView(err)
return
}
view.ViewNoBody()
}