This repository has been archived by the owner on Dec 3, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
193 lines (145 loc) · 4.83 KB
/
app.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
package main
import (
// "log"
"fmt"
"net/http"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"encoding/json"
// "time"
)
type Transaction struct {
Id bson.ObjectId `bson:"_id"`
TransactionNumber string `bson:"TransactionNumber"`
ServiceAreaCategorisation string `bson:"ServiceAreaCategorisation"`
Amount float32 `bson:"Amount"`
ExpensesType string `bson:"ExpensesType"`
ServiceCode string `bson:"ServiceCode"`
AccountCodeDescription string `bson:"AccountCodeDescription"`
Date string `bson:"Date"`
SupplierName string `bson:"SupplierName"`
BodyName string `bson:"BodyName"`
Year string `bson:"Year"`
Month string `bson:"Month"`
Votes float32 `bson:"Votes"`
Distance float32 `bson:"distance"`
}
type DistanceResult struct {
ZeroToFive int
FiveToTen int
TenToTwentyFive int
TwentyFiveToFifty int
FiftyToHundred int
HundredPlus int
}
func CompaniesHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-type", "application/json")
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("hacked").C("transactions")
r.ParseForm()
var result = []Transaction{}
var q = bson.M{}
if len(r.Form.Get("SupplierName")) > 0 {
q["SupplierName"] = r.Form.Get("SupplierName")
}
if len(r.Form.Get("Year")) > 0 {
q["Year"] = r.Form.Get("Year")
}
if len(r.Form.Get("Month")) > 0 {
q["Month"] = r.Form.Get("Month")
}
if len(r.Form.Get("SupplierName")) > 0 {
c.Find(q).Limit(1000).All(&result)
} else {
c.Find(q).Limit(100).All(&result)
}
var indented, _ = json.MarshalIndent(result, "", " ")
fmt.Fprintf(w, "%s\n", indented)
}
func LtdCompaniesHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-type", "application/json")
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("hacked").C("transactions")
var result []string
var err2 = c.Find(nil).Distinct("SupplierName", &result)
if err2 != nil {
fmt.Println(err2)
}
var indented, _ = json.MarshalIndent(result, "", " ")
fmt.Fprintf(w, "%s\n", indented)
}
func UpdateHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-type", "application/json")
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("hacked").C("transactions")
var t Transaction
var decoder = json.NewDecoder(r.Body)
decoder.Decode(&t)
c.Update(bson.M{"_id": t.Id}, t)
}
func DistanceHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-type", "application/json")
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("hacked").C("transactions")
r.ParseForm()
var result = DistanceResult{}
var q = bson.M{}
if len(r.Form.Get("Year")) > 0 {
q["Year"] = r.Form.Get("Year")
}
if len(r.Form.Get("Month")) > 0 {
q["Month"] = r.Form.Get("Month")
}
q["distance"] = bson.M{"$gt": 0, "$lt": 5}
result.ZeroToFive, _ = c.Find(q).Count()
q["distance"] = bson.M{"$gt": 5, "$lt": 10}
result.FiveToTen, _ = c.Find(q).Count()
q["distance"] = bson.M{"$gt": 10, "$lt": 25}
result.TenToTwentyFive, _ = c.Find(q).Count()
q["distance"] = bson.M{"$gt": 25, "$lt": 50}
result.TwentyFiveToFifty, _ = c.Find(q).Count()
q["distance"] = bson.M{"$gt": 50, "$lt": 100}
result.FiftyToHundred, _ = c.Find(q).Count()
q["distance"] = bson.M{"$gt": 100}
result.HundredPlus, _ = c.Find(q).Count()
var indented, _ = json.MarshalIndent(result, "", " ")
fmt.Fprintf(w, "%s\n", indented)
}
func serveSingle(pattern string, filename string) {
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filename)
})
}
func main() {
http.Handle("/", http.FileServer(http.Dir("./app/")))
http.Handle("/bower_components/", http.FileServer(http.Dir(".")))
http.HandleFunc("/api/companies/", CompaniesHandler)
http.HandleFunc("/api/update/", UpdateHandler)
http.HandleFunc("/api/distances/", DistanceHandler)
http.HandleFunc("/api/ltdcompanies/", LtdCompaniesHandler)
http.ListenAndServe(":8080", nil)
}