public bool Edit(SellOfferDTO sellOffer, StatisticsDTO statistics) { var offer = _sellOfferRepository.GetById(sellOffer.Id); statistics.SelectsTime += _sellOfferRepository.LastOperationTime; statistics.SelectsCount++; if (offer == null) { return(false); } offer.Price = sellOffer.Price; offer.Amount = sellOffer.Amount; _sellOfferRepository.Edit(offer); statistics.UpdatesTime += _sellOfferRepository.LastOperationTime; statistics.UpdatesCount++; return(true); }
public void FindBuyOffers(SellOfferDTO sellOffer, UserDTO currentUser, StatisticsDTO statistics) { //get all buy offers for stock specified in offer var share = _shareService.GetShareById(sellOffer.ShareId, statistics); if (share.Amount < sellOffer.Amount) { return; } var offers = _buyOfferService.GetAll(statistics) .Where(o => o.StockId == share.StockId) //check if stock matched .Where(o => o.Price >= sellOffer.Price) //price greater or equeal one in created offer .Where(o => o.BuyerId != currentUser.Id) //remove current user offers .OrderByDescending(o => o.Price); //order by most expensive - shares goes to a highest bidder foreach (var offer in offers) { int tradedAmount = 0; //after this case offer is empty and we don't add it to database if (sellOffer.Amount < offer.Amount) { //sold some amount of shares to some user and subtract amount from this users offer offer.Amount -= sellOffer.Amount; _buyOfferService.Edit(offer, statistics); //subtract those sold shares from current user share entry share.Amount -= sellOffer.Amount; _shareService.EditShare(share.Id, share, statistics); //add sold shares to buyer share or create if doesn't have one var buyerShare = _shareService.GetAllShares(statistics).Where(c => c.OwnerId == offer.BuyerId).FirstOrDefault(); if (buyerShare != null) { buyerShare.Amount += sellOffer.Amount; _shareService.EditShare(buyerShare.Id, buyerShare, statistics); } else { buyerShare = new ShareDTO { OwnerId = currentUser.Id, Amount = sellOffer.Amount, StockId = offer.StockId, }; _shareService.AddShare(buyerShare, statistics); } //give money to current user(share holder) currentUser.Value += sellOffer.Amount * sellOffer.Price; currentUser.Password = null; _userService.EditUser(currentUser.Id, currentUser, statistics); tradedAmount = sellOffer.Amount; sellOffer.Amount = 0; } //this case implies that amount in created offer is greater than selected offer else { sellOffer.Amount -= offer.Amount; share.Amount -= offer.Amount; _shareService.EditShare(share.Id, share, statistics); currentUser.Value += offer.Amount * offer.Price; currentUser.Password = null; _userService.EditUser(currentUser.Id, currentUser, statistics); _buyOfferService.Delete(offer.Id, statistics); } //adding transaction for each iteration of trading TransactionDTO transaction = new TransactionDTO { Amount = tradedAmount, Price = offer.Price, BuyerId = offer.BuyerId, SellerId = currentUser.Id, StockId = offer.StockId, Date = DateTime.Now }; _transactionService.Add(transaction, statistics); } if (sellOffer.Amount > 0) { sellOffer.SellerId = currentUser.Id; _sellOfferService.Add(sellOffer, statistics); //freeze currentuser shares equivalent to amount left in offer share.Amount -= sellOffer.Amount; _shareService.EditShare(share.Id, share, statistics); } CalculatePriceChange(share.StockId, statistics); }
public void Add(SellOfferDTO sellOffer, StatisticsDTO statistics) { _sellOfferRepository.Add(Mapper.Map <SellOffer>(sellOffer)); statistics.SelectsTime += _sellOfferRepository.LastOperationTime; statistics.SelectsCount++; }