Beispiel #1
0
        public async Task <IActionResult> BuyNow([FromBody] BuyNowModel model)
        {
            var product = await _dbContext.Products.FindAsync(model.ProductId);

            if (product == null)
            {
                _logger.LogInformation("Product {productId} not found", model.ProductId);
                return(NotFound());
            }

            if (!product.BuyItNowPrice.HasValue)
            {
                return(BadRequest("Buy it now not enabled for specified product."));
            }

            var evt = await _dbContext.Events.FindAsync(product.EventId);

            if (evt == null)
            {
                _logger.LogInformation("Event {eventId} not found", product.EventId);
                return(NotFound());
            }

            var user = await _userManager.GetUserAsync(User);

            if (product.IsPurchased)
            {
                return(BadRequest("Product has already been purchased."));
            }

            try
            {
                var placeBidRequest = new PlaceBidRequest
                {
                    Event   = evt,
                    Product = product,
                    User    = user,
                    Amount  = product.BuyItNowPrice.Value
                };
                var bidResult = await _bidService.PlaceBidAsync(placeBidRequest);

                // TODO: probably would be wise to move this into the BidService
                product.PurchasedDate   = DateTime.UtcNow;
                product.PurchasedUserId = user.Id;

                _dbContext.Products.Update(product);
                await _dbContext.SaveChangesAsync();

                return(Created("", null));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error procesing buy it now for product ID {id}", model.ProductId);
                return(StatusCode(500));
            }
        }
Beispiel #2
0
        public async Task <IActionResult> Create([FromBody] BidModel model)
        {
            // TODO: Validations
            // - Make sure user can bid
            // - Make sure last bid wasn't current user

            var product = await _dbContext.Products.FindAsync(model.ProductId);

            if (product == null)
            {
                _logger.LogInformation("Product {productId} not found", model.ProductId);
                return(NotFound());
            }

            var evt = await _dbContext.Events.FindAsync(product.EventId);

            if (evt == null)
            {
                _logger.LogInformation("Event {eventId} not found", product.EventId);
                return(NotFound());
            }

            var user = await _userManager.GetUserAsync(User);

            var placeBidRequest = new PlaceBidRequest
            {
                Event   = evt,
                Product = product,
                User    = user,
                Amount  = model.Amount
            };

            try
            {
                var bidResult = await _bidService.PlaceBidAsync(placeBidRequest);

                if (bidResult.ResultType == PlaceBidResultType.BiddingClosed)
                {
                    return(BadRequest("Bidding for event is not open"));
                }
                else if (bidResult.ResultType == PlaceBidResultType.InvalidAmount)
                {
                    return(BadRequest($"Bid amount must be {product.NextMinBidAmount} or greater"));
                }

                // Success
                return(Created("", model));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error placing bid for product ID {id} (User: {userId})", model.ProductId, user.Id);
                return(StatusCode(500)); // TODO: Return something different
            }
        }
Beispiel #3
0
        public async Task <IActionResult> Buy([Bind("Id,TimeOut,AmountOfShares,Price")] BuyStockViewModel createStockViewModel)
        {
            var placeBidRequest = new PlaceBidRequest
            {
                AmountOfShares = createStockViewModel.AmountOfShares,
                TimeOut        = createStockViewModel.TimeOut,
                Price          = createStockViewModel.Price,
                StockId        = createStockViewModel.Id
            };

            var(jwtToken, _) = JwtHelper.GetJwtAndIdFromJwt(Request);
            var validationResult = await _stockShareRequesterClient.PlaceBid(placeBidRequest, jwtToken);

            if (validationResult.Valid)
            {
                return(await StockList());
            }
            ViewBag.ShowErrorDialog = true;
            ViewBag.ErrorText       = validationResult.ErrorMessage;
            return(View(createStockViewModel));
        }
Beispiel #4
0
 public async Task <ValidationResult> PlaceBid(PlaceBidRequest placeBidRequest, string jwtToken)
 {
     return(await PolicyHelper.ThreeRetriesAsync().ExecuteAsync(() =>
                                                                _stockShareRequester.BaseAddress.AppendPathSegment(_stockShareRequester.StockShareRequesterPath.StockBid)
                                                                .WithOAuthBearerToken(jwtToken).PostJsonAsync(placeBidRequest).ReceiveJson <ValidationResult>()));
 }