mirror of
https://github.com/aclindsa/moneygo.git
synced 2025-06-14 05:58:37 -04:00
Move prices to store
This commit is contained in:
@ -18,67 +18,25 @@ func CreatePriceIfNotExist(tx *db.Tx, price *models.Price) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var prices []*models.Price
|
||||
|
||||
_, err := tx.Select(&prices, "SELECT * from prices where SecurityId=? AND CurrencyId=? AND Date=? AND Value=?", price.SecurityId, price.CurrencyId, price.Date, price.Value)
|
||||
exists, err := tx.PriceExists(price)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(prices) > 0 {
|
||||
if exists {
|
||||
return nil // price already exists
|
||||
}
|
||||
|
||||
err = tx.Insert(price)
|
||||
err = tx.InsertPrice(price)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetPrice(tx *db.Tx, priceid, securityid int64) (*models.Price, error) {
|
||||
var p models.Price
|
||||
err := tx.SelectOne(&p, "SELECT * from prices where PriceId=? AND SecurityId=?", priceid, securityid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
func GetPrices(tx *db.Tx, securityid int64) (*[]*models.Price, error) {
|
||||
var prices []*models.Price
|
||||
|
||||
_, err := tx.Select(&prices, "SELECT * from prices where SecurityId=?", securityid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &prices, nil
|
||||
}
|
||||
|
||||
// Return the latest price for security in currency units before date
|
||||
func GetLatestPrice(tx *db.Tx, security, currency *models.Security, date *time.Time) (*models.Price, error) {
|
||||
var p models.Price
|
||||
err := tx.SelectOne(&p, "SELECT * from prices where SecurityId=? AND CurrencyId=? AND Date <= ? ORDER BY Date DESC LIMIT 1", security.SecurityId, currency.SecurityId, date)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
// Return the earliest price for security in currency units after date
|
||||
func GetEarliestPrice(tx *db.Tx, security, currency *models.Security, date *time.Time) (*models.Price, error) {
|
||||
var p models.Price
|
||||
err := tx.SelectOne(&p, "SELECT * from prices where SecurityId=? AND CurrencyId=? AND Date >= ? ORDER BY Date ASC LIMIT 1", security.SecurityId, currency.SecurityId, date)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
// 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) {
|
||||
earliest, _ := GetEarliestPrice(tx, security, currency, date)
|
||||
latest, err := GetLatestPrice(tx, security, currency, date)
|
||||
earliest, _ := tx.GetEarliestPrice(security, currency, date)
|
||||
latest, err := tx.GetLatestPrice(security, currency, date)
|
||||
|
||||
// Return early if either earliest or latest are invalid
|
||||
if earliest == nil {
|
||||
@ -129,7 +87,7 @@ func PriceHandler(r *http.Request, context *Context, user *models.User, security
|
||||
//Return all this security's prices
|
||||
var pl models.PriceList
|
||||
|
||||
prices, err := GetPrices(context.Tx, security.SecurityId)
|
||||
prices, err := context.Tx.GetPrices(security.SecurityId)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NewError(999 /*Internal Error*/)
|
||||
@ -144,7 +102,7 @@ func PriceHandler(r *http.Request, context *Context, user *models.User, security
|
||||
return NewError(3 /*Invalid Request*/)
|
||||
}
|
||||
|
||||
price, err := GetPrice(context.Tx, priceid, security.SecurityId)
|
||||
price, err := context.Tx.GetPrice(priceid, security.SecurityId)
|
||||
if err != nil {
|
||||
return NewError(3 /*Invalid Request*/)
|
||||
}
|
||||
@ -170,21 +128,21 @@ func PriceHandler(r *http.Request, context *Context, user *models.User, security
|
||||
return NewError(3 /*Invalid Request*/)
|
||||
}
|
||||
|
||||
count, err := context.Tx.Update(&price)
|
||||
if err != nil || count != 1 {
|
||||
err = context.Tx.UpdatePrice(&price)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NewError(999 /*Internal Error*/)
|
||||
}
|
||||
|
||||
return &price
|
||||
} else if r.Method == "DELETE" {
|
||||
price, err := GetPrice(context.Tx, priceid, security.SecurityId)
|
||||
price, err := context.Tx.GetPrice(priceid, security.SecurityId)
|
||||
if err != nil {
|
||||
return NewError(3 /*Invalid Request*/)
|
||||
}
|
||||
|
||||
count, err := context.Tx.Delete(price)
|
||||
if err != nil || count != 1 {
|
||||
err = context.Tx.DeletePrice(price)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NewError(999 /*Internal Error*/)
|
||||
}
|
||||
|
Reference in New Issue
Block a user