Esempio n. 1
0
        private void AddShare()
        {
            io.Print(Environment.NewLine);

            foreach (var trader in traderService.TradersList)
            {
                io.Print(trader + Environment.NewLine);
            }

            io.Print(Environment.NewLine);
            io.Print(phraseProvider.GetPhrase(Phrase.Choose));
            string OwnerId = io.Input();

            io.Print(Environment.NewLine);
            io.Print(phraseProvider.GetPhrase(Phrase.EnterName));
            string Name = io.Input();

            io.Print(phraseProvider.GetPhrase(Phrase.EnterPrice));
            string Price = io.Input();

            io.Print(phraseProvider.GetPhrase(Phrase.EnterQuantity));
            string Quantity = io.Input();

            io.Print(Environment.NewLine);
            string res = shareService.AddShare(Name, Price, Quantity, OwnerId);

            io.Print(res + Environment.NewLine);
        }
Esempio n. 2
0
        public ActionResult <StatisticsViewModel> Post([FromBody] EditShareViewModel model)
        {
            var statisticsDto = new StatisticsDTO();

            service.AddShare(Mapper.Map <ShareDTO>(model), statisticsDto);

            Response.StatusCode = 201;
            return(Mapper.Map <StatisticsViewModel>(statisticsDto));
        }
Esempio n. 3
0
 public ActionResult Create(share c)
 {
     if (ModelState.IsValid)
     {
         shse.AddShare(c);
         ViewBag.idBroker = new SelectList(dir.GetAllBrokers(), "idBroker", "title", c.idBroker);
         int idc = c.idshare;
         SendMail(idc);
         return(RedirectToAction("Index"));
     }
     else
     {
         return(View());
     }
 }
Esempio n. 4
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);
        }