Finish 'store' separation

This commit is contained in:
Aaron Lindsay 2017-12-09 05:56:45 -05:00
parent af97f92df5
commit 32aef11da5
15 changed files with 100 additions and 134 deletions

View File

@ -4,14 +4,13 @@ import (
"errors" "errors"
"github.com/aclindsa/moneygo/internal/models" "github.com/aclindsa/moneygo/internal/models"
"github.com/aclindsa/moneygo/internal/store" "github.com/aclindsa/moneygo/internal/store"
"github.com/aclindsa/moneygo/internal/store/db"
"log" "log"
"net/http" "net/http"
) )
// Get (and attempt to create if it doesn't exist). Matches on UserId, // Get (and attempt to create if it doesn't exist). Matches on UserId,
// SecurityId, Type, Name, and ParentAccountId // 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 var account models.Account
accounts, err := tx.FindMatchingAccounts(&a) 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.Name = a.Name
account.ParentAccountId = a.ParentAccountId account.ParentAccountId = a.ParentAccountId
err = tx.Insert(&account) err = tx.InsertAccount(&account)
if err != nil { if err != nil {
return nil, err 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 // Get (and attempt to create if it doesn't exist) the security/currency
// trading account for the supplied 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 tradingAccount models.Account
var account 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 // Get (and attempt to create if it doesn't exist) the security/currency
// imbalance account for the supplied 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 imbalanceAccount models.Account
var account models.Account var account models.Account
xxxtemplate := FindSecurityTemplate("XXX", models.Currency) xxxtemplate := FindSecurityTemplate("XXX", models.Currency)

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"errors" "errors"
"github.com/aclindsa/moneygo/internal/models" "github.com/aclindsa/moneygo/internal/models"
"github.com/aclindsa/moneygo/internal/store/db" "github.com/aclindsa/moneygo/internal/store"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
"strings" "strings"
) )
@ -16,7 +16,7 @@ func luaContextGetAccounts(L *lua.LState) (map[int64]*models.Account, error) {
ctx := L.Context() ctx := L.Context()
tx, ok := ctx.Value(dbContextKey).(*db.Tx) tx, ok := ctx.Value(dbContextKey).(store.Tx)
if !ok { if !ok {
return nil, errors.New("Couldn't find tx in lua's Context") 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) a := luaCheckAccount(L, 1)
ctx := L.Context() ctx := L.Context()
tx, ok := ctx.Value(dbContextKey).(*db.Tx) tx, ok := ctx.Value(dbContextKey).(store.Tx)
if !ok { if !ok {
panic("Couldn't find tx in lua's Context") panic("Couldn't find tx in lua's Context")
} }

View File

@ -17,8 +17,7 @@ type ResponseWriterWriter interface {
} }
type Context struct { type Context struct {
Tx *db.Tx Tx store.Tx
StoreTx store.Tx
User *models.User User *models.User
remainingURL string // portion of URL path not yet reached in the hierarchy 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) { 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() tx, err := ah.Store.Begin()
if err != nil { if err != nil {
log.Print(err) 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) return h(r, context)
} }

View File

@ -3,7 +3,7 @@ package handlers
import ( import (
"encoding/json" "encoding/json"
"github.com/aclindsa/moneygo/internal/models" "github.com/aclindsa/moneygo/internal/models"
"github.com/aclindsa/moneygo/internal/store/db" "github.com/aclindsa/moneygo/internal/store"
"github.com/aclindsa/ofxgo" "github.com/aclindsa/ofxgo"
"io" "io"
"log" "log"
@ -24,7 +24,7 @@ func (od *OFXDownload) Read(json_str string) error {
return dec.Decode(od) 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) itl, err := ImportOFX(r)
if err != nil { if err != nil {

View File

@ -2,16 +2,16 @@ package handlers
import ( import (
"github.com/aclindsa/moneygo/internal/models" "github.com/aclindsa/moneygo/internal/models"
"github.com/aclindsa/moneygo/internal/store/db" "github.com/aclindsa/moneygo/internal/store"
"log" "log"
"net/http" "net/http"
"time" "time"
) )
func CreatePriceIfNotExist(tx *db.Tx, price *models.Price) error { func CreatePriceIfNotExist(tx store.Tx, price *models.Price) error {
if len(price.RemoteId) == 0 { if len(price.RemoteId) == 0 {
// Always create a new price if we can't match on the RemoteId // Always create a new price if we can't match on the RemoteId
err := tx.Insert(price) err := tx.InsertPrice(price)
if err != nil { if err != nil {
return err 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 // 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) earliest, _ := tx.GetEarliestPrice(security, currency, date)
latest, err := tx.GetLatestPrice(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*/) return NewError(3 /*Invalid Request*/)
} }
err = context.Tx.Insert(&price) err = context.Tx.InsertPrice(&price)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
return NewError(999 /*Internal Error*/) return NewError(999 /*Internal Error*/)

View File

@ -5,7 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/aclindsa/moneygo/internal/models" "github.com/aclindsa/moneygo/internal/models"
"github.com/aclindsa/moneygo/internal/store/db" "github.com/aclindsa/moneygo/internal/store"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
"log" "log"
"net/http" "net/http"
@ -25,57 +25,7 @@ const (
const luaTimeoutSeconds time.Duration = 30 // maximum time a lua request can run for 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) { func runReport(tx store.Tx, user *models.User, report *models.Report) (*models.Tabulation, 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) {
// Create a new LState without opening the default libs for security // Create a new LState without opening the default libs for security
L := lua.NewState(lua.Options{SkipOpenLibs: true}) L := lua.NewState(lua.Options{SkipOpenLibs: true})
defer L.Close() 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 { func ReportTabulationHandler(tx store.Tx, r *http.Request, user *models.User, reportid int64) ResponseWriterWriter {
report, err := GetReport(tx, reportid, user.UserId) report, err := tx.GetReport(reportid, user.UserId)
if err != nil { if err != nil {
return NewError(3 /*Invalid Request*/) return NewError(3 /*Invalid Request*/)
} }
@ -175,7 +125,7 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter {
return NewError(3 /*Invalid Request*/) return NewError(3 /*Invalid Request*/)
} }
err = InsertReport(context.Tx, &report) err = context.Tx.InsertReport(&report)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
return NewError(999 /*Internal Error*/) return NewError(999 /*Internal Error*/)
@ -186,7 +136,7 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter {
if context.LastLevel() { if context.LastLevel() {
//Return all Reports //Return all Reports
var rl models.ReportList var rl models.ReportList
reports, err := GetReports(context.Tx, user.UserId) reports, err := context.Tx.GetReports(user.UserId)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
return NewError(999 /*Internal Error*/) return NewError(999 /*Internal Error*/)
@ -204,7 +154,7 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter {
return ReportTabulationHandler(context.Tx, r, user, reportid) return ReportTabulationHandler(context.Tx, r, user, reportid)
} else { } else {
// Return Report with this Id // Return Report with this Id
report, err := GetReport(context.Tx, reportid, user.UserId) report, err := context.Tx.GetReport(reportid, user.UserId)
if err != nil { if err != nil {
return NewError(3 /*Invalid Request*/) return NewError(3 /*Invalid Request*/)
} }
@ -228,7 +178,7 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter {
return NewError(3 /*Invalid Request*/) return NewError(3 /*Invalid Request*/)
} }
err = UpdateReport(context.Tx, &report) err = context.Tx.UpdateReport(&report)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
return NewError(999 /*Internal Error*/) return NewError(999 /*Internal Error*/)
@ -236,12 +186,12 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter {
return &report return &report
} else if r.Method == "DELETE" { } else if r.Method == "DELETE" {
report, err := GetReport(context.Tx, reportid, user.UserId) report, err := context.Tx.GetReport(reportid, user.UserId)
if err != nil { if err != nil {
return NewError(3 /*Invalid Request*/) return NewError(3 /*Invalid Request*/)
} }
err = DeleteReport(context.Tx, report) err = context.Tx.DeleteReport(report)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
return NewError(999 /*Internal Error*/) return NewError(999 /*Internal Error*/)

View File

@ -6,7 +6,6 @@ import (
"errors" "errors"
"github.com/aclindsa/moneygo/internal/models" "github.com/aclindsa/moneygo/internal/models"
"github.com/aclindsa/moneygo/internal/store" "github.com/aclindsa/moneygo/internal/store"
"github.com/aclindsa/moneygo/internal/store/db"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
@ -51,7 +50,7 @@ func FindCurrencyTemplate(iso4217 int64) *models.Security {
return nil 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) user, err := tx.GetUser(s.UserId)
if err != nil { if err != nil {
return return
@ -67,7 +66,7 @@ func UpdateSecurity(tx *db.Tx, s *models.Security) (err error) {
return nil 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 security.UserId = userid
if len(security.AlternateId) == 0 { if len(security.AlternateId) == 0 {
// Always create a new local security if we can't match on the AlternateId // Always create a new local security if we can't match on the AlternateId

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"errors" "errors"
"github.com/aclindsa/moneygo/internal/models" "github.com/aclindsa/moneygo/internal/models"
"github.com/aclindsa/moneygo/internal/store/db" "github.com/aclindsa/moneygo/internal/store"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
) )
@ -15,7 +15,7 @@ func luaContextGetSecurities(L *lua.LState) (map[int64]*models.Security, error)
ctx := L.Context() ctx := L.Context()
tx, ok := ctx.Value(dbContextKey).(*db.Tx) tx, ok := ctx.Value(dbContextKey).(store.Tx)
if !ok { if !ok {
return nil, errors.New("Couldn't find tx in lua's Context") 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) date := luaCheckTime(L, 3)
ctx := L.Context() ctx := L.Context()
tx, ok := ctx.Value(dbContextKey).(*db.Tx) tx, ok := ctx.Value(dbContextKey).(store.Tx)
if !ok { if !ok {
panic("Couldn't find tx in lua's Context") panic("Couldn't find tx in lua's Context")
} }

View File

@ -89,7 +89,7 @@ func SessionHandler(r *http.Request, context *Context) ResponseWriterWriter {
// attacks // attacks
user.HashPassword() user.HashPassword()
dbuser, err := context.StoreTx.GetUserByUsername(user.Username) dbuser, err := context.Tx.GetUserByUsername(user.Username)
if err != nil { if err != nil {
return NewError(2 /*Unauthorized Access*/) return NewError(2 /*Unauthorized Access*/)
} }
@ -98,21 +98,21 @@ func SessionHandler(r *http.Request, context *Context) ResponseWriterWriter {
return NewError(2 /*Unauthorized Access*/) return NewError(2 /*Unauthorized Access*/)
} }
sessionwriter, err := NewSession(context.StoreTx, r, dbuser.UserId) sessionwriter, err := NewSession(context.Tx, r, dbuser.UserId)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
return NewError(999 /*Internal Error*/) return NewError(999 /*Internal Error*/)
} }
return sessionwriter return sessionwriter
} else if r.Method == "GET" { } else if r.Method == "GET" {
s, err := GetSession(context.StoreTx, r) s, err := GetSession(context.Tx, r)
if err != nil { if err != nil {
return NewError(1 /*Not Signed In*/) return NewError(1 /*Not Signed In*/)
} }
return s return s
} else if r.Method == "DELETE" { } else if r.Method == "DELETE" {
err := DeleteSessionIfExists(context.StoreTx, r) err := DeleteSessionIfExists(context.Tx, r)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
return NewError(999 /*Internal Error*/) return NewError(999 /*Internal Error*/)

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"github.com/aclindsa/moneygo/internal/models" "github.com/aclindsa/moneygo/internal/models"
"github.com/aclindsa/moneygo/internal/store" "github.com/aclindsa/moneygo/internal/store"
"github.com/aclindsa/moneygo/internal/store/db"
"log" "log"
"math/big" "math/big"
"net/http" "net/http"
@ -14,7 +13,7 @@ import (
// Return a map of security ID's to big.Rat's containing the amount that // Return a map of security ID's to big.Rat's containing the amount that
// security is imbalanced by // 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) sums := make(map[int64]big.Rat)
if !t.Valid() { 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, // Returns true if all securities contained in this transaction are balanced,
// false otherwise // 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 var zero big.Rat
sums, err := GetTransactionImbalances(tx, t) sums, err := GetTransactionImbalances(tx, t)

View File

@ -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
}

View File

@ -140,7 +140,7 @@ func UserHandler(r *http.Request, context *Context) ResponseWriterWriter {
return user return user
} else if r.Method == "DELETE" { } else if r.Method == "DELETE" {
err := context.StoreTx.DeleteUser(user) err := context.Tx.DeleteUser(user)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
return NewError(999 /*Internal Error*/) return NewError(999 /*Internal Error*/)

View File

@ -28,7 +28,7 @@ func (r *Report) Read(json_str string) error {
} }
type ReportList struct { type ReportList struct {
Reports *[]Report `json:"reports"` Reports *[]*Report `json:"reports"`
} }
func (rl *ReportList) Write(w http.ResponseWriter) error { func (rl *ReportList) Write(w http.ResponseWriter) error {

View File

@ -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
}

View File

@ -96,6 +96,11 @@ type TransactionStore interface {
} }
type ReportStore 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 { type Tx interface {