From c2f8d2eca05e8d9f6e4cc7d6ac67b65d9016084c Mon Sep 17 00:00:00 2001 From: Derek McQuay Date: Sun, 5 Feb 2017 00:23:42 -0800 Subject: [PATCH] addes tranx with user_id and category_id --- api.go | 20 ++++++++++++++++---- db.go | 26 ++++++++++++++++++++++++++ tranx.go | 16 +++++++++------- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/api.go b/api.go index 88a86c9..1392f56 100644 --- a/api.go +++ b/api.go @@ -2,7 +2,6 @@ package chipmunk import ( "encoding/json" - "fmt" "log" "net/http" "time" @@ -214,14 +213,28 @@ func (s *Server) tranx(w http.ResponseWriter, req *http.Request) { http.Error(w, string(b), http.StatusBadRequest) return } + category_id, err := s.db.getCategoryID(t.Category) + if err != nil { + log.Printf("%+v", err) + b, _ := json.Marshal(NewFailure(err.Error())) + http.Error(w, string(b), http.StatusBadRequest) + return + } + user_id, err := s.db.getUserID(t.User) + if err != nil { + log.Printf("%+v", err) + b, _ := json.Marshal(NewFailure(err.Error())) + http.Error(w, string(b), http.StatusBadRequest) + return + } _, err = s.db.db.Exec( `INSERT INTO tranx (cost, store, info, date, category_id, user_id) VALUES ($1, $2, $3, $4, $5, $6)`, t.Cost, t.Store, t.Info, time.Now(), - 1, - 1, + category_id, + user_id, ) if err != nil { log.Printf("%+v", err) @@ -238,7 +251,6 @@ func (s *Server) tranx(w http.ResponseWriter, req *http.Request) { http.Error(w, string(b), http.StatusBadRequest) return } - fmt.Println(t) // TODO need to find better way to delete tranx _, err = s.db.db.Exec("DELETE FROM tranx WHERE store = $1 AND cost = $2", t.Store, t.Cost) if err != nil { diff --git a/db.go b/db.go index d956d5e..71087fe 100644 --- a/db.go +++ b/db.go @@ -54,6 +54,19 @@ func (d *DB) getCategories() ([]category, error) { return results, nil } +func (d *DB) getCategoryID(c string) (int, error) { + result := 0 + row := d.db.QueryRow("SELECT id FROM categories WHERE name = $1", + c, + ) + err := row.Scan(&result) + if err != nil { + return 0, err + } + + return result, nil +} + func (d *DB) getUsers() ([]user, error) { results := []user{} rows, err := d.db.Queryx("SELECT id, email, admin FROM users") @@ -71,6 +84,19 @@ func (d *DB) getUsers() ([]user, error) { return results, nil } +func (d *DB) getUserID(u string) (int, error) { + result := 0 + row := d.db.QueryRow("SELECT id FROM users WHERE email = $1", + u, + ) + err := row.Scan(&result) + if err != nil { + return 0, err + } + + return result, nil +} + func (d *DB) adminUser(e string) bool { result := user{} row := d.db.QueryRow("SELECT admin FROM users WHERE email = $1", diff --git a/tranx.go b/tranx.go index 4bd7386..5c48fbb 100644 --- a/tranx.go +++ b/tranx.go @@ -3,11 +3,13 @@ package chipmunk import "time" type tranx struct { - ID int `json:"id"` - Cost float64 `json:"cost"` - Store string `json:"store"` - Info string `json:"info"` - Date time.Time `json:"date"` - User string `json:"user"` - Category string `json:"category"` + ID int `json:"id"` + Cost float64 `json:"cost"` + Store string `json:"store"` + Info string `json:"info"` + Date time.Time `json:"date"` + User string `json:"user"` + Category string `json:"category"` + User_ID int `json:"user_id"` + Category_ID int `json:"category_id"` }