1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-11-01 00:10:06 -04:00
moneygo/internal/handlers/prices.go

155 lines
3.5 KiB
Go
Raw Normal View History

package handlers
import (
"github.com/aclindsa/moneygo/internal/models"
2017-12-09 05:56:45 -05:00
"github.com/aclindsa/moneygo/internal/store"
"log"
"net/http"
"time"
)
2017-12-09 05:56:45 -05:00
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
2017-12-09 05:56:45 -05:00
err := tx.InsertPrice(price)
if err != nil {
return err
}
return nil
}
2017-12-07 21:05:55 -05:00
exists, err := tx.PriceExists(price)
if err != nil {
return err
}
2017-12-07 21:05:55 -05:00
if exists {
return nil // price already exists
}
2017-12-07 21:05:55 -05:00
err = tx.InsertPrice(price)
if err != nil {
return err
}
return nil
}
// Return the price for security in currency closest to date
2017-12-09 05:56:45 -05:00
func GetClosestPrice(tx store.Tx, security, currency *models.Security, date *time.Time) (*models.Price, error) {
2017-12-07 21:05:55 -05:00
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 {
return latest, err
} else if err != nil {
return earliest, nil
}
howlate := earliest.Date.Sub(*date)
howearly := date.Sub(latest.Date)
if howearly < howlate {
return latest, nil
} else {
return earliest, nil
}
}
func PriceHandler(r *http.Request, context *Context, user *models.User, securityid int64) ResponseWriterWriter {
2017-12-07 20:08:43 -05:00
security, err := context.Tx.GetSecurity(securityid, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
if r.Method == "POST" {
2017-12-04 21:05:17 -05:00
var price models.Price
if err := ReadJSON(r, &price); err != nil {
return NewError(3 /*Invalid Request*/)
}
price.PriceId = -1
if price.SecurityId != security.SecurityId {
return NewError(3 /*Invalid Request*/)
}
2017-12-07 20:08:43 -05:00
_, err = context.Tx.GetSecurity(price.CurrencyId, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
2017-12-09 05:56:45 -05:00
err = context.Tx.InsertPrice(&price)
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
return ResponseWrapper{201, &price}
} else if r.Method == "GET" {
if context.LastLevel() {
//Return all this security's prices
2017-12-04 21:05:17 -05:00
var pl models.PriceList
2017-12-07 21:05:55 -05:00
prices, err := context.Tx.GetPrices(security.SecurityId)
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
pl.Prices = prices
return &pl
}
priceid, err := context.NextID()
if err != nil {
return NewError(3 /*Invalid Request*/)
}
2017-12-07 21:05:55 -05:00
price, err := context.Tx.GetPrice(priceid, security.SecurityId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
return price
} else {
priceid, err := context.NextID()
if err != nil {
return NewError(3 /*Invalid Request*/)
}
if r.Method == "PUT" {
2017-12-04 21:05:17 -05:00
var price models.Price
if err := ReadJSON(r, &price); err != nil || price.PriceId != priceid {
return NewError(3 /*Invalid Request*/)
}
2017-12-07 20:08:43 -05:00
_, err = context.Tx.GetSecurity(price.SecurityId, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
2017-12-07 20:08:43 -05:00
_, err = context.Tx.GetSecurity(price.CurrencyId, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
2017-12-07 21:05:55 -05:00
err = context.Tx.UpdatePrice(&price)
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
return &price
} else if r.Method == "DELETE" {
2017-12-07 21:05:55 -05:00
price, err := context.Tx.GetPrice(priceid, security.SecurityId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
2017-12-07 21:05:55 -05:00
err = context.Tx.DeletePrice(price)
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
return SuccessWriter{}
}
}
return NewError(3 /*Invalid Request*/)
}