added dividend db table and insert by trade
This commit is contained in:
@@ -2,6 +2,7 @@ package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -55,21 +56,28 @@ const (
|
||||
BondTrade
|
||||
)
|
||||
|
||||
type TradeType bool
|
||||
type TradeType int
|
||||
|
||||
const (
|
||||
Buy TradeType = true
|
||||
Sell TradeType = false
|
||||
BuyType TradeType = iota // 0
|
||||
SellType // 1
|
||||
DividendType // 2
|
||||
)
|
||||
|
||||
type AddTradeRequest struct {
|
||||
Symbol string `json:"symbol"`
|
||||
Shares int `json:"shares"`
|
||||
Product int `json:"product"`
|
||||
Type bool `json:"type"`
|
||||
Type int `json:"type"` // was bool, now int
|
||||
Price float64 `json:"price"`
|
||||
CurrencyCode string `json:"currency_code"`
|
||||
Date time.Time `json:"date"`
|
||||
|
||||
// Dividend-specific fields (only populated when Type == 2)
|
||||
TaxAmount float64 `json:"tax_amount,omitempty"`
|
||||
TaxRate float64 `json:"tax_rate,omitempty"`
|
||||
NetValue float64 `json:"net_value,omitempty"`
|
||||
PaymentDate time.Time `json:"payment_date,omitempty"`
|
||||
}
|
||||
|
||||
type Trade struct {
|
||||
@@ -107,6 +115,38 @@ func (r *AddTradeRequest) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r AddTradeRequest) ToDividend() (Dividend, error) {
|
||||
if TradeType(r.Type) != DividendType {
|
||||
return Dividend{}, fmt.Errorf("trade type is not a dividend")
|
||||
}
|
||||
return Dividend{
|
||||
Symbol: r.Symbol,
|
||||
CurrencyCode: r.CurrencyCode,
|
||||
Product: TradeProduct(r.Product),
|
||||
Value: r.Price, // gross value
|
||||
PaymentDate: r.PaymentDate,
|
||||
TaxAmount: r.TaxAmount,
|
||||
TaxRate: r.TaxRate,
|
||||
NetValue: r.NetValue,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r AddTradeRequest) ToTrade() (Trade, error) {
|
||||
t := TradeType(r.Type)
|
||||
if t != BuyType && t != SellType {
|
||||
return Trade{}, fmt.Errorf("trade type is not buy or sell")
|
||||
}
|
||||
return Trade{
|
||||
Symbol: r.Symbol,
|
||||
CurrencyCode: r.CurrencyCode,
|
||||
Shares: r.Shares,
|
||||
Product: TradeProduct(r.Product),
|
||||
Type: t,
|
||||
Price: r.Price,
|
||||
Date: r.Date,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// for now trades and none stock position will not be supported.
|
||||
type Portifolio struct {
|
||||
Positions []Position
|
||||
|
||||
Reference in New Issue
Block a user