public async Task <IHttpActionResult> MakeABid([FromBody] BidViewModel bid, string auctionId)
        {
            bid.DateTime = DateTime.Now;
            var auction = config.AuctionHouses.Search(auctionId);

            if (auction == null)
            {
                BadRequest();
            }

            Auctions.SetAuction(auction);
            var product = await _productService.GetProductAsync(bid.ProductId);

            if (product == null)
            {
                BadRequest();
            }

            var user = await _customUserManager.FindByIdAsync(bid.UserId.ToString());

            if (user == null)
            {
                BadRequest();
            }

            var lastBid = await _bidService.ShowLastBidForProductAsync(product.Id);

            bid.Id = Guid.NewGuid();
            if (lastBid == null && bid.Price > BidStep)
            {
                await _bidService.AddBidAsync(Mapper.Map <BussinessLogic.Models.Bid>(bid));

                return(Ok());
            }

            if (bid.Price >= lastBid.Price + BidStep && lastBid.DateTime < bid.DateTime)
            {
                await _bidService.AddBidAsync(Mapper.Map <BussinessLogic.Models.Bid>(bid));

                return(Ok());
            }

            return(BadRequest());
        }
Exemplo n.º 2
0
        public async Task <JsonResult> Create(string data)
        {
            dynamic answerFromServer = new ExpandoObject();

            answerFromServer.errorMessage = string.Empty;
            answerFromServer.response     = string.Empty;
            answerFromServer.userName     = string.Empty;
            BidViewModel bid = new BidViewModel()
            {
                Id = Guid.NewGuid()
            };

            if (Request.IsAjaxRequest())
            {
                try
                {
                    dynamic bidClient = JsonConvert.DeserializeObject(data);

                    var auctionModel = config.AuctionHouses.Search(bidClient.auction.ToString());
                    if (auctionModel != null)
                    {
                        Auctions.SetAuction(auctionModel);
                    }

                    if ((bidClient.userId == null) || (bidClient.productId == null))
                    {
                        return(answerFromServer.errorMessage = "userId or productId not found in database");
                    }

                    bid.UserId = (bidClient.userId == null) ? Guid.Empty : Guid.Parse(bidClient.userId.ToString());
                    var user = await _customUserManager.FindByIdAsync(bid.UserId.ToString());

                    bid.DateTime  = DateTime.Now.Add(TimeZoneHelper.ConverTimeToServer(user.TimezoneId));
                    bid.Price     = Convert.ToInt32(bidClient.price);
                    bid.ProductId = Guid.Parse(bidClient.productId.ToString());
                    var lastBid = await _bidService.ShowLastBidForProductAsync(bid.ProductId);

                    ProductDTO product = new ProductDTO();
                    if (lastBid == null)
                    {
                        product = await _productService.GetProductAsync(bid.ProductId);
                    }

                    if ((lastBid == null && bid.Price >= product.StartPrice + BidStep) || (lastBid.DateTime < bid.DateTime && lastBid.Price + BidStep <= bid.Price && bid.UserId != Guid.Empty))
                    {
                        answerFromServer.userName = user.FullName;
                        answerFromServer.response = bid;
                        _logger.Info(string.Format("user make to bid on product  - {0}, username -{1}, timezone - {2}, auction - {3} , productId - {4}", user.Id, user.FullName, user.TimezoneId, auctionModel, product.Id));
                        await _bidService.AddBidAsync(Mapper.Map <Bid>(bid));
                    }
                    else
                    {
                        answerFromServer.errorMessage = "Not valid params. Obviously this problem to start when not correct price/data ";
                    }
                }
                catch
                {
                    answerFromServer.errorMessage = "not valid data";
                }
            }

            return(Json(JsonConvert.SerializeObject(answerFromServer, Formatting.None)));
        }