public bool Edit(BuyOfferDTO buyOffer, StatisticsDTO statistics)
        {
            var offer = _buyOfferRepository.GetById(buyOffer.Id);

            statistics.SelectsTime += _buyOfferRepository.LastOperationTime;
            statistics.SelectsCount++;

            if (offer == null)
            {
                return(false);
            }

            offer.Price  = buyOffer.Price;
            offer.Amount = buyOffer.Amount;

            _buyOfferRepository.Edit(offer);
            statistics.UpdatesTime += _buyOfferRepository.LastOperationTime;
            statistics.UpdatesCount++;
            return(true);
        }
Exemple #2
0
        public void FindSellOffers(BuyOfferDTO buyOffer, UserDTO currentUser, StatisticsDTO statistics)
        {
            if (currentUser.Value < buyOffer.Amount * buyOffer.Price)
            {
                return;
            }

            var offers = _sellOfferService.GetAll(statistics).Where(o => o.Price <= buyOffer.Price).OrderBy(o => o.Price);

            foreach (var offer in offers)
            {
                int tradedAmount;

                if (buyOffer.Amount < offer.Amount)
                {
                    var price = offer.Amount * offer.Price;

                    offer.Amount -= buyOffer.Amount;
                    tradedAmount  = buyOffer.Amount;
                    var targetUser = _userService.GetUserById(offer.SellerId, statistics);

                    //money got exchanged
                    targetUser.Value    += price;
                    currentUser.Value   -= price;
                    currentUser.Password = null;
                    targetUser.Password  = null;
                    _userService.EditUser(currentUser.Id, currentUser, statistics);
                    _userService.EditUser(targetUser.Id, targetUser, statistics);

                    //shares were taken from source
                    var targetedShare = _shareService.GetShareById(offer.ShareId, statistics);
                    targetedShare.Amount -= buyOffer.Amount;
                    _shareService.EditShare(targetedShare.Id, targetedShare, statistics);

                    //if current user has company share add them if not create entry for them
                    var currentUserShare = _shareService.GetAllShares(statistics).Where(c => c.OwnerId == currentUser.Id).Where(c => c.StockId == buyOffer.StockId).FirstOrDefault();
                    if (currentUserShare != null)
                    {
                        currentUserShare.Amount += buyOffer.Amount;
                        _shareService.EditShare(currentUserShare.Id, currentUserShare, statistics);
                    }
                    else
                    {
                        currentUserShare = new ShareDTO
                        {
                            OwnerId = currentUser.Id,
                            Amount  = buyOffer.Amount,
                            StockId = buyOffer.StockId,
                        };
                        _shareService.AddShare(currentUserShare, statistics);
                    }

                    //Modify offer that we took shares from
                    _sellOfferService.Edit(offer, statistics);
                    buyOffer.Amount = 0;
                }
                else
                {
                    var price = offer.Amount * offer.Price;

                    buyOffer.Amount -= offer.Amount;
                    tradedAmount     = offer.Amount;
                    var targetUser = _userService.GetUserById(offer.SellerId, statistics);

                    //money got exchanged
                    targetUser.Value    += price;
                    currentUser.Value   -= price;
                    currentUser.Password = null;
                    targetUser.Password  = null;
                    _userService.EditUser(currentUser.Id, currentUser, statistics);
                    _userService.EditUser(targetUser.Id, targetUser, statistics);

                    //taking shares from seller offer
                    var targetedShare = _shareService.GetShareById(offer.ShareId, statistics);
                    targetedShare.Amount -= offer.Amount;
                    _shareService.EditShare(targetedShare.Id, targetedShare, statistics);

                    //giving shares to buyer
                    var currentUserShare = _shareService.GetAllShares(statistics).Where(c => c.OwnerId == currentUser.Id).Where(c => c.StockId == buyOffer.StockId).FirstOrDefault();
                    if (currentUserShare != null)
                    {
                        currentUserShare.Amount += buyOffer.Amount;
                        _shareService.EditShare(currentUserShare.Id, currentUserShare, statistics);
                    }
                    else
                    {
                        currentUserShare = new ShareDTO
                        {
                            OwnerId = currentUser.Id,
                            Amount  = buyOffer.Amount,
                            StockId = buyOffer.StockId,
                        };
                        _shareService.AddShare(currentUserShare, statistics);
                    }

                    //deleting offer
                    _sellOfferService.Delete(offer.Id, statistics);
                }

                TransactionDTO transaction = new TransactionDTO
                {
                    Amount   = tradedAmount,
                    Price    = offer.Price,
                    BuyerId  = currentUser.Id,
                    SellerId = offer.SellerId,
                    StockId  = buyOffer.StockId,
                    Date     = DateTime.Now
                };
                _transactionService.Add(transaction, statistics);
            }

            if (buyOffer.Amount > 0)
            {
                buyOffer.BuyerId = currentUser.Id;
                _buyOfferService.Add(buyOffer, statistics);
                //freeze users money equivalent to amount of shares he wants to buy left after searching through the market
                currentUser.Value   -= buyOffer.Amount * buyOffer.Price;
                currentUser.Password = null;
                _userService.EditUser(currentUser.Id, currentUser, statistics);
            }

            CalculatePriceChange(buyOffer.StockId, statistics);
        }
 public void Add(BuyOfferDTO buyOffer, StatisticsDTO statistics)
 {
     _buyOfferRepository.Add(Mapper.Map <BuyOffer>(buyOffer));
     statistics.SelectsTime += _buyOfferRepository.LastOperationTime;
     statistics.SelectsCount++;
 }