category route added and coded

added api.go where api routes will be built (will move code from
server.go in the future to api.go that fits that) and coded out the
category route (delete, post, and get)
This commit is contained in:
Derek McQuay 2017-02-04 22:33:28 -08:00
parent 95e85aa011
commit d168a117d2
No known key found for this signature in database
GPG Key ID: 92A7BC0C86B0B91A
3 changed files with 98 additions and 16 deletions

79
api.go Normal file
View File

@ -0,0 +1,79 @@
package chipmunk
import (
"encoding/json"
"log"
"net/http"
)
func (s *Server) category(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":
categories, err := s.db.getCategories()
if err != nil {
log.Printf("%+v", err)
b, _ := json.Marshal(NewFailure(err.Error()))
http.Error(w, string(b), http.StatusBadRequest)
return
}
json.NewEncoder(w).Encode(categories)
case "POST":
cat := category{}
err := json.NewDecoder(req.Body).Decode(&cat)
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 categories (name, budget) VALUES ($1, $2)`,
cat.Name,
cat.Budget,
)
if err != nil {
log.Printf("%+v", err)
b, _ := json.Marshal(NewFailure(err.Error()))
http.Error(w, string(b), http.StatusBadRequest)
return
}
case "DELETE":
cat := category{}
err := json.NewDecoder(req.Body).Decode(&cat)
req.Body.Close()
if err != nil {
b, _ := json.Marshal(NewFailure(err.Error()))
http.Error(w, string(b), http.StatusBadRequest)
return
}
_, err = s.db.db.Exec("DELETE FROM categories WHERE name = $1", cat.Name)
if err != nil {
log.Printf("%+v", err)
b, _ := json.Marshal(NewFailure(err.Error()))
http.Error(w, string(b), http.StatusInternalServerError)
return
}
if err != nil {
log.Printf("%+v", err)
b, _ := json.Marshal(NewFailure(err.Error()))
http.Error(w, string(b), http.StatusBadRequest)
return
}
}
}

33
db.go
View File

@ -37,6 +37,23 @@ func (d *DB) initializeDB() {
d.db.Exec(primeCategories)
}
func (d *DB) getCategories() ([]category, error) {
results := []category{}
rows, err := d.db.Queryx("SELECT id, name, budget FROM categories")
if err != nil {
return nil, err
}
for rows.Next() {
var result category
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
@ -175,22 +192,6 @@ func (d *DB) initializeDB() {
// Name string `json:"name"db:"name"`
//}
//
//func (d *DB) getCategories() (map[string]int, error) {
// results := map[string]int{}
// rows, err := d.db.Queryx("SELECT id, name FROM categories")
// if err != nil {
// return nil, err
// }
// for rows.Next() {
// var result Category
// err := rows.StructScan(&result)
// if err != nil {
// return nil, err
// }
// results[result.Name] = result.Id
// }
// return results, nil
//}
//
//func (d *DB) flagPic(filename string, reporter string) (int, error) {
// logs.Debug("(%s, %s)", filename, reporter)

View File

@ -23,6 +23,7 @@ func addRoutes(sm *http.ServeMux, server *Server, staticFiles string) {
"auth": "/api/v0/auth/",
"health": "/healthz",
"list": "/api/v0/list/",
"category": "/api/v0/category/",
"tranx": "/api/v0/tranx/",
"cost": "/api/v0/costpermonth/",
@ -72,6 +73,7 @@ func addRoutes(sm *http.ServeMux, server *Server, staticFiles string) {
//sm.HandleFunc(prefix["list"], server.listUsers)
//sm.HandleFunc(prefix["tranx"], server.tranx)
//sm.HandleFunc(prefix["cost"], server.costPerMonth)
sm.HandleFunc(prefix["category"], server.category)
sm.HandleFunc(prefix["protected"], server.plist)
sm.HandleFunc(prefix["info"], server.serverInfo)
sm.HandleFunc(prefix["login"], server.login)