added tranx route and partially coded
This commit is contained in:
parent
162f86c894
commit
dc4b1be612
74
api.go
74
api.go
@ -2,8 +2,10 @@ package chipmunk
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *Server) category(w http.ResponseWriter, req *http.Request) {
|
||||
@ -175,3 +177,75 @@ func (s *Server) user(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) tranx(w http.ResponseWriter, req *http.Request) {
|
||||
// TODO add back in
|
||||
//w.Header().Set("Content-Type", "application/json")
|
||||
//session, err := store.Get(r, "creds")
|
||||
//if err != nil {
|
||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
// return
|
||||
//}
|
||||
//if loggedIn := session.Values["authenticated"]; loggedIn != true {
|
||||
// http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||
// return
|
||||
//}
|
||||
switch req.Method {
|
||||
default:
|
||||
b, _ := json.Marshal(NewFailure("Allowed methods: GET, POST, DELETE"))
|
||||
http.Error(w, string(b), http.StatusBadRequest)
|
||||
return
|
||||
case "GET":
|
||||
tranxs, err := s.db.getTranxs()
|
||||
if err != nil {
|
||||
log.Printf("%+v", err)
|
||||
b, _ := json.Marshal(NewFailure(err.Error()))
|
||||
http.Error(w, string(b), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(tranxs)
|
||||
case "POST":
|
||||
t := tranx{}
|
||||
err := json.NewDecoder(req.Body).Decode(&t)
|
||||
req.Body.Close()
|
||||
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,
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("%+v", err)
|
||||
b, _ := json.Marshal(NewFailure(err.Error()))
|
||||
http.Error(w, string(b), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
case "DELETE":
|
||||
t := tranx{}
|
||||
err := json.NewDecoder(req.Body).Decode(&t)
|
||||
req.Body.Close()
|
||||
if err != nil {
|
||||
b, _ := json.Marshal(NewFailure(err.Error()))
|
||||
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 {
|
||||
log.Printf("%+v", err)
|
||||
b, _ := json.Marshal(NewFailure(err.Error()))
|
||||
http.Error(w, string(b), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
17
db.go
17
db.go
@ -84,6 +84,23 @@ func (d *DB) adminUser(e string) bool {
|
||||
return result.Admin
|
||||
}
|
||||
|
||||
func (d *DB) getTranxs() ([]tranx, error) {
|
||||
results := []tranx{}
|
||||
rows, err := d.db.Queryx("SELECT * FROM tranx")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
var result tranx
|
||||
err := rows.StructScan(&result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, result)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
//func (d *DB) checkOwner(filename, client string) error {
|
||||
// row := d.db.QueryRowx("SELECT client FROM pics WHERE filename = $1", filename)
|
||||
// var owner string
|
||||
|
@ -69,7 +69,7 @@ func addRoutes(sm *http.ServeMux, server *Server, staticFiles string) {
|
||||
}
|
||||
|
||||
store = sessions.NewCookieStore([]byte(server.cookieSecret))
|
||||
//sm.HandleFunc(prefix["tranx"], server.tranx)
|
||||
sm.HandleFunc(prefix["tranx"], server.tranx)
|
||||
sm.HandleFunc(prefix["fake"], server.fakeSetup)
|
||||
sm.HandleFunc(prefix["category"], server.category)
|
||||
sm.HandleFunc(prefix["user"], server.user)
|
||||
|
Loading…
Reference in New Issue
Block a user