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:
parent
95e85aa011
commit
d168a117d2
79
api.go
Normal file
79
api.go
Normal 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
33
db.go
@ -37,6 +37,23 @@ func (d *DB) initializeDB() {
|
|||||||
d.db.Exec(primeCategories)
|
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 {
|
//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
|
||||||
@ -175,22 +192,6 @@ func (d *DB) initializeDB() {
|
|||||||
// Name string `json:"name"db:"name"`
|
// 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) {
|
//func (d *DB) flagPic(filename string, reporter string) (int, error) {
|
||||||
// logs.Debug("(%s, %s)", filename, reporter)
|
// logs.Debug("(%s, %s)", filename, reporter)
|
||||||
|
@ -23,6 +23,7 @@ func addRoutes(sm *http.ServeMux, server *Server, staticFiles string) {
|
|||||||
"auth": "/api/v0/auth/",
|
"auth": "/api/v0/auth/",
|
||||||
"health": "/healthz",
|
"health": "/healthz",
|
||||||
"list": "/api/v0/list/",
|
"list": "/api/v0/list/",
|
||||||
|
"category": "/api/v0/category/",
|
||||||
"tranx": "/api/v0/tranx/",
|
"tranx": "/api/v0/tranx/",
|
||||||
"cost": "/api/v0/costpermonth/",
|
"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["list"], server.listUsers)
|
||||||
//sm.HandleFunc(prefix["tranx"], server.tranx)
|
//sm.HandleFunc(prefix["tranx"], server.tranx)
|
||||||
//sm.HandleFunc(prefix["cost"], server.costPerMonth)
|
//sm.HandleFunc(prefix["cost"], server.costPerMonth)
|
||||||
|
sm.HandleFunc(prefix["category"], server.category)
|
||||||
sm.HandleFunc(prefix["protected"], server.plist)
|
sm.HandleFunc(prefix["protected"], server.plist)
|
||||||
sm.HandleFunc(prefix["info"], server.serverInfo)
|
sm.HandleFunc(prefix["info"], server.serverInfo)
|
||||||
sm.HandleFunc(prefix["login"], server.login)
|
sm.HandleFunc(prefix["login"], server.login)
|
||||||
|
Loading…
Reference in New Issue
Block a user