forked from bp-alex/ibconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount_handler.go
More file actions
97 lines (83 loc) · 2.55 KB
/
account_handler.go
File metadata and controls
97 lines (83 loc) · 2.55 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package server
import (
"database/sql"
"fmt"
"net/http"
"time"
"github.com/ant0ine/go-json-rest/rest"
"github.com/benalexau/ibconnect/core"
"github.com/russross/meddler"
)
type AccountHandler struct {
db *sql.DB
n *core.Notifier
u *Util
}
type AccountReport struct {
AccountCode string
Timestamp string
Balance core.AccountAmountView
Positions []*core.AccountPositionView
}
func (a *AccountHandler) GetAll(w rest.ResponseWriter, r *rest.Request) {
err := RefreshIfNeeded(a.n, r, core.NtAccountRefresh, core.NtAccountFeedDone, 15*time.Second)
if err != nil {
a.u.HandleError(err, w, r)
return
}
var accounts []*core.Account
err = meddler.QueryAll(a.db, &accounts, "SELECT * FROM account")
if err != nil {
a.u.HandleError(err, w, r)
return
}
w.Header().Add("Cache-Control", "private, max-age=60")
w.WriteJson(&accounts)
}
func (a *AccountHandler) GetLatest(w rest.ResponseWriter, r *rest.Request) {
code := r.PathParam("accountCode")
latest := new(core.AccountSnapshotLatest)
err := meddler.QueryRow(a.db, latest, "SELECT * FROM v_account_snapshot_latest WHERE account_code = $1", code)
if err != nil {
a.u.HandleError(err, w, r)
return
}
path := fmt.Sprintf("/v1/accounts/%s/%s", code, latest.Latest.Format(time.RFC3339Nano))
url := r.UrlFor(path, make(map[string][]string))
w.Header().Add("Location", url.String())
w.WriteHeader(http.StatusSeeOther)
}
func (a *AccountHandler) GetReport(w rest.ResponseWriter, r *rest.Request) {
var report AccountReport
report.AccountCode = r.PathParam("accountCode")
report.Timestamp = r.PathParam("timestamp")
existing := new(core.Account)
err := meddler.QueryRow(a.db, existing, "SELECT * FROM account WHERE account_code = $1", report.AccountCode)
if err != nil {
a.u.HandleError(err, w, r)
return
}
created, err := time.Parse(time.RFC3339Nano, report.Timestamp)
if err != nil {
a.u.HandleError(err, w, r)
return
}
var snap core.AccountSnapshot
err = meddler.QueryRow(a.db, &snap, "SELECT * FROM account_snapshot WHERE account_id = $1 AND created = $2", existing.Id, created)
if err != nil {
a.u.HandleError(err, w, r)
return
}
err = meddler.QueryAll(a.db, &report.Positions, "SELECT * FROM v_account_position WHERE account_snapshot_id = $1", snap.Id)
if err != nil {
a.u.HandleError(err, w, r)
return
}
err = meddler.QueryRow(a.db, &report.Balance, "SELECT * FROM v_account_amount WHERE account_snapshot_id = $1", snap.Id)
if err != nil {
a.u.HandleError(err, w, r)
return
}
w.Header().Add("Cache-Control", "private, max-age=31556926")
w.WriteJson(&report)
}