/
customer_handler.go
200 lines (170 loc) · 6.7 KB
/
customer_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
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
package api
import (
"context"
"encoding/json"
"io"
"net/http"
"strconv"
"github.com/tedsuo/rata"
"github.com/jmelchio/vetlab/model"
)
// CustomerServer struct allows the CustomerService injection into the REST handler
type CustomerServer struct {
CustomerService CustomerService
}
// Constants for error messages and request names
const (
CreateCustomer = "create_customer"
UpdateCustomer = "update_customer"
DeleteCustomer = "delete_customer"
CustomerLogin = "customer_login"
FindCustomer = "find_customer"
FindCustomerByUserName = "find_customer_by_user_name"
UnableToCreateCustomer = "Unable to create a customer"
UnableToUpdateCustomer = "Unable to update a customer"
UnableToDeleteCustomer = "Unable to delete a customer"
UnableToLoginCustomer = "Unable to login the customer"
UnableToFindCustomer = "Unable to find the customer(s)"
)
// CustomerRoutes are the REST endpoint routes for the customer REST interface
var CustomerRoutes = rata.Routes{
{Path: "/customers", Method: rata.POST, Name: CreateCustomer},
{Path: "/customers", Method: rata.PUT, Name: UpdateCustomer},
{Path: "/customers", Method: rata.DELETE, Name: DeleteCustomer},
{Path: "/customers/login", Method: rata.POST, Name: CustomerLogin},
{Path: "/customers/:customer_id", Method: rata.GET, Name: FindCustomer},
{Path: "/customers/user_name/:user_name", Method: rata.GET, Name: FindCustomerByUserName},
}
// NewCustomerHandler provides the factory function to create the REST interface for customer actions
func NewCustomerHandler(customerService CustomerService) (http.Handler, error) {
customerServer := &CustomerServer{CustomerService: customerService}
handlers := rata.Handlers{
CreateCustomer: openCors(http.HandlerFunc(customerServer.CreateCustomer), "*"),
UpdateCustomer: openCors(http.HandlerFunc(customerServer.UpdateCustomer), "*"),
DeleteCustomer: openCors(http.HandlerFunc(customerServer.DeleteCustomer), "*"),
CustomerLogin: openCors(http.HandlerFunc(customerServer.CustomerLogin), "*"),
FindCustomer: openCors(http.HandlerFunc(customerServer.FindCustomer), "*"),
FindCustomerByUserName: openCors(http.HandlerFunc(customerServer.FindCustomerByUserName), "*"),
}
return rata.NewRouter(CustomerRoutes, handlers)
}
// CreateCustomer is the REST endpoint function that allows for the creation of customers in the system
func (customerServer *CustomerServer) CreateCustomer(writer http.ResponseWriter, request *http.Request) {
if request.Body == nil {
http.Error(writer, EmptyBody, http.StatusBadRequest)
return
}
requestBody, err := io.ReadAll(request.Body)
if err != nil {
http.Error(writer, UnableToParseBody, http.StatusBadRequest)
return
}
var createCustomer model.Customer
err = json.Unmarshal(requestBody, &createCustomer)
if err != nil {
http.Error(writer, InvalidBody, http.StatusBadRequest)
return
}
newCustomer, err := customerServer.CustomerService.CreateCustomer(context.TODO(), createCustomer)
if err != nil {
http.Error(writer, UnableToCreateCustomer, http.StatusInternalServerError)
return
}
writeJSONResponse(writer, http.StatusCreated, newCustomer)
}
// UpdateCustomer handles the request for updating a Customer on the system
func (customerServer *CustomerServer) UpdateCustomer(writer http.ResponseWriter, request *http.Request) {
if request.Body == nil {
http.Error(writer, EmptyBody, http.StatusBadRequest)
return
}
requestBody, err := io.ReadAll(request.Body)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
return
}
var updateCustomer model.Customer
err = json.Unmarshal(requestBody, &updateCustomer)
if err != nil {
http.Error(writer, InvalidBody, http.StatusBadRequest)
return
}
newCustomer, err := customerServer.CustomerService.UpdateCustomer(context.TODO(), updateCustomer)
if err != nil {
http.Error(writer, UnableToUpdateCustomer, http.StatusInternalServerError)
return
}
writeJSONResponse(writer, http.StatusOK, newCustomer)
}
// DeleteCustomer handles the request to delete a Customer from the system
func (customerServer *CustomerServer) DeleteCustomer(writer http.ResponseWriter, request *http.Request) {
if request.Body == nil {
http.Error(writer, EmptyBody, http.StatusBadRequest)
return
}
requestBody, err := io.ReadAll(request.Body)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
return
}
var deleteCustomer model.Customer
err = json.Unmarshal(requestBody, &deleteCustomer)
if err != nil {
http.Error(writer, InvalidBody, http.StatusBadRequest)
return
}
err = customerServer.CustomerService.DeleteCustomer(context.TODO(), deleteCustomer)
if err != nil {
http.Error(writer, UnableToDeleteCustomer, http.StatusInternalServerError)
return
}
writer.WriteHeader(http.StatusNoContent)
}
// CustomerLogin handles the request to log in a Customer to the system
func (customerServer *CustomerServer) CustomerLogin(writer http.ResponseWriter, request *http.Request) {
if request.Body == nil {
http.Error(writer, EmptyBody, http.StatusBadRequest)
return
}
requestBody, err := io.ReadAll(request.Body)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
return
}
var loginRequest model.LoginRequest
err = json.Unmarshal(requestBody, &loginRequest)
if err != nil {
http.Error(writer, InvalidBody, http.StatusBadRequest)
return
}
loginCustomer, err := customerServer.CustomerService.Login(context.TODO(), loginRequest.UserName, loginRequest.Password)
if err != nil {
http.Error(writer, UnableToLoginCustomer, http.StatusInternalServerError)
return
}
writeJSONResponse(writer, http.StatusOK, loginCustomer)
}
// FindCustomer handles the request to find a Customer by their customer id
func (customerServer *CustomerServer) FindCustomer(writer http.ResponseWriter, request *http.Request) {
customerID := rata.Param(request, "customer_id")
if uintValue, err := strconv.ParseUint(customerID, 10, 32); err == nil {
foundCustomer, err := customerServer.CustomerService.FindCustomerByID(context.TODO(), uint(uintValue))
if err != nil {
http.Error(writer, UnableToFindCustomer, http.StatusNotFound)
return
}
writeJSONResponse(writer, http.StatusOK, foundCustomer)
return
}
http.Error(writer, NoParamsFound, http.StatusBadRequest)
}
// FindCustomerByUserName handles the request to find a Customer by their customer name
func (customerServer *CustomerServer) FindCustomerByUserName(writer http.ResponseWriter, request *http.Request) {
userName := rata.Param(request, "user_name")
foundCustomer, err := customerServer.CustomerService.FindCustomerByUserName(context.TODO(), userName)
if err != nil {
http.Error(writer, UnableToFindCustomer, http.StatusNotFound)
return
}
writeJSONResponse(writer, http.StatusOK, foundCustomer)
}