working on getting edits for tranx

This commit is contained in:
Derek McQuay 2017-02-11 21:30:59 -08:00
parent 2bb365206e
commit 754fd144e6
No known key found for this signature in database
GPG Key ID: 92A7BC0C86B0B91A
2 changed files with 115 additions and 1 deletions

90
api.go
View File

@ -2,6 +2,7 @@ package chipmunk
import ( import (
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"net/http" "net/http"
"strings" "strings"
@ -192,7 +193,7 @@ func (s *Server) tranx(w http.ResponseWriter, req *http.Request) {
//} //}
switch req.Method { switch req.Method {
default: 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) http.Error(w, string(b), http.StatusBadRequest)
return return
case "GET": case "GET":
@ -220,8 +221,95 @@ func (s *Server) tranx(w http.ResponseWriter, req *http.Request) {
http.Error(w, string(b), http.StatusBadRequest) http.Error(w, string(b), http.StatusBadRequest)
return 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) 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": case "POST":
t := tranx{} t := tranx{}
err := json.NewDecoder(req.Body).Decode(&t) err := json.NewDecoder(req.Body).Decode(&t)

26
db.go
View File

@ -67,6 +67,32 @@ func (d *DB) getCategoryID(c string) (int, error) {
return result, nil 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) { func (d *DB) getUsers() ([]user, error) {
results := []user{} results := []user{}
rows, err := d.db.Queryx("SELECT id, email, admin FROM users") rows, err := d.db.Queryx("SELECT id, email, admin FROM users")