-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcustomers_handlers.go
executable file
·135 lines (114 loc) · 3.67 KB
/
customers_handlers.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
package main
import (
"encoding/json"
"gopkg.in/mgo.v2/bson"
"net/http"
"time"
)
type CustomersHandlers struct {
dbConnection *MongoConnection
}
func (cHandlers *CustomersHandlers) NewCustomer(w http.ResponseWriter, r *http.Request) {
token := r.FormValue("token")
name := r.FormValue("name")
email := r.FormValue("email")
address := r.FormValue("address")
address2 := r.FormValue("address2")
city := r.FormValue("city")
zip := r.FormValue("zip")
country := r.FormValue("country")
telephone := r.FormValue("telephone")
telephone2 := r.FormValue("telephone2")
customer := &Customer{
UserID: bson.ObjectIdHex(token),
Name: name,
Email: email,
Address: address,
Address2: address2,
City: city,
Zip: zip,
Country: country,
Telephone: telephone,
Telephone2: telephone2,
DateCreated: time.Now()}
result := cHandlers.dbConnection.CreateNewCustomer(customer)
if result {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(jsonErr{Code: http.StatusNotFound, Text: "Not Found"}); err != nil {
panic(err)
}
}
func (cHandlers *CustomersHandlers) UpdateCustomer(w http.ResponseWriter, r *http.Request) {
token := r.FormValue("token")
customerID := r.FormValue("customer_id")
name := r.FormValue("name")
email := r.FormValue("email")
address := r.FormValue("address")
address2 := r.FormValue("address2")
city := r.FormValue("city")
zip := r.FormValue("zip")
country := r.FormValue("country")
telephone := r.FormValue("telephone")
telephone2 := r.FormValue("telephone2")
customer := &Customer{
Id: bson.ObjectIdHex(customerID),
UserID: bson.ObjectIdHex(token),
Name: name,
Email: email,
Address: address,
Address2: address2,
City: city,
Zip: zip,
Country: country,
Telephone: telephone,
Telephone2: telephone2,
DateCreated: time.Now()}
result := cHandlers.dbConnection.UpdateCustomer(customer)
if result {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(jsonErr{Code: http.StatusNotFound, Text: "Not Found"}); err != nil {
panic(err)
}
}
func (cHandlers *CustomersHandlers) DeleteCustomer(w http.ResponseWriter, r *http.Request) {
token := r.FormValue("token")
customerID := r.FormValue("customer_id")
result := cHandlers.dbConnection.DeleteCustomer(token, customerID)
if result {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(jsonErr{Code: http.StatusNotFound, Text: "Error removing customer"}); err != nil {
panic(err)
}
}
func (cHandlers *CustomersHandlers) ListCustomers(w http.ResponseWriter, r *http.Request) {
token := r.FormValue("token")
customers := cHandlers.dbConnection.ListAllCustomers(token)
if len(customers) > 0 {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(customers); err != nil {
panic(err)
}
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(jsonErr{Code: http.StatusNotFound, Text: "Not Found"}); err != nil {
panic(err)
}
}