plumbed through child add/delete subcommands

This commit is contained in:
sm
2015-08-09 16:07:39 -07:00
parent 8e5f559567
commit 5957437ded
5 changed files with 107 additions and 53 deletions
+15 -9
View File
@@ -9,6 +9,8 @@ import (
"strings"
"github.com/gorilla/sessions"
"mcquay.me/allowances/children"
"mcquay.me/fs"
)
func init() {
@@ -28,7 +30,8 @@ func NewFailure(msg string) *failure {
}
type Allowances struct {
db string
db children.DB
dbfile string
hashes Passes
store *sessions.CookieStore
}
@@ -47,11 +50,16 @@ func NewAllowances(sm *http.ServeMux, dbfile, passfile, staticFiles string) (*Al
return nil, err
}
if !Exists(dbfile) {
if !fs.Exists(dbfile) {
return nil, fmt.Errorf("child db file doesn't exist: %q", dbfile)
}
db, err := children.Load(dbfile)
if err != nil {
return nil, err
}
r := &Allowances{
db: dbfile,
db: db,
dbfile: dbfile,
hashes: hashes,
store: sessions.NewCookieStore([]byte("hello world")),
}
@@ -60,8 +68,7 @@ func NewAllowances(sm *http.ServeMux, dbfile, passfile, staticFiles string) (*Al
}
func (a *Allowances) home(w http.ResponseWriter, req *http.Request, uid string) error {
children := loadChildren(a.db)
tmpls["home"].Execute(w, map[string]interface{}{"children": children})
tmpls["home"].Execute(w, map[string]interface{}{"children": a.db})
return nil
}
@@ -102,12 +109,11 @@ func (a *Allowances) add(w http.ResponseWriter, req *http.Request, uid string) e
if err != nil {
return fmt.Errorf("couldn't parse a dollar amount: %v", err)
}
children := loadChildren(a.db)
children[child] += amount
defer dumpChildren(a.db, children)
a.db[child] += amount
a.db.Save(a.dbfile)
w.Header().Set("Content-Type", "application/json")
b, err := json.Marshal(map[string]interface{}{
"amount": dollarize(children[child]),
"amount": dollarize(a.db[child]),
"name": child,
})
if err != nil {