tranx POST and GET working
cleaned up the getUser function, now returns index to user rather than *user
This commit is contained in:
parent
6b2f97632d
commit
36a90e04c8
94
server.go
94
server.go
@ -3,7 +3,6 @@ package chipmunk
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
@ -141,21 +140,42 @@ func (s *Server) tranx(w http.ResponseWriter, r *http.Request) {
|
|||||||
//}
|
//}
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
default:
|
default:
|
||||||
b, _ := json.Marshal(NewFailure("Allowed method: GET"))
|
b, _ := json.Marshal(NewFailure("Allowed method: POST"))
|
||||||
http.Error(w, string(b), http.StatusBadRequest)
|
http.Error(w, string(b), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
case "GET":
|
case "GET":
|
||||||
u, err := getUser("derekmcquay@gmail.com")
|
u, err := getUser("derekmcquay@gmail.com") //TODO will grab this from session
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b, _ := json.Marshal(NewFailure(err.Error()))
|
b, _ := json.Marshal(NewFailure(err.Error()))
|
||||||
http.Error(w, string(b), http.StatusInternalServerError)
|
http.Error(w, string(b), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
u.addTranx(
|
json.NewEncoder(w).Encode(users[u].txs)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case "POST":
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
t := tranx{}
|
||||||
|
err = json.NewDecoder(r.Body).Decode(&t)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
users[u].addTranx(
|
||||||
tranx{
|
tranx{
|
||||||
Cost: 1.99,
|
Cost: t.Cost,
|
||||||
Store: "target",
|
Store: t.Store,
|
||||||
Info: "just because",
|
Info: t.Info,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -175,62 +195,12 @@ func (s *Server) listUsers(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, string(b), http.StatusBadRequest)
|
http.Error(w, string(b), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
case "GET":
|
case "GET":
|
||||||
j, _ := json.Marshal(users)
|
err := json.NewEncoder(w).Encode(users)
|
||||||
fmt.Fprintf(w, string(j))
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) list(w http.ResponseWriter, r *http.Request) {
|
|
||||||
//w.Header().Set("Content-Type", "application/json")
|
|
||||||
//session, _ := store.Get(r, "creds")
|
|
||||||
//if loggedIn := session.Values["authenticated"]; loggedIn != true {
|
|
||||||
// http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
//switch r.Method {
|
|
||||||
//default:
|
|
||||||
// b, _ := json.Marshal(NewFailure("Allowed method: POST"))
|
|
||||||
// http.Error(w, string(b), http.StatusBadRequest)
|
|
||||||
// return
|
|
||||||
//case "POST":
|
|
||||||
// searchreq := r.URL.Path[len(prefix["list"]):]
|
|
||||||
// if len(searchreq) == 0 {
|
|
||||||
// b, _ := json.Marshal(NewFailure("url could not be parsed"))
|
|
||||||
// http.Error(w, string(b), http.StatusBadRequest)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// if searchreq[len(searchreq)-1] != '/' {
|
|
||||||
// http.Redirect(w, r, prefix["list"]+searchreq+"/", http.StatusMovedPermanently)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// searchReqParsed := strings.Split(searchreq, "/")
|
|
||||||
// client := github.NewClient(nil)
|
|
||||||
// if s.ApiToken != "" {
|
|
||||||
// ts := oauth2.StaticTokenSource(
|
|
||||||
// &oauth2.Token{AccessToken: s.ApiToken},
|
|
||||||
// )
|
|
||||||
// tc := oauth2.NewClient(oauth2.NoContext, ts)
|
|
||||||
// client = github.NewClient(tc)
|
|
||||||
// }
|
|
||||||
// opt := &github.RepositoryListOptions{}
|
|
||||||
// repos, _, err := client.Repositories.List(searchReqParsed[0], opt)
|
|
||||||
// if err != nil {
|
|
||||||
// b, _ := json.Marshal(NewFailure("user could not be found"))
|
|
||||||
// http.Error(w, string(b), http.StatusBadRequest)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// var items []Item
|
|
||||||
// for _, i := range repos {
|
|
||||||
// items = append(items, Item{*i.Name, *i.StargazersCount})
|
|
||||||
// }
|
|
||||||
|
|
||||||
// err = json.NewEncoder(w).Encode(items)
|
|
||||||
// if err != nil {
|
|
||||||
// b, _ := json.Marshal(NewFailure(err.Error()))
|
|
||||||
// http.Error(w, string(b), http.StatusInternalServerError)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) auth(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) auth(w http.ResponseWriter, r *http.Request) {
|
||||||
|
12
user.go
12
user.go
@ -35,13 +35,15 @@ func authorizedEmail(e string) bool {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUser(e string) (*user, error) {
|
// getUser returns index of user with given email, otherwise it returns an
|
||||||
for _, i := range users {
|
// error that it could not find that user
|
||||||
if e == i.Info.Email {
|
func getUser(e string) (int, error) {
|
||||||
return &i, nil
|
for i, u := range users {
|
||||||
|
if e == u.Info.Email {
|
||||||
|
return i, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &user{}, fmt.Errorf("could not find user")
|
return 0, fmt.Errorf("could not find user")
|
||||||
}
|
}
|
||||||
|
|
||||||
func addUser(u userInfo) {
|
func addUser(u userInfo) {
|
||||||
|
Loading…
Reference in New Issue
Block a user