forked from aclindsa/moneygo
/
prices.go
133 lines (113 loc) · 2.94 KB
/
prices.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package handlers
import (
"log"
"net/http"
"github.com/GanGral/moneygo/internal/models"
"github.com/GanGral/moneygo/internal/store"
)
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.InsertPrice(price)
if err != nil {
return err
}
return nil
}
exists, err := tx.PriceExists(price)
if err != nil {
return err
}
if exists {
return nil // price already exists
}
err = tx.InsertPrice(price)
if err != nil {
return err
}
return nil
}
func PriceHandler(r *http.Request, context *Context, user *models.User, securityid int64) ResponseWriterWriter {
security, err := context.Tx.GetSecurity(securityid, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
if r.Method == "POST" {
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*/)
}
_, err = context.Tx.GetSecurity(price.CurrencyId, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
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
var pl models.PriceList
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*/)
}
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" {
var price models.Price
if err := ReadJSON(r, &price); err != nil || price.PriceId != priceid {
return NewError(3 /*Invalid Request*/)
}
_, err = context.Tx.GetSecurity(price.SecurityId, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
_, err = context.Tx.GetSecurity(price.CurrencyId, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
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 := context.Tx.GetPrice(priceid, security.SecurityId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
err = context.Tx.DeletePrice(price)
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
return SuccessWriter{}
}
}
return NewError(3 /*Invalid Request*/)
}