Got app working
This commit is contained in:
+23
@@ -1,7 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func homeHandler(w http.ResponseWriter, req *http.Request) {
|
||||
@@ -35,3 +39,22 @@ func logoutHandler(w http.ResponseWriter, req *http.Request) {
|
||||
http.Redirect(w, req, "/", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
func addHandler(w http.ResponseWriter, req *http.Request) {
|
||||
path := req.URL.Path[len(addPath):]
|
||||
bits := strings.Split(path, "/")
|
||||
child := bits[0]
|
||||
amount, err := strconv.Atoi(bits[1])
|
||||
if err != nil {
|
||||
log.Fatal("couldn't parse a dollar amount", err)
|
||||
}
|
||||
children := loadChildren(*db_file)
|
||||
children[child] += amount
|
||||
defer dumpChildren(*db_file, children)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
b, err := json.Marshal(map[string]interface{}{
|
||||
"amount": dollarize(children[child]),
|
||||
"name": child,
|
||||
})
|
||||
w.Write(b)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user