Exemplo n.º 1
0
        public async Task MakeBid(int userId, int lotId, decimal bidValue)
        {
            var lot = _lotsService.GetItem(lotId);

            if (lot == null)
            {
                _errors.AddError(nameof(lot.LotId), "Lot with id doesnt exist");
            }
            var user = _userService.GetItem(userId);

            if (lot.Price >= bidValue)
            {
                _errors.AddError(nameof(bidValue), "Bid must be greater than value price");
            }
            if (user.Money < bidValue)
            {
                _errors.AddError(nameof(bidValue), "Not enough funds");
            }
            if (!_errors.Empty)
            {
                var res = JsonConvert.SerializeObject(_errors.Errors, Formatting.Indented);
                await Clients.Caller.SendAsync(
                    "Error", res);

                return;
            }

            _userService.MakeBid(lot.LotId, userId, bidValue);
            var lastbid  = _bidsService.GetLastBidForLot(lotId);
            var row_info = string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr", lastbid.User.UserName,
                                         lastbid.Price, lastbid.DateOfBid.ToString("f"));
            await Clients.All.SendAsync("BidMade", lotId, row_info, lastbid.Price.ToString("C"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> MakeBid([FromBody] BidDto bid)
        {
            var lot = _lotsService.GetItem(bid.LotId);

            if (lot == null)
            {
                return(BadRequest());
            }
            var user = _usersServices.GetItem(UserId);

            if (lot.Price >= bid.Price)
            {
                ModelState.AddModelError(nameof(bid.Price), "Bid must be greater than value price");
            }
            if (user.Money < bid.Price)
            {
                ModelState.AddModelError(nameof(bid.Price), "Not enough funds");
            }
            if (ModelState.IsValid)
            {
                _usersServices.MakeBid(lot.LotId, UserId, bid.Price);
                var lastbid  = _bidsService.GetLastBidForLot(bid.LotId);
                var row_info = string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr", lastbid.User.UserName,
                                             lastbid.Price.ToString("C"), lastbid.DateOfBid.ToString("f"));
                await _hubcontext.Clients.All.SendAsync("BidMade",
                                                        lot.LotId, row_info, lastbid.Price.ToString("C"));

                return(Ok());
                //return new ObjectResult(new {row = row_info});
            }
            return(new BadRequestObjectResult(ModelState));
        }