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
3 changed files with 98 additions and 16 deletions
+79
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
}
}
}