Browse Source

Got app working

master
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 @@
package main

type Child struct {
Name string
Money int
}

+ 12
- 1
db.go View File

@@ -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
}

+ 23
- 0
handlers.go View File

@@ -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)
}

+ 3
- 0
main.go View File

@@ -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 {

+ 4
- 3
static/allowances.js View File

@@ -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"]);
});
});
});

+ 7
- 7
templates/index.html View File

@@ -15,18 +15,18 @@
</tr>
</thead>
<tbody>
{{ range .children }}
{{ range $name, $money := .children }}
<tr class="child">
<td class="name">{{ .Name }}</td>
<td>{{ dollarize .Money }}</td>
<td class="name {{ $name }}">{{ $name }}</td>
<td class="amount">{{ dollarize $money }}</td>
</tr>
<tr class="controls">
<td colspan=2 style="text-align: center;">
<div class="btn-group">
<button class="btn" value=100>$1.00</button>
<button class="btn" value=25>25&cent;</button>
<button class="btn" value=10>10&cent;</button>
<button class="btn" value=5>5&cent;</button>
<button class="btn" value="/add/{{ $name}}/100">$1.00</button>
<button class="btn" value="/add/{{ $name}}/25">25&cent;</button>
<button class="btn" value="/add/{{ $name}}/10">10&cent;</button>
<button class="btn" value="/add/{{ $name}}/5">5&cent;</button>
</div>
</td>
</tr>

Loading…
Cancel
Save