-
Notifications
You must be signed in to change notification settings - Fork 0
/
customerhandler.go
54 lines (48 loc) · 1.33 KB
/
customerhandler.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
package app
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/ravikrs/go-rest-banking/s3/dto"
"github.com/ravikrs/go-rest-banking/s3/errs"
"github.com/ravikrs/go-rest-banking/s3/service"
"net/http"
)
type CustomerHandler struct {
service service.CustomerService
}
func (ch CustomerHandler) getAllCustomers(w http.ResponseWriter, r *http.Request) {
statusQueryParam := r.URL.Query().Get("status")
var customers []dto.CustomerResponse
var err *errs.AppError
if statusQueryParam != "" {
customers, err = ch.service.GetAllCustomerByStatus(statusQueryParam)
} else {
customers, err = ch.service.GetAllCustomer()
}
if err != nil {
writeResponse(w, err.Code, err)
return
}
writeResponse(w, http.StatusOK, customers)
}
func (ch CustomerHandler) getCustomerByID(w http.ResponseWriter, r *http.Request) {
customerId := mux.Vars(r)["customer_id"]
customer, err := ch.service.GetCustomerByID(customerId)
if err != nil {
writeResponse(w, err.Code, customer)
return
}
writeResponse(w, http.StatusOK, customer)
}
func writeResponse(w http.ResponseWriter, statusCode int, data interface{}) {
w.WriteHeader(statusCode)
w.Header().Add("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(data)
if err != nil {
panic(err)
}
}
func health(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "UP")
}