Browse Source

Got app working

Stephen McQuay 6 years ago
parent
commit
d48fd3f852
6 changed files with 49 additions and 17 deletions
  1. 0
    6
      children.go
  2. 12
    1
      db.go
  3. 23
    0
      handlers.go
  4. 3
    0
      main.go
  5. 4
    3
      static/allowances.js
  6. 7
    7
      templates/index.html

+ 0
- 6
children.go View File

@@ -1,6 +0,0 @@
1
-package main
2
-
3
-type Child struct {
4
-	Name  string
5
-	Money int
6
-}

+ 12
- 1
db.go View File

@@ -53,7 +53,7 @@ func check_password(filename, attempt string) (result bool) {
53 53
 	return
54 54
 }
55 55
 
56
-func loadChildren(filename string) (children []Child) {
56
+func loadChildren(filename string) (children map[string]int) {
57 57
 	dbMutex.RLock()
58 58
 	defer dbMutex.RUnlock()
59 59
 	b, err := ioutil.ReadFile(filename)
@@ -66,3 +66,14 @@ func loadChildren(filename string) (children []Child) {
66 66
 	}
67 67
 	return
68 68
 }
69
+
70
+func dumpChildren(filename string, children map[string]int) {
71
+	dbMutex.Lock()
72
+	defer dbMutex.Unlock()
73
+    b, err := json.Marshal(children)
74
+	err = ioutil.WriteFile(filename, b, 0644)
75
+	if err != nil {
76
+		log.Fatal("serious issue writing children db file", err)
77
+	}
78
+	return
79
+}

+ 23
- 0
handlers.go View File

@@ -1,7 +1,11 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"encoding/json"
5
+	"log"
4 6
 	"net/http"
7
+	"strconv"
8
+	"strings"
5 9
 )
6 10
 
7 11
 func homeHandler(w http.ResponseWriter, req *http.Request) {
@@ -35,3 +39,22 @@ func logoutHandler(w http.ResponseWriter, req *http.Request) {
35 39
 	http.Redirect(w, req, "/", http.StatusSeeOther)
36 40
 	return
37 41
 }
42
+
43
+func addHandler(w http.ResponseWriter, req *http.Request) {
44
+	path := req.URL.Path[len(addPath):]
45
+	bits := strings.Split(path, "/")
46
+	child := bits[0]
47
+	amount, err := strconv.Atoi(bits[1])
48
+	if err != nil {
49
+		log.Fatal("couldn't parse a dollar amount", err)
50
+	}
51
+	children := loadChildren(*db_file)
52
+	children[child] += amount
53
+	defer dumpChildren(*db_file, children)
54
+	w.Header().Set("Content-Type", "application/json")
55
+    b, err := json.Marshal(map[string]interface{}{
56
+        "amount": dollarize(children[child]),
57
+        "name": child,
58
+    })
59
+	w.Write(b)
60
+}

+ 3
- 0
main.go View File

@@ -17,6 +17,8 @@ var template_dir = flag.String("templates", "templates", "template dir")
17 17
 var add_pw = flag.String("passwd", "", "add this pass to the db")
18 18
 var check_pw = flag.String("checkpw", "", "check if this pw is in db")
19 19
 
20
+var addPath = "/add/"
21
+
20 22
 var store = sessions.NewCookieStore([]byte("hello world"))
21 23
 var templates *template.Template
22 24
 
@@ -31,6 +33,7 @@ func main() {
31 33
 		http.HandleFunc("/", homeHandler)
32 34
 		http.HandleFunc("/login", loginHandler)
33 35
 		http.HandleFunc("/logout", logoutHandler)
36
+		http.HandleFunc(addPath, addHandler)
34 37
 		http.Handle("/s/", http.StripPrefix("/s/",
35 38
 			http.FileServer(http.Dir(*static_files))))
36 39
 		if err := http.ListenAndServe(*addr, nil); err != nil {

+ 4
- 3
static/allowances.js View File

@@ -4,8 +4,9 @@ $(function() {
4 4
         $(this).next(".controls").toggle();
5 5
     });
6 6
     $(".btn").click(function(e) {
7
-        var amount = $(this)[0].value;
8
-        var name = $(this).parent().parent().parent().prev().find(".name").text();
9
-        console.log(name + " " + amount);
7
+        var target = $(this).val();
8
+        var req = $.get(target, function(data) {
9
+            $("." + data["name"]).next().text(data["amount"]);
10
+        });
10 11
     });
11 12
 });

+ 7
- 7
templates/index.html View File

@@ -15,18 +15,18 @@
15 15
                 </tr>
16 16
             </thead>
17 17
             <tbody>
18
-                {{ range .children }}
18
+                {{ range $name, $money := .children }}
19 19
                 <tr class="child">
20
-                    <td class="name">{{ .Name }}</td>
21
-                    <td>{{ dollarize .Money }}</td>
20
+                    <td class="name {{ $name }}">{{ $name }}</td>
21
+                    <td class="amount">{{ dollarize $money }}</td>
22 22
                 </tr>
23 23
                 <tr class="controls">
24 24
                     <td colspan=2 style="text-align: center;">
25 25
                         <div class="btn-group">
26
-                            <button class="btn" value=100>$1.00</button>
27
-                            <button class="btn" value=25>25&cent;</button>
28
-                            <button class="btn" value=10>10&cent;</button>
29
-                            <button class="btn" value=5>5&cent;</button>
26
+                            <button class="btn" value="/add/{{ $name}}/100">$1.00</button>
27
+                            <button class="btn" value="/add/{{ $name}}/25">25&cent;</button>
28
+                            <button class="btn" value="/add/{{ $name}}/10">10&cent;</button>
29
+                            <button class="btn" value="/add/{{ $name}}/5">5&cent;</button>
30 30
                         </div>
31 31
                     </td>
32 32
                 </tr>

Loading…
Cancel
Save