can get a map of month to cost

doesnt concern itself with years yet...
This commit is contained in:
Derek McQuay 2016-08-23 09:45:14 -07:00
parent 840db08294
commit e6e2f47134
2 changed files with 46 additions and 2 deletions

View File

@ -24,6 +24,8 @@ func addRoutes(sm *http.ServeMux, server *Server, staticFiles string) {
"health": "/healthz", "health": "/healthz",
"list": "/api/v0/list/", "list": "/api/v0/list/",
"tranx": "/api/v0/tranx/", "tranx": "/api/v0/tranx/",
"cost": "/api/v0/costpermonth/",
"fake": "/fake/", "fake": "/fake/",
} }
@ -76,5 +78,6 @@ func addRoutes(sm *http.ServeMux, server *Server, staticFiles string) {
sm.HandleFunc(prefix["health"], server.health) sm.HandleFunc(prefix["health"], server.health)
sm.HandleFunc(prefix["list"], server.listUsers) sm.HandleFunc(prefix["list"], server.listUsers)
sm.HandleFunc(prefix["tranx"], server.tranx) sm.HandleFunc(prefix["tranx"], server.tranx)
sm.HandleFunc(prefix["cost"], server.costPerMonth)
sm.HandleFunc(prefix["fake"], server.fakeSetup) sm.HandleFunc(prefix["fake"], server.fakeSetup)
} }

View File

@ -117,7 +117,7 @@ func (s *Server) tranx(w http.ResponseWriter, r *http.Request) {
} }
defer r.Body.Close() defer r.Body.Close()
users[u].addTranx( users[u].txs = append(users[u].txs,
tranx{ tranx{
Cost: t.Cost, Cost: t.Cost,
Store: t.Store, Store: t.Store,
@ -128,6 +128,47 @@ func (s *Server) tranx(w http.ResponseWriter, r *http.Request) {
} }
} }
func (s *Server) costPerMonth(w http.ResponseWriter, r *http.Request) {
//TODO add back in oauth
//w.Header().Set("Content-Type", "application/json")
//session, _ := store.Get(r, "creds")
//if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
//}
//if loggedIn := session.Values["authenticated"]; loggedIn != true {
// http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
// return
//}
switch r.Method {
default:
b, _ := json.Marshal(NewFailure("Allowed method: GET"))
http.Error(w, string(b), http.StatusBadRequest)
return
case "GET":
u, err := getUser("derekmcquay@gmail.com") //TODO will grab this from session
if err != nil {
b, _ := json.Marshal(NewFailure(err.Error()))
http.Error(w, string(b), http.StatusInternalServerError)
return
}
monthCost := make(map[time.Month]float32)
for _, t := range users[u].txs {
c, ok := monthCost[t.Month]
if !ok {
monthCost[t.Month] = t.Cost
continue
}
monthCost[t.Month] = t.Cost + c
}
err = json.NewEncoder(w).Encode(monthCost)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func (s *Server) listUsers(w http.ResponseWriter, r *http.Request) { func (s *Server) listUsers(w http.ResponseWriter, r *http.Request) {
//TODO add back in oauth //TODO add back in oauth
//w.Header().Set("Content-Type", "application/json") //w.Header().Set("Content-Type", "application/json")