addes tranx with user_id and category_id
This commit is contained in:
parent
4d665b3ffe
commit
c2f8d2eca0
20
api.go
20
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 {
|
||||
|
26
db.go
26
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",
|
||||
|
16
tranx.go
16
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"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user