From 754fd144e685f593ebb25412578545f45973914d Mon Sep 17 00:00:00 2001 From: Derek McQuay Date: Sat, 11 Feb 2017 21:30:59 -0800 Subject: [PATCH] working on getting edits for tranx --- api.go | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- db.go | 26 +++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) diff --git a/api.go b/api.go index b9abf62..978fd68 100644 --- a/api.go +++ b/api.go @@ -2,6 +2,7 @@ package chipmunk import ( "encoding/json" + "fmt" "log" "net/http" "strings" @@ -192,7 +193,7 @@ func (s *Server) tranx(w http.ResponseWriter, req *http.Request) { //} switch req.Method { default: - b, _ := json.Marshal(NewFailure("Allowed methods: GET, POST, DELETE")) + b, _ := json.Marshal(NewFailure("Allowed methods: GET, POST, PATCH, DELETE")) http.Error(w, string(b), http.StatusBadRequest) return case "GET": @@ -220,8 +221,95 @@ func (s *Server) tranx(w http.ResponseWriter, req *http.Request) { http.Error(w, string(b), http.StatusBadRequest) return } + t.User, err = s.db.getUserEmailByID(t.User_ID) + if err != nil { + log.Printf("%+v", err) + b, _ := json.Marshal(NewFailure(err.Error())) + http.Error(w, string(b), http.StatusBadRequest) + return + } + t.Category, err = s.db.getCategoryNameByID(t.Category_ID) + if err != nil { + log.Printf("%+v", err) + b, _ := json.Marshal(NewFailure(err.Error())) + http.Error(w, string(b), http.StatusBadRequest) + return + } json.NewEncoder(w).Encode(t) + case "PUT": + searchreq := req.URL.Path[len(prefix["tranx"]):] + if len(searchreq) == 0 { + b, _ := json.Marshal(NewFailure(fmt.Sprint("invalid url path"))) + http.Error(w, string(b), http.StatusBadRequest) + return + } + if searchreq[len(searchreq)-1] != '/' { + http.Redirect(w, req, prefix["tranx"]+searchreq+"/", http.StatusMovedPermanently) + return + } + err := req.ParseForm() + if err != nil { + log.Printf("%+v", err) + b, _ := json.Marshal(NewFailure(err.Error())) + http.Error(w, string(b), http.StatusBadRequest) + return + } + f := map[string]string{} + if req.Form("ID") != "" { + f["ID"] = req.Form("ID") + } + if req.Form("Cost") != "" { + f["Cost"] = req.Form("Cost") + } + if req.Form("Store") != "" { + f["Store"] = req.Form("Store") + } + if req.Form("Info") != "" { + f["Info"] = req.Form("Info") + } + if req.Form("Date") != "" { + f["Date"] = req.Form("Date") + } + if req.Form("User") != "" { + f["User"] = req.Form("User") + } + if req.Form("Category") != "" { + f["Category"] = req.Form("Category") + } + + searchReqParsed := strings.Split(searchreq, "/") + old, err := s.db.getTranx(searchReqParsed[0]) + if err != nil { + log.Printf("%+v", err) + b, _ := json.Marshal(NewFailure(err.Error())) + http.Error(w, string(b), http.StatusBadRequest) + return + } + old.User, err = s.db.getUserEmailByID(t.User_ID) + if err != nil { + log.Printf("%+v", err) + b, _ := json.Marshal(NewFailure(err.Error())) + http.Error(w, string(b), http.StatusBadRequest) + return + } + old.Category, err = s.db.getCategoryNameByID(t.Category_ID) + if err != nil { + log.Printf("%+v", err) + b, _ := json.Marshal(NewFailure(err.Error())) + http.Error(w, string(b), http.StatusBadRequest) + return + } + in := tranx{} + err := json.NewDecoder(req.Body).Decode(&in) + req.Body.Close() + if err != nil { + log.Printf("%+v", err) + b, _ := json.Marshal(NewFailure(err.Error())) + http.Error(w, string(b), http.StatusBadRequest) + return + } + json.NewEncoder(w).Encode(t) case "POST": t := tranx{} err := json.NewDecoder(req.Body).Decode(&t) diff --git a/db.go b/db.go index 8d7fa89..93cb414 100644 --- a/db.go +++ b/db.go @@ -67,6 +67,32 @@ func (d *DB) getCategoryID(c string) (int, error) { return result, nil } +func (d *DB) getCategoryNameByID(id int) (string, error) { + name := "" + row := d.db.QueryRow("SELECT name FROM categories WHERE id = $1", + id, + ) + err := row.Scan(&name) + if err != nil { + return name, err + } + + return name, nil +} + +func (d *DB) getUserEmailByID(id int) (string, error) { + email := "" + row := d.db.QueryRow("SELECT email FROM users WHERE id = $1", + id, + ) + err := row.Scan(&email) + if err != nil { + return email, err + } + + return email, nil +} + func (d *DB) getUsers() ([]user, error) { results := []user{} rows, err := d.db.Queryx("SELECT id, email, admin FROM users")