-
Notifications
You must be signed in to change notification settings - Fork 0
/
marbles.go
193 lines (172 loc) · 4.14 KB
/
marbles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"fmt"
"github.com/golang/protobuf/proto"
pb "github.com/sourcequench/marbles/proto"
"html/template"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
)
var templates = template.Must(template.ParseFiles("index.html", "ledger.html"))
// Transaction type enum
type transType int32
const (
credit transType = iota
debit
)
// Person name enum
type person int32
const (
josie person = iota
audra
)
type Ledger struct {
log []Transaction
}
// Transaction
type Transaction struct {
marbles int32
ttype transType
account person
time int64
humanTime string
desc string
merit bool
}
func (t *Transaction) save() error {
tl, err := readLog()
if err != nil {
log.Fatal("got error reading log")
}
tl.Transactions = append(tl.Transactions, t.toProto())
out := proto.MarshalTextString(tl)
fmt.Println("writing file")
if err := ioutil.WriteFile("transactions.pb", []byte(out), 0644); err != nil {
log.Fatalln("Failed to write transaction log:", err)
}
return nil
}
func (t *Transaction) toProto() *pb.Transaction {
trans := &pb.Transaction{
Marbles: proto.Int32(t.marbles),
Time: proto.Int64(t.time),
}
if t.desc != "" {
trans.Description = proto.String(t.desc)
}
// Handle credit vs debit
switch t.ttype {
case credit:
trans.Type = pb.Transaction_CREDIT.Enum()
case debit:
trans.Type = pb.Transaction_DEBIT.Enum()
}
// Handle credit vs debit
switch t.account {
case josie:
trans.Account = pb.Transaction_JOSIE.Enum()
case audra:
trans.Account = pb.Transaction_AUDRA.Enum()
}
return trans
}
func readLog() (*pb.TransactionLog, error) {
filename := "transactions.pb"
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
tl := &pb.TransactionLog{}
if err := proto.UnmarshalText(string(data), tl); err != nil {
return nil, err
}
return tl, nil
}
func renderTemplate(w http.ResponseWriter, tmpl string, i interface{}) {
err := templates.ExecuteTemplate(w, tmpl+".html", i)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func marbleHandler(w http.ResponseWriter, r *http.Request) {
tl, err := readLog()
if err != nil {
log.Fatal("got error reading log")
}
bal := Balance(tl)
movies := bal / 90
// Struct literal to fulfill interface{}.
b := struct{ Balance, Movies int32 }{Balance: bal, Movies: movies}
renderTemplate(w, "index", b)
}
func ledgerHandler(w http.ResponseWriter, r *http.Request) {
tl, err := readLog()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
l := Ledger{}
for _, t := range tl.GetTransactions() {
tTime := time.Unix(*t.Time, 0)
tString := tTime.Format("Sat Mar 7 11:06AM 2015")
trans := Transaction{
marbles: *t.Marbles,
humanTime: tString,
}
if t.Description != nil {
trans.desc = *t.Description
}
l.log = append(l.log, trans)
}
renderTemplate(w, "ledger", tl)
}
func saveHandler(w http.ResponseWriter, r *http.Request) {
mc, err := strconv.ParseInt(r.FormValue("marblecount"), 10, 32)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
p, err := strconv.ParseInt(r.FormValue("person"), 10, 32)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
t := time.Now()
trans := &Transaction{
marbles: int32(mc),
account: person(int32(p)),
time: t.Unix(),
humanTime: t.String(),
ttype: debit,
desc: r.FormValue("description"),
}
fmt.Println("trying to save")
err = trans.save()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("saved")
http.Redirect(w, r, "/", http.StatusFound)
}
func Balance(tl *pb.TransactionLog) int32 {
var balance int32
for _, t := range tl.GetTransactions() {
switch *t.Type {
case pb.Transaction_CREDIT:
balance += *t.Marbles
case pb.Transaction_DEBIT:
balance -= *t.Marbles
}
}
return balance
}
func main() {
http.HandleFunc("/", marbleHandler)
http.HandleFunc("/save/", saveHandler)
http.HandleFunc("/ledger/", ledgerHandler)
http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("img"))))
log.Fatal(http.ListenAndServe(":9999", nil))
}