public ActionResult <StatisticsViewModel> DeleteSell(int id) { var statisticsDto = new StatisticsDTO(); var currentUserName = User.FindFirst(ClaimTypes.Name).Value; var currentUserDto = _userService.GetUserByName(currentUserName, statisticsDto); var sellOfferToDelete = _sellOfferService.GetById(id, statisticsDto); if (sellOfferToDelete != null) { if (currentUserDto.Id == sellOfferToDelete.SellerId) { if (_sellOfferService.Delete(id, statisticsDto)) { //if offer exists, belongs to user and is sucessfully deleted add amount to share amount var share = _shareService.GetShareById(sellOfferToDelete.ShareId, statisticsDto); share.Amount += sellOfferToDelete.Amount; _shareService.EditShare(share.Id, share, statisticsDto); return(Mapper.Map <StatisticsViewModel>(statisticsDto)); } } } return(NotFound(Mapper.Map <StatisticsViewModel>(statisticsDto))); }
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); }