async Task IService <BllBid, int> .Delete(BllBid e)
        {
            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }

            await RemoveBet(e.Id);
        }
        async Task <int> IService <BllBid, int> .Create(BllBid e)
        {
            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }

            await PlaceBet(e.Lot, e.User, e.Price);

            return(-1);
        }
Ejemplo n.º 3
0
 public static DalBid ToDalBid(this BllBid bid)
 {
     return(new DalBid()
     {
         Id = bid.Id,
         DateOfBid = bid.BidDate,
         Lot = bid.Lot,
         Price = bid.Price,
         User = bid.User
     });
 }
        public static Bid ToMvcBid(this BllBid bllBid, IUserService userService)
        {
            var bid = new Bid()
            {
                Id          = bllBid.Id,
                BidDate     = bllBid.BidDate,
                MoneyAmount = bllBid.Price,
                LotId       = bllBid.Lot
            };

            Task <BllUser> task = Task.Run(() => userService.GetById(bllBid.User));

            task.Wait();
            bid.UserName = task.Result.Login;
            return(bid);
        }
        /// <summary>
        /// Update the bid
        /// </summary>
        /// <param name="e">Bid with new info</param>
        public async Task Update(BllBid e)
        {
            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }

            if (e.Id < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(e.Id));
            }

            if (!await context.BidsRepository.IsExist(e.Id))
            {
                throw new ArgumentOutOfRangeException(nameof(e));
            }

            await context.BidsRepository.Update(e.ToDalBid());
        }
        /// <summary>
        /// Place a bet
        /// </summary>
        /// <param name="auctionId">Id of the auction</param>
        /// <param name="userName">User login</param>
        /// <param name="moneyAmount">Count of money</param>
        /// <returns>true - if succeded</returns>
        public async Task <bool> PlaceBet(int auctionId, string userName, decimal moneyAmount)
        {
            if (auctionId < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(auctionId));
            }

            if (string.IsNullOrEmpty(userName))
            {
                throw new ArgumentNullException(nameof(userName));
            }

            if (moneyAmount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(moneyAmount));
            }

            if (await context.LotsRepository.IsExist(auctionId) && await context.UserStore.IsExistingUserAsync(userName))
            {
                BllBid bid = (await context.BidsRepository.FindLastLotBid(auctionId)).ToBllBid();

                // if bid equals null that mean that current bid is the first bid
                if (bid == null)
                {
                    await context.BidsRepository.Create(new DalBid()
                    {
                        DateOfBid = DateTime.Now,
                        Price     = moneyAmount,
                        User      = (await context.UserStore.FindByNameAsync(userName)).Id,
                        Lot       = auctionId
                    });
                }
                else
                {
                    if (bid.Price < moneyAmount)
                    {
                        await context.BidsRepository.Create(new DalBid()
                        {
                            DateOfBid = DateTime.Now,
                            Price     = moneyAmount,
                            User      = (await context.UserStore.FindByNameAsync(userName)).Id,
                            Lot       = auctionId
                        });
                    }
                    else
                    {
                        return(false);
                    }
                }

                context.Commit();
                var lot = await context.LotsRepository.GetById(auctionId);

                lot.Price = moneyAmount;
                await context.LotsRepository.Update(lot);

                return(true);
            }
            else
            {
                return(false);
            }
        }