Esempio n. 1
0
        public async Task <IActionResult> Post([FromBody] TradeModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_portfolioRepository.Query().Any(p => p.Id == model.PortfolioId))
            {
                throw new HttpStatusCodeException(400, "Share not exists");
            }

            if (!_shareRepository.Query().Any(x => x.Symbol.Equals(model.Symbol)))
            {
                throw new HttpStatusCodeException(400, "Share not exists");
            }

            Trade trade = null;

            switch (model.Action)
            {
            case "BUY":
                trade = await _tradeRepository.Buy(model, () => {
                    return(_shareRepository.GetLatestPrice(model.Symbol));
                });

                break;

            case "SELL":
                trade = await _tradeRepository.Sell(model, () => {
                    return(_shareRepository.GetLatestPrice(model.Symbol));
                });

                break;

            default:
                throw new HttpStatusCodeException(400, "Action Incorrect");
            }

            return(Created("Trade", trade));
        }