-
Notifications
You must be signed in to change notification settings - Fork 1
/
chi-handler.go
156 lines (138 loc) · 3.73 KB
/
chi-handler.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
package handler
import (
"encoding/json"
"log"
"net/http"
cdomain "github.com/devpablocristo/golang/06-projects/qh/internal/commons/domain"
port "github.com/devpablocristo/golang/06-projects/qh/person/application/port"
domain "github.com/devpablocristo/golang/06-projects/qh/person/domain"
"github.com/go-chi/chi"
)
type ChiHandler struct {
personService port.Service
}
func NewChiHandler(ps port.Service) *ChiHandler {
return &ChiHandler{
personService: ps,
}
}
func (h *ChiHandler) CreatePerson(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
body := r.Body
defer body.Close()
var errReq cdomain.APIError
errReq.Method = "chiAdapter.CreatePerson"
var newPerson domain.Person
err := json.NewDecoder(body).Decode(&newPerson)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
errReq = cdomain.ErrInvalidJSON
errReq.Error = err.Error()
err := json.NewEncoder(w).Encode(
errReq,
)
if err != nil {
errReq.Error = err.Error()
log.Println(errReq)
w.Write([]byte(errReq.Message + " - " + errReq.Error))
return
}
log.Println(errReq)
w.Write([]byte(errReq.Message + " - " + errReq.Error))
return
}
ctx := r.Context()
person, err := h.personService.CreatePerson(ctx, &newPerson)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
errReq = cdomain.ErrInternalServer
errReq.Error = err.Error()
err := json.NewEncoder(w).Encode(
cdomain.ErrInternalServer,
)
if err != nil {
errReq.Error = err.Error()
log.Println(errReq)
w.Write([]byte(errReq.Message + " - " + errReq.Error))
return
}
log.Println(errReq)
w.Write([]byte(errReq.Message + " - " + errReq.Error))
return
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(
cdomain.ResponseAPI{
Success: true,
Status: http.StatusCreated,
Result: person,
},
)
}
func (h *ChiHandler) GetPerson(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
UUID := chi.URLParam(r, "personUUID")
var errReq cdomain.APIError
errReq.Method = "chihandler.GetPerson"
ctx := r.Context()
person, err := h.personService.GetPerson(ctx, UUID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
errReq = cdomain.ErrInternalServer
errReq.Error = err.Error()
err := json.NewEncoder(w).Encode(
cdomain.ErrInternalServer,
)
if err != nil {
errReq.Error = err.Error()
log.Println(errReq)
w.Write([]byte(errReq.Message + " - " + errReq.Error))
return
}
log.Println(errReq)
w.Write([]byte(errReq.Message + " - " + errReq.Error))
return
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(
cdomain.ResponseAPI{
Success: true,
Status: http.StatusCreated,
Result: person,
},
)
}
func (h *ChiHandler) GetPersons(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var errReq cdomain.APIError
errReq.Method = "chihandler.GetPersons"
ctx := r.Context()
persons, err := h.personService.GetPersons(ctx)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
errReq = cdomain.ErrInternalServer
errReq.Error = err.Error()
err := json.NewEncoder(w).Encode(
cdomain.ErrInternalServer,
)
if err != nil {
errReq.Error = err.Error()
log.Println(errReq)
w.Write([]byte(errReq.Message + " - " + errReq.Error))
return
}
log.Println(errReq)
w.Write([]byte(errReq.Message + " - " + errReq.Error))
return
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(
cdomain.ResponseAPI{
Success: true,
Status: http.StatusCreated,
Result: persons,
},
)
}
func (h *ChiHandler) UpdatePerson(w http.ResponseWriter, r *http.Request) {}
func (h *ChiHandler) DeletePerson(w http.ResponseWriter, r *http.Request) {}