From 2970bbbbe85cb52da8cc2b6e0ea39ace076af259 Mon Sep 17 00:00:00 2001 From: koebi Date: Sat, 24 Aug 2019 13:53:32 +0200 Subject: [PATCH 1/2] allow user to deposit money --- http.go | 57 +++++++++++++++++++++++++++++++++++++++ main.go | 19 +++++++++++++ templates/addBalance.html | 36 +++++++++++++++++++++++++ templates/dashboard.html | 2 ++ 4 files changed, 114 insertions(+) create mode 100644 templates/addBalance.html diff --git a/http.go b/http.go index 2836ba4..55728d8 100644 --- a/http.go +++ b/http.go @@ -3,6 +3,7 @@ package main import ( "bytes" "net/http" + "strconv" "github.com/gorilla/mux" ) @@ -203,6 +204,60 @@ func (k *Kasse) GetLogout(res http.ResponseWriter, req *http.Request) { } } +func (k *Kasse) GetAddBalancePage(res http.ResponseWriter, req *http.Request) { + session, err := k.sessions.Get(req, "nnev-kasse") + if err != nil { + http.Redirect(res, req, "/login.html", 302) + return + } + ui, ok := session.Values["user"] + if !ok { + http.Redirect(res, req, "/login.html", 302) + return + } + user := ui.(User) + + data := struct { + User User + }{ + User: user, + } + + res.Header().Set("Content-Type", "text/html") + + if err := ExecuteTemplate(res, TemplateInput{Title: "Add Balance", Body: "addBalance.html", Data: data}); err != nil { + k.log.Println("Could not render template:", err) + http.Error(res, "Internal error", http.StatusInternalServerError) + return + } +} + +func (k *Kasse) PostAddBalance(res http.ResponseWriter, req *http.Request) { + deposit, err := strconv.ParseInt(req.FormValue("deposit"), 10, 64) + + session, err := k.sessions.Get(req, "nnev-kasse") + if err != nil { + http.Redirect(res, req, "/login.html", 302) + return + } + ui, ok := session.Values["user"] + if !ok { + http.Redirect(res, req, "/login.html", 302) + return + } + user := ui.(User) + + err = k.AddBalance(user, deposit) + if err != nil { + k.log.Println("Could not deposit money:", err) + http.Error(res, "Internal error", http.StatusInternalServerError) + return + } + + http.Redirect(res, req, "/", 302) + +} + // Handler returns a http.Handler for the webinterface. func (k *Kasse) Handler() http.Handler { r := mux.NewRouter() @@ -213,5 +268,7 @@ func (k *Kasse) Handler() http.Handler { r.Methods("GET").Path("/logout.html").HandlerFunc(k.GetLogout) r.Methods("GET").Path("/create_user.html").HandlerFunc(k.GetNewUserPage) r.Methods("POST").Path("/create_user.html").HandlerFunc(k.PostNewUserPage) + r.Methods("GET").Path("/addBalance.html").HandlerFunc(k.GetAddBalancePage) + r.Methods("POST").Path("/addBalance.html").HandlerFunc(k.PostAddBalance) return r } diff --git a/main.go b/main.go index 75c62cb..04aafae 100644 --- a/main.go +++ b/main.go @@ -366,6 +366,25 @@ func (k *Kasse) GetBalance(user User) (int64, error) { return b.Int64, nil } +func (k *Kasse) AddBalance(user User, deposit int64) error { + k.log.Printf("Adding balance %d for owner %s", deposit, user.Name) + + tx, err := k.db.Beginx() + if err != nil { + return err + } + defer tx.Rollback() + + if _, err := tx.Exec(`INSERT INTO transactions (user_id, card_id, time, amount, kind) VALUES ($1, NULL, $3, $4, $5)`, user.ID, time.Now(), deposit*100, "Aufladung"); err != nil { + return err + } + + if err := tx.Commit(); err != nil { + return err + } + return nil +} + // GetTransactions gets the last n transactions for a given user. If n ≤ 0, all // transactions are returnsed. func (k *Kasse) GetTransactions(user User, n int) ([]Transaction, error) { diff --git a/templates/addBalance.html b/templates/addBalance.html new file mode 100644 index 0000000..977bb27 --- /dev/null +++ b/templates/addBalance.html @@ -0,0 +1,36 @@ + + +
+
+
+
+

Adding Balance for {{ .User.Name }}

+
+
+ Wie viel Geld (in Euro) möchtest du einzahlen? +
+
+
+
+ + +
+
+
+ +
+
+ +
+
+
+ diff --git a/templates/dashboard.html b/templates/dashboard.html index fbb9ff5..44be2e0 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -9,9 +9,11 @@

{{ .User.Name }}

{{ .Balance }}€
+ +
From 4245144bdc0ff5946218749ea5eaf1885898b2fe Mon Sep 17 00:00:00 2001 From: koebi Date: Thu, 29 Aug 2019 21:59:52 +0200 Subject: [PATCH 2/2] show Aufladung instead of nothing in transactions --- templates/dashboard.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/dashboard.html b/templates/dashboard.html index 44be2e0..25df8ed 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -75,7 +75,7 @@

Letzte Transaktionen

{{ range .Transactions }} - {{ printf "%x" .Card }} + {{if .Card}} {{ printf "%x" .Card }} {{else}} {{`Aufladung`}} {{end}} {{ toEuros .Amount }}€