added tranx route and partially coded

This commit is contained in:
Derek McQuay 2017-02-04 23:54:18 -08:00
parent 162f86c894
commit dc4b1be612
No known key found for this signature in database
GPG Key ID: 92A7BC0C86B0B91A
3 changed files with 92 additions and 1 deletions

74
api.go
View File

@ -2,8 +2,10 @@ package chipmunk
import ( import (
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"net/http" "net/http"
"time"
) )
func (s *Server) category(w http.ResponseWriter, req *http.Request) { 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
View File

@ -84,6 +84,23 @@ func (d *DB) adminUser(e string) bool {
return result.Admin 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 { //func (d *DB) checkOwner(filename, client string) error {
// row := d.db.QueryRowx("SELECT client FROM pics WHERE filename = $1", filename) // row := d.db.QueryRowx("SELECT client FROM pics WHERE filename = $1", filename)
// var owner string // var owner string

View File

@ -69,7 +69,7 @@ func addRoutes(sm *http.ServeMux, server *Server, staticFiles string) {
} }
store = sessions.NewCookieStore([]byte(server.cookieSecret)) 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["fake"], server.fakeSetup)
sm.HandleFunc(prefix["category"], server.category) sm.HandleFunc(prefix["category"], server.category)
sm.HandleFunc(prefix["user"], server.user) sm.HandleFunc(prefix["user"], server.user)