From d48fd3f852e2d092b3a13a3350e82c18fdc78089 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Fri, 8 Mar 2013 22:28:24 -0800 Subject: [PATCH] Got app working --- children.go | 6 ------ db.go | 13 ++++++++++++- handlers.go | 23 +++++++++++++++++++++++ main.go | 3 +++ static/allowances.js | 7 ++++--- templates/index.html | 14 +++++++------- 6 files changed, 49 insertions(+), 17 deletions(-) delete mode 100644 children.go diff --git a/children.go b/children.go deleted file mode 100644 index 712a809..0000000 --- a/children.go +++ /dev/null @@ -1,6 +0,0 @@ -package main - -type Child struct { - Name string - Money int -} diff --git a/db.go b/db.go index 4f3cc9e..524cecc 100644 --- a/db.go +++ b/db.go @@ -53,7 +53,7 @@ func check_password(filename, attempt string) (result bool) { return } -func loadChildren(filename string) (children []Child) { +func loadChildren(filename string) (children map[string]int) { dbMutex.RLock() defer dbMutex.RUnlock() b, err := ioutil.ReadFile(filename) @@ -66,3 +66,14 @@ func loadChildren(filename string) (children []Child) { } return } + +func dumpChildren(filename string, children map[string]int) { + dbMutex.Lock() + defer dbMutex.Unlock() + b, err := json.Marshal(children) + err = ioutil.WriteFile(filename, b, 0644) + if err != nil { + log.Fatal("serious issue writing children db file", err) + } + return +} diff --git a/handlers.go b/handlers.go index bd3f901..6d789fd 100644 --- a/handlers.go +++ b/handlers.go @@ -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) +} diff --git a/main.go b/main.go index d9058ef..f23eb56 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,8 @@ var template_dir = flag.String("templates", "templates", "template dir") var add_pw = flag.String("passwd", "", "add this pass to the db") var check_pw = flag.String("checkpw", "", "check if this pw is in db") +var addPath = "/add/" + var store = sessions.NewCookieStore([]byte("hello world")) var templates *template.Template @@ -31,6 +33,7 @@ func main() { http.HandleFunc("/", homeHandler) http.HandleFunc("/login", loginHandler) http.HandleFunc("/logout", logoutHandler) + http.HandleFunc(addPath, addHandler) http.Handle("/s/", http.StripPrefix("/s/", http.FileServer(http.Dir(*static_files)))) if err := http.ListenAndServe(*addr, nil); err != nil { diff --git a/static/allowances.js b/static/allowances.js index 42e9f2c..5afa6cb 100644 --- a/static/allowances.js +++ b/static/allowances.js @@ -4,8 +4,9 @@ $(function() { $(this).next(".controls").toggle(); }); $(".btn").click(function(e) { - var amount = $(this)[0].value; - var name = $(this).parent().parent().parent().prev().find(".name").text(); - console.log(name + " " + amount); + var target = $(this).val(); + var req = $.get(target, function(data) { + $("." + data["name"]).next().text(data["amount"]); + }); }); }); diff --git a/templates/index.html b/templates/index.html index 81717c9..e210d8f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -15,18 +15,18 @@ - {{ range .children }} + {{ range $name, $money := .children }} - {{ .Name }} - {{ dollarize .Money }} + {{ $name }} + {{ dollarize $money }}
- - - - + + + +