-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuysell.go
175 lines (163 loc) · 6.95 KB
/
buysell.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
)
// BuySellAdd .
func BuySellAdd(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var response = make(map[string]interface{})
body := map[string]string{}
r.ParseMultipartForm(32 << 20)
for key, value := range r.Form {
body[key] = value[0]
}
fieldCheck := requiredFiledsCheck(body, buySellRequiredFields)
if len(fieldCheck) > 0 {
SetReponseStatus(w, r, statusCodeBadRequest, fieldCheck+" required", dialogType, response)
return
}
if !isMarketOpen() {
SetReponseStatus(w, r, statusCodeBadRequest, "Order was placed outside of trading hours.", dialogType, response)
return
}
body["status"] = "1"
body["created_date_time"] = time.Now().In(mumbai).String()
tx, err := db.Begin()
if err != nil {
SetReponseStatus(w, r, statusCodeBadRequest, "Order not placed", dialogType, response)
return
}
last := strings.Split(getValueRedis(body["ticker"]), ":")
if len(last) > 1 {
price, _ := strconv.ParseFloat(last[1], 64)
shares, _ := strconv.ParseFloat(body["shares"], 64)
body["price"] = strconv.FormatFloat(price, 'f', 2, 64)
body["invested"] = strconv.FormatFloat(price*shares, 'f', 2, 64)
if price > 0 {
if strings.EqualFold(body["type"], "1") {
amountData, _, _ := selectProcess("select amount from " + accountTable + " where user_id = '" + body["user_id"] + "'")
amount, _ := strconv.ParseFloat(amountData[0]["amount"], 64)
invested, _ := strconv.ParseFloat(body["invested"], 64)
if amount >= invested {
positionMap := map[string]string{
"user_id": body["user_id"],
"ticker": body["ticker"],
"name": body["name"],
"invested": body["invested"],
"shares": body["shares"],
"status": "1",
"created_date_time": body["created_date_time"],
}
if len(body["expiry"]) > 0 {
positionMap["expiry"] = body["expiry"]
}
_, err = tx.Exec(buildInsertStatement(positionTable, positionMap) + " on duplicate key update invested = invested + " + body["invested"] +
", shares = shares + " + body["shares"] + ", modified_date_time = '" + body["created_date_time"] + "'")
if err != nil {
tx.Rollback()
SetReponseStatus(w, r, statusCodeBadRequest, "Order not placed", dialogType, response)
return
}
_, err = tx.Exec("update " + accountTable + " set amount = amount - " + body["invested"] + ", modified_date_time = '" + body["created_date_time"] + "' where user_id = '" + body["user_id"] + "'")
if err != nil {
tx.Rollback()
SetReponseStatus(w, r, statusCodeBadRequest, "Order not placed", dialogType, response)
return
}
delete(body, "expiry")
_, err = tx.Exec(buildInsertStatement(orderTable, body))
if err != nil {
tx.Rollback()
SetReponseStatus(w, r, statusCodeBadRequest, "Order not placed", dialogType, response)
return
}
response["meta"] = setMeta(statusCodeOk, "Order complete", "")
} else {
response["meta"] = setMeta(statusCodeBadRequest, "Insufficient funds. Amount required is "+body["invested"]+", but available amount is "+amountData[0]["amount"], dialogType)
}
} else {
positionData, _, _ := selectProcess("select shares from " + positionTable + " where user_id = '" + body["user_id"] + "' and ticker = '" + body["ticker"] + "'")
sharesAvailable, _ := strconv.ParseFloat(positionData[0]["shares"], 64)
sharesToSell, _ := strconv.ParseFloat(body["shares"], 64)
if sharesAvailable > sharesToSell {
positionMap := map[string]string{
"user_id": body["user_id"],
"ticker": body["ticker"],
"name": body["name"],
"invested": body["invested"],
"shares": body["shares"],
"status": "1",
"expiry": body["expiry"],
"created_date_time": body["created_date_time"],
}
if len(body["expiry"]) > 0 {
positionMap["expiry"] = body["expiry"]
}
_, err = tx.Exec(buildInsertStatement(positionTable, positionMap) + " on duplicate key update invested = invested - " + body["invested"] +
", shares = shares - " + body["shares"] + ", modified_date_time = '" + body["created_date_time"] + "'")
if err != nil {
tx.Rollback()
SetReponseStatus(w, r, statusCodeBadRequest, "Order not placed", dialogType, response)
return
}
_, err = tx.Exec("update " + accountTable + " set amount = amount + " + body["invested"] + ", modified_date_time = '" + body["created_date_time"] + "' where user_id = '" + body["user_id"] + "'")
if err != nil {
tx.Rollback()
SetReponseStatus(w, r, statusCodeBadRequest, "Order not placed", dialogType, response)
return
}
delete(body, "expiry")
_, err = tx.Exec(buildInsertStatement(orderTable, body))
if err != nil {
tx.Rollback()
SetReponseStatus(w, r, statusCodeBadRequest, "Order not placed", dialogType, response)
return
}
response["meta"] = setMeta(statusCodeOk, "Order complete", "")
} else if sharesAvailable == sharesToSell {
_, err = tx.Exec("delete from " + positionTable + " where user_id = '" + body["user_id"] + "' and ticker = '" + body["ticker"] + "'")
if err != nil {
tx.Rollback()
SetReponseStatus(w, r, statusCodeBadRequest, "Order not placed", dialogType, response)
return
}
_, err = tx.Exec("update " + accountTable + " set amount = amount + " + body["invested"] + ", modified_date_time = '" + body["created_date_time"] + "' where user_id = '" + body["user_id"] + "'")
if err != nil {
tx.Rollback()
SetReponseStatus(w, r, statusCodeBadRequest, "Order not placed", dialogType, response)
return
}
delete(body, "expiry")
_, err = tx.Exec(buildInsertStatement(orderTable, body))
if err != nil {
tx.Rollback()
SetReponseStatus(w, r, statusCodeBadRequest, "Order not placed", dialogType, response)
return
}
response["meta"] = setMeta(statusCodeOk, "Order complete", "")
} else {
response["meta"] = setMeta(statusCodeBadRequest, "Insufficient holdings. Only "+positionData[0]["shares"]+" available to sell", dialogType)
}
}
response["price"] = body["price"]
response["invested"] = body["invested"]
response["created_date_time"] = time.Now().In(mumbai).Format("2006-01-02 15:04:05")
} else {
response["meta"] = setMeta(statusCodeBadRequest, "Stock not available for transaction", dialogType)
}
} else {
response["meta"] = setMeta(statusCodeBadRequest, "Stock not available for transaction", dialogType)
}
err = tx.Commit()
if err != nil {
SetReponseStatus(w, r, statusCodeBadRequest, "Order not placed", dialogType, response)
return
}
w.Header().Set("Status", response["meta"].(map[string]string)["status"])
w.WriteHeader(getHTTPStatusCode(response["meta"].(map[string]string)["status"]))
json.NewEncoder(w).Encode(response)
}