1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-10-30 07:40:05 -04:00
moneygo/internal/handlers/securities.go

266 lines
6.8 KiB
Go
Raw Permalink Normal View History

package handlers
//go:generate make
2015-06-25 22:36:58 -04:00
2015-06-27 08:31:38 -04:00
import (
"errors"
2017-12-03 06:38:22 -05:00
"github.com/aclindsa/moneygo/internal/models"
2017-12-07 20:47:55 -05:00
"github.com/aclindsa/moneygo/internal/store"
2015-06-27 08:31:38 -04:00
"log"
"net/http"
"net/url"
"strconv"
"strings"
2015-06-27 08:31:38 -04:00
)
2017-12-03 06:38:22 -05:00
func SearchSecurityTemplates(search string, _type models.SecurityType, limit int64) []*models.Security {
upperSearch := strings.ToUpper(search)
2017-12-03 06:38:22 -05:00
var results []*models.Security
for i, security := range SecurityTemplates {
if strings.Contains(strings.ToUpper(security.Name), upperSearch) ||
strings.Contains(strings.ToUpper(security.Description), upperSearch) ||
strings.Contains(strings.ToUpper(security.Symbol), upperSearch) {
if _type == 0 || _type == security.Type {
results = append(results, &SecurityTemplates[i])
if limit != -1 && int64(len(results)) >= limit {
break
}
}
}
2015-06-27 08:31:38 -04:00
}
return results
2015-06-27 08:31:38 -04:00
}
2017-12-03 06:38:22 -05:00
func FindSecurityTemplate(name string, _type models.SecurityType) *models.Security {
for _, security := range SecurityTemplates {
if name == security.Name && _type == security.Type {
return &security
}
}
return nil
}
2017-12-03 06:38:22 -05:00
func FindCurrencyTemplate(iso4217 int64) *models.Security {
2017-06-21 21:25:38 -04:00
iso4217string := strconv.FormatInt(iso4217, 10)
for _, security := range SecurityTemplates {
2017-12-03 06:38:22 -05:00
if security.Type == models.Currency && security.AlternateId == iso4217string {
2017-06-21 21:25:38 -04:00
return &security
}
}
return nil
}
2017-12-09 05:56:45 -05:00
func UpdateSecurity(tx store.Tx, s *models.Security) (err error) {
2017-12-07 20:08:43 -05:00
user, err := tx.GetUser(s.UserId)
if err != nil {
return
2017-12-03 06:38:22 -05:00
} else if user.DefaultCurrency == s.SecurityId && s.Type != models.Currency {
return errors.New("Cannot change security which is user's default currency to be non-currency")
}
2017-12-07 20:08:43 -05:00
err = tx.UpdateSecurity(s)
if err != nil {
return
}
return nil
2015-06-27 08:31:38 -04:00
}
2017-12-09 05:56:45 -05:00
func ImportGetCreateSecurity(tx store.Tx, userid int64, security *models.Security) (*models.Security, error) {
2017-06-04 16:01:42 -04:00
security.UserId = userid
if len(security.AlternateId) == 0 {
// Always create a new local security if we can't match on the AlternateId
2017-12-07 20:08:43 -05:00
err := tx.InsertSecurity(security)
if err != nil {
return nil, err
}
return security, nil
}
2017-12-07 20:47:55 -05:00
securities, err := tx.FindMatchingSecurities(security)
if err != nil {
return nil, err
}
// First try to find a case insensitive match on the name or symbol
upperName := strings.ToUpper(security.Name)
upperSymbol := strings.ToUpper(security.Symbol)
2017-12-07 20:08:43 -05:00
for _, s := range *securities {
if (len(s.Name) > 0 && strings.ToUpper(s.Name) == upperName) ||
(len(s.Symbol) > 0 && strings.ToUpper(s.Symbol) == upperSymbol) {
return s, nil
}
}
// if strings.Contains(strings.ToUpper(security.Name), upperSearch) ||
// Try to find a partial string match on the name or symbol
2017-12-07 20:08:43 -05:00
for _, s := range *securities {
sUpperName := strings.ToUpper(s.Name)
sUpperSymbol := strings.ToUpper(s.Symbol)
if (len(upperName) > 0 && len(s.Name) > 0 && (strings.Contains(upperName, sUpperName) || strings.Contains(sUpperName, upperName))) ||
(len(upperSymbol) > 0 && len(s.Symbol) > 0 && (strings.Contains(upperSymbol, sUpperSymbol) || strings.Contains(sUpperSymbol, upperSymbol))) {
return s, nil
}
}
// Give up and return the first security in the list
2017-12-07 20:08:43 -05:00
if len(*securities) > 0 {
return (*securities)[0], nil
}
// If there wasn't even one security in the list, make a new one
2017-12-07 20:08:43 -05:00
err = tx.InsertSecurity(security)
if err != nil {
return nil, err
}
return security, nil
}
func SecurityHandler(r *http.Request, context *Context) ResponseWriterWriter {
user, err := GetUserFromSession(context.Tx, r)
if err != nil {
return NewError(1 /*Not Signed In*/)
}
if r.Method == "POST" {
if !context.LastLevel() {
securityid, err := context.NextID()
if err != nil {
return NewError(3 /*Invalid Request*/)
}
if context.NextLevel() != "prices" {
return NewError(3 /*Invalid Request*/)
}
return PriceHandler(r, context, user, securityid)
}
2017-12-03 06:38:22 -05:00
var security models.Security
if err := ReadJSON(r, &security); err != nil {
return NewError(3 /*Invalid Request*/)
}
security.SecurityId = -1
security.UserId = user.UserId
2017-12-07 20:08:43 -05:00
err = context.Tx.InsertSecurity(&security)
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
return ResponseWrapper{201, &security}
} else if r.Method == "GET" {
if context.LastLevel() {
//Return all securities
2017-12-03 06:38:22 -05:00
var sl models.SecurityList
2017-12-07 20:08:43 -05:00
securities, err := context.Tx.GetSecurities(user.UserId)
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
sl.Securities = securities
return &sl
} else {
securityid, err := context.NextID()
if err != nil {
return NewError(3 /*Invalid Request*/)
}
if !context.LastLevel() {
if context.NextLevel() != "prices" {
return NewError(3 /*Invalid Request*/)
}
return PriceHandler(r, context, user, securityid)
}
2017-12-07 20:08:43 -05:00
security, err := context.Tx.GetSecurity(securityid, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
return security
}
} else {
securityid, err := context.NextID()
if err != nil {
return NewError(3 /*Invalid Request*/)
}
if !context.LastLevel() {
if context.NextLevel() != "prices" {
return NewError(3 /*Invalid Request*/)
}
return PriceHandler(r, context, user, securityid)
}
if r.Method == "PUT" {
2017-12-03 06:38:22 -05:00
var security models.Security
if err := ReadJSON(r, &security); err != nil || security.SecurityId != securityid {
return NewError(3 /*Invalid Request*/)
2015-06-27 08:31:38 -04:00
}
security.UserId = user.UserId
err = UpdateSecurity(context.Tx, &security)
2015-06-27 08:31:38 -04:00
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
2015-06-27 08:31:38 -04:00
}
return &security
} else if r.Method == "DELETE" {
2017-12-07 20:08:43 -05:00
security, err := context.Tx.GetSecurity(securityid, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
2017-12-07 20:08:43 -05:00
err = context.Tx.DeleteSecurity(security)
2017-12-07 20:47:55 -05:00
if _, ok := err.(store.SecurityInUseError); ok {
return NewError(7 /*In Use Error*/)
} else if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
return SuccessWriter{}
}
}
return NewError(3 /*Invalid Request*/)
}
func SecurityTemplateHandler(r *http.Request, context *Context) ResponseWriterWriter {
if r.Method == "GET" {
2017-12-03 06:38:22 -05:00
var sl models.SecurityList
query, _ := url.ParseQuery(r.URL.RawQuery)
var limit int64 = -1
search := query.Get("search")
2017-12-03 06:38:22 -05:00
var _type models.SecurityType = 0
typestring := query.Get("type")
if len(typestring) > 0 {
2017-12-03 06:38:22 -05:00
_type = models.GetSecurityType(typestring)
if _type == 0 {
return NewError(3 /*Invalid Request*/)
}
}
limitstring := query.Get("limit")
if limitstring != "" {
limitint, err := strconv.ParseInt(limitstring, 10, 0)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
limit = limitint
}
securities := SearchSecurityTemplates(search, _type, limit)
sl.Securities = &securities
return &sl
2015-06-27 08:31:38 -04:00
} else {
return NewError(3 /*Invalid Request*/)
2015-06-27 08:31:38 -04:00
}
}