|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "github.com/gin-gonic/gin" |
| 7 | + "github.com/munisp/NGApp/services/gateway/internal/models" |
| 8 | +) |
| 9 | + |
| 10 | +// ============================================================ |
| 11 | +// Forex Trading API Handlers |
| 12 | +// ============================================================ |
| 13 | + |
| 14 | +// --- FX Pairs --- |
| 15 | + |
| 16 | +func (s *Server) fxListPairs(c *gin.Context) { |
| 17 | + category := c.Query("category") |
| 18 | + pairs := s.store.GetFXPairs(category) |
| 19 | + c.JSON(http.StatusOK, models.APIResponse{ |
| 20 | + Success: true, |
| 21 | + Data: pairs, |
| 22 | + Meta: models.PaginationMeta{Total: len(pairs), Page: 1, Limit: len(pairs), Pages: 1}, |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +func (s *Server) fxGetPair(c *gin.Context) { |
| 27 | + symbol := c.Param("pair") |
| 28 | + pair, ok := s.store.GetFXPair(symbol) |
| 29 | + if !ok { |
| 30 | + c.JSON(http.StatusNotFound, models.APIResponse{Success: false, Error: "FX pair not found: " + symbol}) |
| 31 | + return |
| 32 | + } |
| 33 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: pair}) |
| 34 | +} |
| 35 | + |
| 36 | +func (s *Server) fxSearchPairs(c *gin.Context) { |
| 37 | + q := c.Query("q") |
| 38 | + if q == "" { |
| 39 | + c.JSON(http.StatusBadRequest, models.APIResponse{Success: false, Error: "query parameter 'q' required"}) |
| 40 | + return |
| 41 | + } |
| 42 | + results := s.store.SearchFXPairs(q) |
| 43 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: results}) |
| 44 | +} |
| 45 | + |
| 46 | +// --- FX Orders --- |
| 47 | + |
| 48 | +func (s *Server) fxListOrders(c *gin.Context) { |
| 49 | + userID := s.getUserID(c) |
| 50 | + status := c.Query("status") |
| 51 | + orders := s.store.GetFXOrders(userID, status) |
| 52 | + c.JSON(http.StatusOK, models.APIResponse{ |
| 53 | + Success: true, |
| 54 | + Data: orders, |
| 55 | + Meta: models.PaginationMeta{Total: len(orders), Page: 1, Limit: len(orders), Pages: 1}, |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +func (s *Server) fxGetOrder(c *gin.Context) { |
| 60 | + orderID := c.Param("id") |
| 61 | + order, ok := s.store.GetFXOrder(orderID) |
| 62 | + if !ok { |
| 63 | + c.JSON(http.StatusNotFound, models.APIResponse{Success: false, Error: "FX order not found"}) |
| 64 | + return |
| 65 | + } |
| 66 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: order}) |
| 67 | +} |
| 68 | + |
| 69 | +func (s *Server) fxCreateOrder(c *gin.Context) { |
| 70 | + var req models.CreateFXOrderRequest |
| 71 | + if err := c.ShouldBindJSON(&req); err != nil { |
| 72 | + c.JSON(http.StatusBadRequest, models.APIResponse{Success: false, Error: err.Error()}) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + // Validate pair exists |
| 77 | + pair, ok := s.store.GetFXPair(req.Pair) |
| 78 | + if !ok { |
| 79 | + c.JSON(http.StatusBadRequest, models.APIResponse{Success: false, Error: "unknown FX pair: " + req.Pair}) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + // Validate lot size |
| 84 | + if req.LotSize < pair.MinLotSize || req.LotSize > pair.MaxLotSize { |
| 85 | + c.JSON(http.StatusBadRequest, models.APIResponse{ |
| 86 | + Success: false, |
| 87 | + Error: "lot size must be between min and max for this pair", |
| 88 | + }) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + // Validate leverage |
| 93 | + if req.Leverage > pair.MaxLeverage { |
| 94 | + c.JSON(http.StatusBadRequest, models.APIResponse{ |
| 95 | + Success: false, |
| 96 | + Error: "leverage exceeds maximum for this pair", |
| 97 | + }) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + // Validate OCO order has both prices |
| 102 | + if req.Type == models.FXOrderOCO && (req.OCOStopPrice == 0 || req.OCOLimitPrice == 0) { |
| 103 | + c.JSON(http.StatusBadRequest, models.APIResponse{ |
| 104 | + Success: false, |
| 105 | + Error: "OCO orders require both ocoStopPrice and ocoLimitPrice", |
| 106 | + }) |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + userID := s.getUserID(c) |
| 111 | + order := models.FXOrder{ |
| 112 | + UserID: userID, |
| 113 | + Pair: req.Pair, |
| 114 | + Side: req.Side, |
| 115 | + Type: req.Type, |
| 116 | + LotSize: req.LotSize, |
| 117 | + Price: req.Price, |
| 118 | + StopLoss: req.StopLoss, |
| 119 | + TakeProfit: req.TakeProfit, |
| 120 | + TrailingStopPips: req.TrailingStopPips, |
| 121 | + OCOStopPrice: req.OCOStopPrice, |
| 122 | + OCOLimitPrice: req.OCOLimitPrice, |
| 123 | + Leverage: req.Leverage, |
| 124 | + Comment: req.Comment, |
| 125 | + } |
| 126 | + |
| 127 | + created := s.store.CreateFXOrder(order) |
| 128 | + |
| 129 | + // Publish order event via Kafka (fallback: no-op) |
| 130 | + s.kafka.ProduceAsync("fx-orders", created.ID, created) |
| 131 | + |
| 132 | + c.JSON(http.StatusCreated, models.APIResponse{Success: true, Data: created}) |
| 133 | +} |
| 134 | + |
| 135 | +func (s *Server) fxCancelOrder(c *gin.Context) { |
| 136 | + orderID := c.Param("id") |
| 137 | + order, err := s.store.CancelFXOrder(orderID) |
| 138 | + if err != nil { |
| 139 | + c.JSON(http.StatusBadRequest, models.APIResponse{Success: false, Error: err.Error()}) |
| 140 | + return |
| 141 | + } |
| 142 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: order}) |
| 143 | +} |
| 144 | + |
| 145 | +// --- FX Positions --- |
| 146 | + |
| 147 | +func (s *Server) fxListPositions(c *gin.Context) { |
| 148 | + userID := s.getUserID(c) |
| 149 | + status := c.DefaultQuery("status", "OPEN") |
| 150 | + positions := s.store.GetFXPositions(userID, status) |
| 151 | + c.JSON(http.StatusOK, models.APIResponse{ |
| 152 | + Success: true, |
| 153 | + Data: positions, |
| 154 | + Meta: models.PaginationMeta{Total: len(positions), Page: 1, Limit: len(positions), Pages: 1}, |
| 155 | + }) |
| 156 | +} |
| 157 | + |
| 158 | +func (s *Server) fxGetPosition(c *gin.Context) { |
| 159 | + posID := c.Param("id") |
| 160 | + pos, ok := s.store.GetFXPosition(posID) |
| 161 | + if !ok { |
| 162 | + c.JSON(http.StatusNotFound, models.APIResponse{Success: false, Error: "FX position not found"}) |
| 163 | + return |
| 164 | + } |
| 165 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: pos}) |
| 166 | +} |
| 167 | + |
| 168 | +func (s *Server) fxModifyPosition(c *gin.Context) { |
| 169 | + posID := c.Param("id") |
| 170 | + var req models.ModifyFXPositionRequest |
| 171 | + if err := c.ShouldBindJSON(&req); err != nil { |
| 172 | + c.JSON(http.StatusBadRequest, models.APIResponse{Success: false, Error: err.Error()}) |
| 173 | + return |
| 174 | + } |
| 175 | + pos, err := s.store.ModifyFXPosition(posID, req) |
| 176 | + if err != nil { |
| 177 | + c.JSON(http.StatusBadRequest, models.APIResponse{Success: false, Error: err.Error()}) |
| 178 | + return |
| 179 | + } |
| 180 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: pos}) |
| 181 | +} |
| 182 | + |
| 183 | +func (s *Server) fxClosePosition(c *gin.Context) { |
| 184 | + posID := c.Param("id") |
| 185 | + pos, err := s.store.CloseFXPosition(posID) |
| 186 | + if err != nil { |
| 187 | + c.JSON(http.StatusBadRequest, models.APIResponse{Success: false, Error: err.Error()}) |
| 188 | + return |
| 189 | + } |
| 190 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: pos}) |
| 191 | +} |
| 192 | + |
| 193 | +// --- FX Account --- |
| 194 | + |
| 195 | +func (s *Server) fxAccountSummary(c *gin.Context) { |
| 196 | + userID := s.getUserID(c) |
| 197 | + summary := s.store.GetFXAccountSummary(userID) |
| 198 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: summary}) |
| 199 | +} |
| 200 | + |
| 201 | +// --- FX Swap Rates --- |
| 202 | + |
| 203 | +func (s *Server) fxSwapRates(c *gin.Context) { |
| 204 | + rates := s.store.GetFXSwapRates() |
| 205 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: rates}) |
| 206 | +} |
| 207 | + |
| 208 | +// --- FX Cross Rates --- |
| 209 | + |
| 210 | +func (s *Server) fxCrossRates(c *gin.Context) { |
| 211 | + rates := s.store.GetFXCrossRates() |
| 212 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: rates}) |
| 213 | +} |
| 214 | + |
| 215 | +// --- FX Margin Requirements --- |
| 216 | + |
| 217 | +func (s *Server) fxMarginRequirements(c *gin.Context) { |
| 218 | + reqs := s.store.GetFXMarginRequirements() |
| 219 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: reqs}) |
| 220 | +} |
| 221 | + |
| 222 | +// --- FX Liquidity Providers --- |
| 223 | + |
| 224 | +func (s *Server) fxLiquidityProviders(c *gin.Context) { |
| 225 | + providers := s.store.GetFXLiquidityProviders() |
| 226 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: providers}) |
| 227 | +} |
| 228 | + |
| 229 | +// --- FX Regulatory Info --- |
| 230 | + |
| 231 | +func (s *Server) fxRegulatoryInfo(c *gin.Context) { |
| 232 | + info := s.store.GetFXRegulatoryInfo() |
| 233 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: info}) |
| 234 | +} |
| 235 | + |
| 236 | +// --- FX Pip Calculator --- |
| 237 | + |
| 238 | +func (s *Server) fxPipCalculator(c *gin.Context) { |
| 239 | + var req models.FXPipCalculatorRequest |
| 240 | + if err := c.ShouldBindJSON(&req); err != nil { |
| 241 | + c.JSON(http.StatusBadRequest, models.APIResponse{Success: false, Error: err.Error()}) |
| 242 | + return |
| 243 | + } |
| 244 | + result := s.store.CalculateFXPips(req) |
| 245 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: result}) |
| 246 | +} |
| 247 | + |
| 248 | +// --- FX Trading Hours --- |
| 249 | + |
| 250 | +func (s *Server) fxTradingHours(c *gin.Context) { |
| 251 | + pairs := s.store.GetFXPairs("") |
| 252 | + type hourInfo struct { |
| 253 | + Pair string `json:"pair"` |
| 254 | + TradingHours string `json:"tradingHours"` |
| 255 | + Active bool `json:"active"` |
| 256 | + Category string `json:"category"` |
| 257 | + } |
| 258 | + var hours []hourInfo |
| 259 | + for _, p := range pairs { |
| 260 | + hours = append(hours, hourInfo{ |
| 261 | + Pair: p.Symbol, |
| 262 | + TradingHours: p.TradingHours, |
| 263 | + Active: p.Active, |
| 264 | + Category: p.Category, |
| 265 | + }) |
| 266 | + } |
| 267 | + c.JSON(http.StatusOK, models.APIResponse{Success: true, Data: hours}) |
| 268 | +} |
0 commit comments