/// <summary> /// Transfers shares of a stock to the specified account. /// </summary> /// <param name="accountId">Id of the account that "buys" the shares.</param> /// <param name="stockId">Id of the stock of which shares are "bought".</param> /// <param name="sharesCount">Number of shares that should be "bought".</param> /// <param name="maxShareValue">Maximum value for an individual share; if the current value exceeds this value, an exception will be thrown.</param> /// <returns>A task that completes when the operation has been completed.</returns> public async Task BuyShares(int accountId, int stockId, int sharesCount, decimal?maxShareValue) { var accountStatus = await _accountTransactionsAggregator.AggregateTransactionsOfAccount(accountId); var stock = await _entityQuerySource.GetById <Stock>(stockId); var shareValue = await _stockInfoService.GetCurrentShareValue(stock); if (shareValue > maxShareValue) { throw new Exception("Zu teuer"); } var total = shareValue * sharesCount; if (total > accountStatus.Balance) { throw new Exception("Zu wenig guthaben"); } await _entityRepositoryFactory.Use(repository => { repository.Add(new StockTransaction { AccountId = accountId, SharesCount = sharesCount, ShareValue = shareValue, TransactionTimestamp = DateTime.UtcNow, StockId = stockId }); }); }
/// <summary> /// Changes the password of the specified account. /// </summary> /// <param name="email">E-Mail Address of the account of which to change the password.</param> /// <param name="newPassword">Password that should be used for the account.</param> /// <returns>A Task that will complete when the password has been changed.</returns> public async Task ChangePassword(int accountId, string newPassword) { var account = await _entityQuerySource.GetById <Account>(accountId); var hashedPassword = HashedPassword.CreateFromPlainPassword(newPassword); await _entityRepositoryFactory.Use(repository => { repository.Change <Account>(account.Id, a => { a.PasswordHash = hashedPassword.Hash; a.PasswordSalt = hashedPassword.Salt; }); }); }