From 32aef11da58b0b8171484ebd109399337d094c6c Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Sat, 9 Dec 2017 05:56:45 -0500 Subject: [PATCH] Finish 'store' separation --- internal/handlers/accounts.go | 9 ++-- internal/handlers/accounts_lua.go | 6 +-- internal/handlers/handlers.go | 32 +------------ internal/handlers/imports.go | 4 +- internal/handlers/prices.go | 10 ++--- internal/handlers/reports.go | 70 +++++------------------------ internal/handlers/securities.go | 5 +-- internal/handlers/securities_lua.go | 6 +-- internal/handlers/sessions.go | 8 ++-- internal/handlers/transactions.go | 5 +-- internal/handlers/tx.go | 14 ------ internal/handlers/users.go | 2 +- internal/models/reports.go | 2 +- internal/store/db/reports.go | 56 +++++++++++++++++++++++ internal/store/store.go | 5 +++ 15 files changed, 100 insertions(+), 134 deletions(-) delete mode 100644 internal/handlers/tx.go create mode 100644 internal/store/db/reports.go diff --git a/internal/handlers/accounts.go b/internal/handlers/accounts.go index 60a2199..7f2cc23 100644 --- a/internal/handlers/accounts.go +++ b/internal/handlers/accounts.go @@ -4,14 +4,13 @@ import ( "errors" "github.com/aclindsa/moneygo/internal/models" "github.com/aclindsa/moneygo/internal/store" - "github.com/aclindsa/moneygo/internal/store/db" "log" "net/http" ) // Get (and attempt to create if it doesn't exist). Matches on UserId, // SecurityId, Type, Name, and ParentAccountId -func GetCreateAccount(tx *db.Tx, a models.Account) (*models.Account, error) { +func GetCreateAccount(tx store.Tx, a models.Account) (*models.Account, error) { var account models.Account accounts, err := tx.FindMatchingAccounts(&a) @@ -27,7 +26,7 @@ func GetCreateAccount(tx *db.Tx, a models.Account) (*models.Account, error) { account.Name = a.Name account.ParentAccountId = a.ParentAccountId - err = tx.Insert(&account) + err = tx.InsertAccount(&account) if err != nil { return nil, err } @@ -37,7 +36,7 @@ func GetCreateAccount(tx *db.Tx, a models.Account) (*models.Account, error) { // Get (and attempt to create if it doesn't exist) the security/currency // trading account for the supplied security/currency -func GetTradingAccount(tx *db.Tx, userid int64, securityid int64) (*models.Account, error) { +func GetTradingAccount(tx store.Tx, userid int64, securityid int64) (*models.Account, error) { var tradingAccount models.Account var account models.Account @@ -79,7 +78,7 @@ func GetTradingAccount(tx *db.Tx, userid int64, securityid int64) (*models.Accou // Get (and attempt to create if it doesn't exist) the security/currency // imbalance account for the supplied security/currency -func GetImbalanceAccount(tx *db.Tx, userid int64, securityid int64) (*models.Account, error) { +func GetImbalanceAccount(tx store.Tx, userid int64, securityid int64) (*models.Account, error) { var imbalanceAccount models.Account var account models.Account xxxtemplate := FindSecurityTemplate("XXX", models.Currency) diff --git a/internal/handlers/accounts_lua.go b/internal/handlers/accounts_lua.go index 2985ff5..6d135a6 100644 --- a/internal/handlers/accounts_lua.go +++ b/internal/handlers/accounts_lua.go @@ -4,7 +4,7 @@ import ( "context" "errors" "github.com/aclindsa/moneygo/internal/models" - "github.com/aclindsa/moneygo/internal/store/db" + "github.com/aclindsa/moneygo/internal/store" "github.com/yuin/gopher-lua" "strings" ) @@ -16,7 +16,7 @@ func luaContextGetAccounts(L *lua.LState) (map[int64]*models.Account, error) { ctx := L.Context() - tx, ok := ctx.Value(dbContextKey).(*db.Tx) + tx, ok := ctx.Value(dbContextKey).(store.Tx) if !ok { return nil, errors.New("Couldn't find tx in lua's Context") } @@ -150,7 +150,7 @@ func luaAccountBalance(L *lua.LState) int { a := luaCheckAccount(L, 1) ctx := L.Context() - tx, ok := ctx.Value(dbContextKey).(*db.Tx) + tx, ok := ctx.Value(dbContextKey).(store.Tx) if !ok { panic("Couldn't find tx in lua's Context") } diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 47a58b6..88a11fd 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -17,8 +17,7 @@ type ResponseWriterWriter interface { } type Context struct { - Tx *db.Tx - StoreTx store.Tx + Tx store.Tx User *models.User remainingURL string // portion of URL path not yet reached in the hierarchy } @@ -52,33 +51,6 @@ type APIHandler struct { } func (ah *APIHandler) txWrapper(h Handler, r *http.Request, context *Context) (writer ResponseWriterWriter) { - tx, err := GetTx(ah.Store.DbMap) - if err != nil { - log.Print(err) - return NewError(999 /*Internal Error*/) - } - defer func() { - if r := recover(); r != nil { - tx.Rollback() - panic(r) - } - if _, ok := writer.(*Error); ok { - tx.Rollback() - } else { - err = tx.Commit() - if err != nil { - log.Print(err) - writer = NewError(999 /*Internal Error*/) - } - } - }() - - context.Tx = tx - context.StoreTx = tx - return h(r, context) -} - -func (ah *APIHandler) storeTxWrapper(h Handler, r *http.Request, context *Context) (writer ResponseWriterWriter) { tx, err := ah.Store.Begin() if err != nil { log.Print(err) @@ -100,7 +72,7 @@ func (ah *APIHandler) storeTxWrapper(h Handler, r *http.Request, context *Contex } }() - context.StoreTx = tx + context.Tx = tx return h(r, context) } diff --git a/internal/handlers/imports.go b/internal/handlers/imports.go index b76ae68..58696d0 100644 --- a/internal/handlers/imports.go +++ b/internal/handlers/imports.go @@ -3,7 +3,7 @@ package handlers import ( "encoding/json" "github.com/aclindsa/moneygo/internal/models" - "github.com/aclindsa/moneygo/internal/store/db" + "github.com/aclindsa/moneygo/internal/store" "github.com/aclindsa/ofxgo" "io" "log" @@ -24,7 +24,7 @@ func (od *OFXDownload) Read(json_str string) error { return dec.Decode(od) } -func ofxImportHelper(tx *db.Tx, r io.Reader, user *models.User, accountid int64) ResponseWriterWriter { +func ofxImportHelper(tx store.Tx, r io.Reader, user *models.User, accountid int64) ResponseWriterWriter { itl, err := ImportOFX(r) if err != nil { diff --git a/internal/handlers/prices.go b/internal/handlers/prices.go index c51d5c8..f737df1 100644 --- a/internal/handlers/prices.go +++ b/internal/handlers/prices.go @@ -2,16 +2,16 @@ package handlers import ( "github.com/aclindsa/moneygo/internal/models" - "github.com/aclindsa/moneygo/internal/store/db" + "github.com/aclindsa/moneygo/internal/store" "log" "net/http" "time" ) -func CreatePriceIfNotExist(tx *db.Tx, price *models.Price) error { +func CreatePriceIfNotExist(tx store.Tx, price *models.Price) error { if len(price.RemoteId) == 0 { // Always create a new price if we can't match on the RemoteId - err := tx.Insert(price) + err := tx.InsertPrice(price) if err != nil { return err } @@ -34,7 +34,7 @@ func CreatePriceIfNotExist(tx *db.Tx, price *models.Price) error { } // Return the price for security in currency closest to date -func GetClosestPrice(tx *db.Tx, security, currency *models.Security, date *time.Time) (*models.Price, error) { +func GetClosestPrice(tx store.Tx, security, currency *models.Security, date *time.Time) (*models.Price, error) { earliest, _ := tx.GetEarliestPrice(security, currency, date) latest, err := tx.GetLatestPrice(security, currency, date) @@ -75,7 +75,7 @@ func PriceHandler(r *http.Request, context *Context, user *models.User, security return NewError(3 /*Invalid Request*/) } - err = context.Tx.Insert(&price) + err = context.Tx.InsertPrice(&price) if err != nil { log.Print(err) return NewError(999 /*Internal Error*/) diff --git a/internal/handlers/reports.go b/internal/handlers/reports.go index 46d8061..abf554f 100644 --- a/internal/handlers/reports.go +++ b/internal/handlers/reports.go @@ -5,7 +5,7 @@ import ( "errors" "fmt" "github.com/aclindsa/moneygo/internal/models" - "github.com/aclindsa/moneygo/internal/store/db" + "github.com/aclindsa/moneygo/internal/store" "github.com/yuin/gopher-lua" "log" "net/http" @@ -25,57 +25,7 @@ const ( const luaTimeoutSeconds time.Duration = 30 // maximum time a lua request can run for -func GetReport(tx *db.Tx, reportid int64, userid int64) (*models.Report, error) { - var r models.Report - - err := tx.SelectOne(&r, "SELECT * from reports where UserId=? AND ReportId=?", userid, reportid) - if err != nil { - return nil, err - } - return &r, nil -} - -func GetReports(tx *db.Tx, userid int64) (*[]models.Report, error) { - var reports []models.Report - - _, err := tx.Select(&reports, "SELECT * from reports where UserId=?", userid) - if err != nil { - return nil, err - } - return &reports, nil -} - -func InsertReport(tx *db.Tx, r *models.Report) error { - err := tx.Insert(r) - if err != nil { - return err - } - return nil -} - -func UpdateReport(tx *db.Tx, r *models.Report) error { - count, err := tx.Update(r) - if err != nil { - return err - } - if count != 1 { - return errors.New("Updated more than one report") - } - return nil -} - -func DeleteReport(tx *db.Tx, r *models.Report) error { - count, err := tx.Delete(r) - if err != nil { - return err - } - if count != 1 { - return errors.New("Deleted more than one report") - } - return nil -} - -func runReport(tx *db.Tx, user *models.User, report *models.Report) (*models.Tabulation, error) { +func runReport(tx store.Tx, user *models.User, report *models.Report) (*models.Tabulation, error) { // Create a new LState without opening the default libs for security L := lua.NewState(lua.Options{SkipOpenLibs: true}) defer L.Close() @@ -139,8 +89,8 @@ func runReport(tx *db.Tx, user *models.User, report *models.Report) (*models.Tab } } -func ReportTabulationHandler(tx *db.Tx, r *http.Request, user *models.User, reportid int64) ResponseWriterWriter { - report, err := GetReport(tx, reportid, user.UserId) +func ReportTabulationHandler(tx store.Tx, r *http.Request, user *models.User, reportid int64) ResponseWriterWriter { + report, err := tx.GetReport(reportid, user.UserId) if err != nil { return NewError(3 /*Invalid Request*/) } @@ -175,7 +125,7 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter { return NewError(3 /*Invalid Request*/) } - err = InsertReport(context.Tx, &report) + err = context.Tx.InsertReport(&report) if err != nil { log.Print(err) return NewError(999 /*Internal Error*/) @@ -186,7 +136,7 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter { if context.LastLevel() { //Return all Reports var rl models.ReportList - reports, err := GetReports(context.Tx, user.UserId) + reports, err := context.Tx.GetReports(user.UserId) if err != nil { log.Print(err) return NewError(999 /*Internal Error*/) @@ -204,7 +154,7 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter { return ReportTabulationHandler(context.Tx, r, user, reportid) } else { // Return Report with this Id - report, err := GetReport(context.Tx, reportid, user.UserId) + report, err := context.Tx.GetReport(reportid, user.UserId) if err != nil { return NewError(3 /*Invalid Request*/) } @@ -228,7 +178,7 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter { return NewError(3 /*Invalid Request*/) } - err = UpdateReport(context.Tx, &report) + err = context.Tx.UpdateReport(&report) if err != nil { log.Print(err) return NewError(999 /*Internal Error*/) @@ -236,12 +186,12 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter { return &report } else if r.Method == "DELETE" { - report, err := GetReport(context.Tx, reportid, user.UserId) + report, err := context.Tx.GetReport(reportid, user.UserId) if err != nil { return NewError(3 /*Invalid Request*/) } - err = DeleteReport(context.Tx, report) + err = context.Tx.DeleteReport(report) if err != nil { log.Print(err) return NewError(999 /*Internal Error*/) diff --git a/internal/handlers/securities.go b/internal/handlers/securities.go index 7c720eb..f5e82ff 100644 --- a/internal/handlers/securities.go +++ b/internal/handlers/securities.go @@ -6,7 +6,6 @@ import ( "errors" "github.com/aclindsa/moneygo/internal/models" "github.com/aclindsa/moneygo/internal/store" - "github.com/aclindsa/moneygo/internal/store/db" "log" "net/http" "net/url" @@ -51,7 +50,7 @@ func FindCurrencyTemplate(iso4217 int64) *models.Security { return nil } -func UpdateSecurity(tx *db.Tx, s *models.Security) (err error) { +func UpdateSecurity(tx store.Tx, s *models.Security) (err error) { user, err := tx.GetUser(s.UserId) if err != nil { return @@ -67,7 +66,7 @@ func UpdateSecurity(tx *db.Tx, s *models.Security) (err error) { return nil } -func ImportGetCreateSecurity(tx *db.Tx, userid int64, security *models.Security) (*models.Security, error) { +func ImportGetCreateSecurity(tx store.Tx, userid int64, security *models.Security) (*models.Security, error) { security.UserId = userid if len(security.AlternateId) == 0 { // Always create a new local security if we can't match on the AlternateId diff --git a/internal/handlers/securities_lua.go b/internal/handlers/securities_lua.go index 78716f2..eaaf71d 100644 --- a/internal/handlers/securities_lua.go +++ b/internal/handlers/securities_lua.go @@ -4,7 +4,7 @@ import ( "context" "errors" "github.com/aclindsa/moneygo/internal/models" - "github.com/aclindsa/moneygo/internal/store/db" + "github.com/aclindsa/moneygo/internal/store" "github.com/yuin/gopher-lua" ) @@ -15,7 +15,7 @@ func luaContextGetSecurities(L *lua.LState) (map[int64]*models.Security, error) ctx := L.Context() - tx, ok := ctx.Value(dbContextKey).(*db.Tx) + tx, ok := ctx.Value(dbContextKey).(store.Tx) if !ok { return nil, errors.New("Couldn't find tx in lua's Context") } @@ -159,7 +159,7 @@ func luaClosestPrice(L *lua.LState) int { date := luaCheckTime(L, 3) ctx := L.Context() - tx, ok := ctx.Value(dbContextKey).(*db.Tx) + tx, ok := ctx.Value(dbContextKey).(store.Tx) if !ok { panic("Couldn't find tx in lua's Context") } diff --git a/internal/handlers/sessions.go b/internal/handlers/sessions.go index 71deff4..e273cb1 100644 --- a/internal/handlers/sessions.go +++ b/internal/handlers/sessions.go @@ -89,7 +89,7 @@ func SessionHandler(r *http.Request, context *Context) ResponseWriterWriter { // attacks user.HashPassword() - dbuser, err := context.StoreTx.GetUserByUsername(user.Username) + dbuser, err := context.Tx.GetUserByUsername(user.Username) if err != nil { return NewError(2 /*Unauthorized Access*/) } @@ -98,21 +98,21 @@ func SessionHandler(r *http.Request, context *Context) ResponseWriterWriter { return NewError(2 /*Unauthorized Access*/) } - sessionwriter, err := NewSession(context.StoreTx, r, dbuser.UserId) + sessionwriter, err := NewSession(context.Tx, r, dbuser.UserId) if err != nil { log.Print(err) return NewError(999 /*Internal Error*/) } return sessionwriter } else if r.Method == "GET" { - s, err := GetSession(context.StoreTx, r) + s, err := GetSession(context.Tx, r) if err != nil { return NewError(1 /*Not Signed In*/) } return s } else if r.Method == "DELETE" { - err := DeleteSessionIfExists(context.StoreTx, r) + err := DeleteSessionIfExists(context.Tx, r) if err != nil { log.Print(err) return NewError(999 /*Internal Error*/) diff --git a/internal/handlers/transactions.go b/internal/handlers/transactions.go index 2d16da1..1d522d0 100644 --- a/internal/handlers/transactions.go +++ b/internal/handlers/transactions.go @@ -4,7 +4,6 @@ import ( "errors" "github.com/aclindsa/moneygo/internal/models" "github.com/aclindsa/moneygo/internal/store" - "github.com/aclindsa/moneygo/internal/store/db" "log" "math/big" "net/http" @@ -14,7 +13,7 @@ import ( // Return a map of security ID's to big.Rat's containing the amount that // security is imbalanced by -func GetTransactionImbalances(tx *db.Tx, t *models.Transaction) (map[int64]big.Rat, error) { +func GetTransactionImbalances(tx store.Tx, t *models.Transaction) (map[int64]big.Rat, error) { sums := make(map[int64]big.Rat) if !t.Valid() { @@ -42,7 +41,7 @@ func GetTransactionImbalances(tx *db.Tx, t *models.Transaction) (map[int64]big.R // Returns true if all securities contained in this transaction are balanced, // false otherwise -func TransactionBalanced(tx *db.Tx, t *models.Transaction) (bool, error) { +func TransactionBalanced(tx store.Tx, t *models.Transaction) (bool, error) { var zero big.Rat sums, err := GetTransactionImbalances(tx, t) diff --git a/internal/handlers/tx.go b/internal/handlers/tx.go deleted file mode 100644 index 750220a..0000000 --- a/internal/handlers/tx.go +++ /dev/null @@ -1,14 +0,0 @@ -package handlers - -import ( - "github.com/aclindsa/gorp" - "github.com/aclindsa/moneygo/internal/store/db" -) - -func GetTx(gdb *gorp.DbMap) (*db.Tx, error) { - tx, err := gdb.Begin() - if err != nil { - return nil, err - } - return &db.Tx{gdb.Dialect, tx}, nil -} diff --git a/internal/handlers/users.go b/internal/handlers/users.go index e9a468d..ed7275d 100644 --- a/internal/handlers/users.go +++ b/internal/handlers/users.go @@ -140,7 +140,7 @@ func UserHandler(r *http.Request, context *Context) ResponseWriterWriter { return user } else if r.Method == "DELETE" { - err := context.StoreTx.DeleteUser(user) + err := context.Tx.DeleteUser(user) if err != nil { log.Print(err) return NewError(999 /*Internal Error*/) diff --git a/internal/models/reports.go b/internal/models/reports.go index 493fd21..01606ee 100644 --- a/internal/models/reports.go +++ b/internal/models/reports.go @@ -28,7 +28,7 @@ func (r *Report) Read(json_str string) error { } type ReportList struct { - Reports *[]Report `json:"reports"` + Reports *[]*Report `json:"reports"` } func (rl *ReportList) Write(w http.ResponseWriter) error { diff --git a/internal/store/db/reports.go b/internal/store/db/reports.go new file mode 100644 index 0000000..e220695 --- /dev/null +++ b/internal/store/db/reports.go @@ -0,0 +1,56 @@ +package db + +import ( + "fmt" + "github.com/aclindsa/moneygo/internal/models" +) + +func (tx *Tx) GetReport(reportid int64, userid int64) (*models.Report, error) { + var r models.Report + + err := tx.SelectOne(&r, "SELECT * from reports where UserId=? AND ReportId=?", userid, reportid) + if err != nil { + return nil, err + } + return &r, nil +} + +func (tx *Tx) GetReports(userid int64) (*[]*models.Report, error) { + var reports []*models.Report + + _, err := tx.Select(&reports, "SELECT * from reports where UserId=?", userid) + if err != nil { + return nil, err + } + return &reports, nil +} + +func (tx *Tx) InsertReport(report *models.Report) error { + err := tx.Insert(report) + if err != nil { + return err + } + return nil +} + +func (tx *Tx) UpdateReport(report *models.Report) error { + count, err := tx.Update(report) + if err != nil { + return err + } + if count != 1 { + return fmt.Errorf("Expected to update 1 report, was going to update %d", count) + } + return nil +} + +func (tx *Tx) DeleteReport(report *models.Report) error { + count, err := tx.Delete(report) + if err != nil { + return err + } + if count != 1 { + return fmt.Errorf("Expected to delete 1 report, was going to delete %d", count) + } + return nil +} diff --git a/internal/store/store.go b/internal/store/store.go index c890dd7..3f87880 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -96,6 +96,11 @@ type TransactionStore interface { } type ReportStore interface { + InsertReport(report *models.Report) error + GetReport(reportid int64, userid int64) (*models.Report, error) + GetReports(userid int64) (*[]*models.Report, error) + UpdateReport(report *models.Report) error + DeleteReport(report *models.Report) error } type Tx interface {