public ActionResult <Database.Models.BillPay> Post(TradeStockRequest request) { long memberId = _httpContextAccessor.HttpContext.User.GetMemberId(); var result = _brokerageTradeService.TradeStock(request, memberId); if (result.ResultType == TradeStockType.InvalidTradeType) { return(NotFound($"Trade type {request.TradeType} not found.")); } if (result.ResultType == TradeStockType.AccountNotFoundForMember) { return(NotFound($"Account with guid {request.AccountGuid} not found.")); } if (result.ResultType == TradeStockType.BrokerageAccountNotFoundForMember) { return(NotFound($"Brokerage Account with guid {request.BrokerageAccountGuid} not found.")); } if (result.ResultType == TradeStockType.StockNotFound) { return(NotFound($"Stock with ID {request.StockId} not found.")); } return(CreatedAtAction(nameof(Get), new { guid = result.Trade.Guid }, result.Trade)); }
public TradeStockResult TradeStock(TradeStockRequest request, long memberId) { if (request.TradeType != BrokerageTradeTypes.Buy && request.TradeType != BrokerageTradeTypes.Sell) { return(new TradeStockResult(TradeStockType.InvalidTradeType, null)); } long?brokerageAccountId = _dbContext.BrokerageAccounts .Where(a => a.Guid == request.BrokerageAccountGuid && a.MemberId == memberId) .Select(a => a.Id) .SingleOrDefault(); if (brokerageAccountId == null) { return(new TradeStockResult(TradeStockType.BrokerageAccountNotFoundForMember, null)); } // This is the account they're drawing from to Buy a stock or depositing into if they're Selling a stock long?accountId = _dbContext.Accounts .Where(a => a.Guid == request.AccountGuid && a.MemberId == memberId) .Select(a => a.Id) .SingleOrDefault(); if (accountId == null) { return(new TradeStockResult(TradeStockType.AccountNotFoundForMember, null)); } var stock = _dbContext.BrokerageStocks .Where(s => s.Id == request.StockId) .SingleOrDefault(); if (stock == null) { return(new TradeStockResult(TradeStockType.StockNotFound, null)); } var mostRecentPrice = _dbContext.BrokerageStockPriceHistory .Where(b => b.StockId == stock.Id) .OrderByDescending(b => b.Timestamp) .FirstOrDefault(); var trade = new BrokerageTrade { BrokerageAccountId = brokerageAccountId.Value, StockId = stock.Id, Price = mostRecentPrice.Price, Quantity = request.Quantity, BrokerageTradeType = request.TradeType }; _dbContext.BrokerageTrades.Add(trade); _dbContext.SaveChanges(); return(new TradeStockResult(TradeStockType.Successful, trade)); }
public static async Task <HttpResponseMessage> SendBrokerageTradePostRequest(this HttpClient client, TradeStockRequest request) { var result = await client.PostAsJsonAsync(Base, request); return(result); }