1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-12-25 07:13:20 -05:00

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"
"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)

View File

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

View File

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

View File

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

View File

@ -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*/)

View File

@ -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*/)

View File

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

View File

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

View File

@ -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*/)

View File

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

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
} 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*/)

View File

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

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 {
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 {