-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd-tx.go
More file actions
74 lines (64 loc) · 1.48 KB
/
cmd-tx.go
File metadata and controls
74 lines (64 loc) · 1.48 KB
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
package main
import (
"fmt"
"strconv"
"time"
)
type txCmd struct {
Amount float64
}
func (txCmd) Command() string {
return "tx"
}
func (txCmd) Description() string {
return "Manages Transactions for the current crew"
}
func (txCmd) Help(args []string) {
output(func(print printer) {
print("tx ls - lists all transactions and the balance")
print("tx new VALUE - creates a new transaction")
})
}
func (cmd txCmd) Execute(args []string) error {
if activeCrewID == 0 {
return nil
}
if args[0] == "ls" {
return cmd.ls(args[1:])
} else if args[0] == "new" {
return cmd.new(args[1:])
}
return nil
}
func (txCmd) ls(args []string) error {
var transactions []transaction
filter := transaction{CrewID: activeCrewID}
database.Where(&filter).Order("date asc").Find(&transactions)
var balance float64
output(func(print printer) {
for _, tx := range transactions {
print(fmt.Sprintf("%.2f - %s", tx.Value, tx.Description))
balance += tx.Value
}
print("---")
print(fmt.Sprintf("Balance: %.2f", balance))
})
return nil
}
func (cmd txCmd) new(args []string) error {
cmd.Amount, _ = strconv.ParseFloat(args[0], 64)
var casted cmdlinesink = cmd
inputfocus = &casted
output(func(print printer) {
print("Description?")
})
return nil
}
func (cmd txCmd) TextEntered(data string) error {
output(func(print printer) {
print(data)
})
tx := transaction{Date: int(time.Now().Unix()), CrewID: activeCrewID, Value: cmd.Amount, Description: data}
database.Create(&tx)
return nil
}