public void ClientsTrade() { string answer = ""; var clients = requestSender.GetTop10Clients(1, 10, out answer); if (clients.Count() > 1) { var tradingClients = clients.OrderBy(x => Guid.NewGuid()).Take(2).ToList(); logger.WriteInfo($"Starting operation between {tradingClients[0].ClientID} and {tradingClients[1].ClientID}"); ClientsSharesEntity shareType = tradingClients[0].ClientsShares.Where(x => x.Amount > 0).OrderBy(x => Guid.NewGuid()).FirstOrDefault(); if (shareType == null) { logger.WriteWarn($"{tradingClients[0].ClientID} not have shares"); return; } int numberOfSoldShares = uniformRandomiser.Next(1, (int)shareType.Amount); TransactionHistoryInfo transaction = new TransactionHistoryInfo() { BuyerClientID = tradingClients[1].ClientID, SellerClientID = tradingClients[0].ClientID, ShareID = shareType.ShareID, Amount = numberOfSoldShares }; requestSender.PostMakeDeal(transaction, out answer); logger.WriteInfo($"Result: {answer}"); } }
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] TransactionHistoryInfo transactionInfo) { if (transactionInfo == null) { return(Request.CreateResponse(new ArgumentException($"Transaction information is empty"))); } var sharesService = clientsSharesService.GetAllClientsShares().Where( x => x.ClientID == transactionInfo.SellerClientID && x.ShareID == transactionInfo.ShareID).FirstOrDefault(); if (sharesService == null) { return(Request.CreateResponse(new ArgumentException($"Combination of client {transactionInfo.SellerClientID} and {transactionInfo.ShareID} not exist"))); } tradingOperationService.SellAndBuyShares(transactionInfo.BuyerClientID, transactionInfo.SellerClientID, sharesService, transactionInfo.Amount); return(Request.CreateResponse(HttpStatusCode.OK)); }
public int Add(TransactionHistoryInfo operationHistoryInfo) { var operationHistory = new TransactionHistoryEntity() { BuyerClientID = operationHistoryInfo.BuyerClientID, SellerClientID = operationHistoryInfo.SellerClientID, Amount = operationHistoryInfo.Amount, ShareID = operationHistoryInfo.ShareID, SumOfOperation = operationHistoryInfo.SumOfOperation, DateTime = operationHistoryInfo.DateTime }; operationHistoryRepository.Add(operationHistory); operationHistoryRepository.SaveChanges(); return(operationHistory.TransactionID); }
public string Run(string[] splittedUserInpit, RequestSender requestSender) { string answer = ""; if (splittedUserInpit.Length < 3) { return("Not enough parameters"); } int firstClientId; if (!int.TryParse(splittedUserInpit[1], out firstClientId)) { return("Cannot parse ID"); } int secondClientId; if (!int.TryParse(splittedUserInpit[2], out secondClientId)) { return("Cannot parse ID"); } int shareID; if (!int.TryParse(splittedUserInpit[2], out shareID)) { return("Cannot parse ID"); } int amount; if (!int.TryParse(splittedUserInpit[2], out amount)) { return("Cannot parse amount"); } TransactionHistoryInfo transactionInfo = new TransactionHistoryInfo() { BuyerClientID = firstClientId, SellerClientID = secondClientId, DateTime = DateTime.Now, Amount = amount, ShareID = shareID, }; requestSender.PostMakeDeal(transactionInfo, out answer); return(answer); }
public void PostMakeDeal(TransactionHistoryInfo transactionInfo, out string answer) { string request = "deal/make"; PostCommand(request, transactionInfo, out answer); }