forked from joshsoftware/curem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlead_api.go
127 lines (118 loc) · 3.12 KB
/
lead_api.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
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
"labix.org/v2/mgo/bson"
)
// LeadsBaseURL is the base URL for the location of a lead resource.
const LeadsBaseURL string = "http://localhost:3000/leads/"
func init() {
r.HandleFunc("/leads", getLeadsHandler).Methods("GET")
r.HandleFunc("/leads", postLeadHandler).Methods("POST")
r.HandleFunc("/leads/{id}", getLeadHandler).Methods("GET")
r.HandleFunc("/leads/{id}", patchLeadHandler).Methods("PATCH")
r.HandleFunc("/leads/{id}", deleteLeadHandler).Methods("DELETE")
}
func getLeadsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
c, err := GetAllLeads()
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
enc := json.NewEncoder(w)
if err = enc.Encode(c); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func postLeadHandler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var l lead
err := decoder.Decode(&l)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
n, err := NewLead(l.ContactID, l.Source, l.Owner, l.Status, l.TeamSize, l.RatePerHour,
l.DurationInMonths, l.EstimatedStartDate, l.Comments)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
url := LeadsBaseURL + n.ID.Hex()
w.Header().Set("Location", url)
w.WriteHeader(http.StatusCreated)
}
func getLeadHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r)
i := vars["id"]
id := bson.ObjectIdHex(i)
c, err := GetLead(id)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
enc := json.NewEncoder(w)
if err = enc.Encode(c); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func patchLeadHandler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var l incomingLead
err := decoder.Decode(&l)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
vars := mux.Vars(r)
id := bson.ObjectIdHex(vars["id"])
fl, err := GetLead(id)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = fl.copyIncomingFields(&l)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = fl.Update()
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func deleteLeadHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
i := vars["id"]
id := bson.ObjectIdHex(i)
c, err := GetLead(id)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = c.Delete()
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}