public void SellAndBuyShares(int firstClientID, int secondClientID, ClientsSharesEntity shareType, int numberOfSoldShares) { if (numberOfSoldShares > shareType.Amount) { throw new ArgumentException($"Cannot sell {numberOfSoldShares} that more than client have {shareType.Amount}"); } if (firstClientID == secondClientID) { throw new ArgumentException($"Cannot sell shares to yourself"); } ClientsSharesEntity sharesInfo = new ClientsSharesEntity() { ClientID = shareType.ClientID, ShareID = shareType.ShareID, Amount = shareType.Amount - numberOfSoldShares, CostOfOneShare = shareType.CostOfOneShare }; clientsSharesService.UpdateShares(sharesInfo); sharesInfo.ClientID = firstClientID; var secondClientShares = clientsSharesService.GetAllClientsShares().Where(x => x.ClientID == sharesInfo.ClientID && x.ShareID == sharesInfo.ShareID).FirstOrDefault(); if (secondClientShares == null) { ClientsSharesInfo clientsSharesInfo = new ClientsSharesInfo() { ClientID = sharesInfo.ClientID, ShareID = sharesInfo.ShareID, Amount = numberOfSoldShares }; clientsSharesService.AddShares(clientsSharesInfo); } else { sharesInfo.Amount = secondClientShares.Amount + numberOfSoldShares; clientsSharesService.UpdateShares(sharesInfo); } balanceService.ChangeMoney(firstClientID, shareType.CostOfOneShare * numberOfSoldShares); balanceService.ChangeMoney(secondClientID, -(shareType.CostOfOneShare * numberOfSoldShares)); TransactionHistoryInfo operationHistoryInfo = new TransactionHistoryInfo() { BuyerClientID = firstClientID, SellerClientID = secondClientID, ShareID = shareType.ShareID, Amount = numberOfSoldShares, SumOfOperation = shareType.CostOfOneShare * numberOfSoldShares, DateTime = DateTime.Now }; operationHistoryService.Add(operationHistoryInfo); }
public HttpResponseMessage Post([FromBody] ClientsSharesInfo sharesInfo) { shareService.AddShares(sharesInfo); return(Request.CreateResponse(HttpStatusCode.OK)); }