addes tranx with user_id and category_id

This commit is contained in:
Derek McQuay 2017-02-05 00:23:42 -08:00
parent 4d665b3ffe
commit c2f8d2eca0
No known key found for this signature in database
GPG Key ID: 92A7BC0C86B0B91A
3 changed files with 51 additions and 11 deletions

20
api.go
View File

@ -2,7 +2,6 @@ package chipmunk
import ( import (
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"net/http" "net/http"
"time" "time"
@ -214,14 +213,28 @@ 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
} }
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( _, err = s.db.db.Exec(
`INSERT INTO tranx (cost, store, info, date, category_id, user_id) VALUES ($1, $2, $3, $4, $5, $6)`, `INSERT INTO tranx (cost, store, info, date, category_id, user_id) VALUES ($1, $2, $3, $4, $5, $6)`,
t.Cost, t.Cost,
t.Store, t.Store,
t.Info, t.Info,
time.Now(), time.Now(),
1, category_id,
1, user_id,
) )
if err != nil { if err != nil {
log.Printf("%+v", err) 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) http.Error(w, string(b), http.StatusBadRequest)
return return
} }
fmt.Println(t)
// TODO need to find better way to delete tranx // 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) _, err = s.db.db.Exec("DELETE FROM tranx WHERE store = $1 AND cost = $2", t.Store, t.Cost)
if err != nil { if err != nil {

26
db.go
View File

@ -54,6 +54,19 @@ func (d *DB) getCategories() ([]category, error) {
return results, nil 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) { 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")
@ -71,6 +84,19 @@ func (d *DB) getUsers() ([]user, error) {
return results, nil 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 { func (d *DB) adminUser(e string) bool {
result := user{} result := user{}
row := d.db.QueryRow("SELECT admin FROM users WHERE email = $1", row := d.db.QueryRow("SELECT admin FROM users WHERE email = $1",

View File

@ -10,4 +10,6 @@ type tranx struct {
Date time.Time `json:"date"` Date time.Time `json:"date"`
User string `json:"user"` User string `json:"user"`
Category string `json:"category"` Category string `json:"category"`
User_ID int `json:"user_id"`
Category_ID int `json:"category_id"`
} }