Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aufladen #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"net/http"
"strconv"

"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -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()
Expand All @@ -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
}
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
36 changes: 36 additions & 0 deletions templates/addBalance.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<style>
.mdl-card__actions {
display: flex;
align-items: center;
}
</style>

<div class="mdl-grid">
<div class="mdl-cell mdl-cell--4-col mdl-cell--4-offset">
<div class="mdl-card mdl-shadow--2dp" id="add-card-box">
<div class="mdl-card__title">
<h2 class="mdl-card__title-text">Adding Balance for {{ .User.Name }}</h2>
</div>
<div class="mdl-card__supporting-text">
Wie viel Geld (in Euro) möchtest du einzahlen?
</div>
<div class="mdl-card__supporting-text">
<form method="POST">
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="text"
pattern="-?[0-9]*(\.[0-9]+)?" name="deposit" />
<label class="mdl-textfield__label" for="deposit">Guthaben</label>
</div>
<div class="mdl-card__actions">
<div class="mdl-layout-spacer"></div>
<button class="mdl-button mdl-button--colored mdl-js-button
mdl-js-ripple-effect" type="submit">
Aufladen
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
4 changes: 3 additions & 1 deletion templates/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ <h2 class="mdl-card__title-text">{{ .User.Name }}</h2>
<div>{{ .Balance }}€</div>
</div>
<div class="mdl-card__actions mdl-card--border">
<a href="/addBalance.html">
<button class="mdl-button mdl-button--accent mdl-jso-button mdl-js-ripple-effect">
Aufladen
</button>
</a>
</div>
</div>
</div>
Expand Down Expand Up @@ -73,7 +75,7 @@ <h2 class="mdl-card__title-text">Letzte Transaktionen</h2>
<tbody>
{{ range .Transactions }}
<tr>
<td class="mdl-data-table__cell--non-numeric">{{ printf "%x" .Card }}</td>
<td class="mdl-data-table__cell--non-numeric">{{if .Card}} {{ printf "%x" .Card }} {{else}} {{`Aufladung`}} {{end}}</td>
<td class="mdl-data-table__cell--non-numeric"><time>{{ .Time.Format "2006-01-02 15:04" }}</time></td>
<td class="mdl-data-table__cell--non-numeric">{{ toEuros .Amount }}€</td>
</tr>
Expand Down