Esempio n. 1
0
        public ActionResult <double> Credit([Required, FromRoute] string accountNumber,
                                            [BindRequired, FromQuery] double sum,
                                            [Required, FromQuery] string memo)
        {
            double balance;

            try
            {
                memo = Util.Dequotify(memo);

                UserInputValidation.ValidateAccountNumber(accountNumber);
                UserInputValidation.ValidateAmount(sum);
                UserInputValidation.ValidateMemo(memo);

                var userInfo = User.GetUserInfo();
                var username = userInfo.AccountsUsername;

                sum = Util.TruncateMoneyValue(sum);

                using (var session = Logic.Login(Repository, username))
                {
                    Logic.ExecuteTransaction(session, accountNumber, AccountsLib.ETransactionType.Credit, sum, memo);

                    balance = Logic.GetBalance(session, accountNumber);
                }
            }
            catch (AccountsLibException ex)
            {
                return(new ActionFailureResult(new ApiResponse(500, ex.Message)));
            }

            return(balance);
        }
Esempio n. 2
0
        public ActionResult <AccountHistory> GetHistory([Required, FromRoute] string accountNumber)
        {
            AccountHistory history;

            try
            {
                UserInputValidation.ValidateAccountNumber(accountNumber);

                var userInfo = User.GetUserInfo();
                var username = userInfo.AccountsUsername;

                using (var session = Logic.Login(Repository, username))
                {
                    history = new AccountHistory
                    {
                        StartingBalance = 0.0,
                        FinalBalance    = 0.0,

                        History = Logic.GetTransactionHistory(session, accountNumber)
                                  .Select(t => new Transaction(t))
                                  .ToList()
                    };

                    history.FinalBalance +=
                        history.History.Sum(t => (t.Action == ETransactionType.Credit) ? t.Amount : (-t.Amount));
                }
            }
            catch (AccountsLibException ex)
            {
                return(new ActionFailureResult(new ApiResponse(500, ex.Message)));
            }

            return(history);
        }
Esempio n. 3
0
        /// <summary>
        /// Get the history of an account from the Accounts API.
        /// </summary>
        /// <param name="accountNumber">The account to be queried.</param>
        /// <returns>The account history as list of signed doubles.</returns>
        ///
        public async Task <AccountHistory> GetAccountHistory(string accountNumber)
        {
            UserInputValidation.ValidateAccountNumber(accountNumber);

            var response = await this.InvokeApiAsync(Get, $"api/accounts/history/{accountNumber}");

            var content = await response.TryGetResponseContent();

            var history = JsonConvert.DeserializeObject <AccountHistory>(content);

            return(history);
        }
Esempio n. 4
0
        /// <summary>
        /// Get the balance of an account from the Accounts API.
        /// </summary>
        /// <param name="accountNumber">The account to be queried.</param>
        /// <returns>The account balance as a double.</returns>
        ///
        public async Task <double> GetAccountBalance(string accountNumber)
        {
            UserInputValidation.ValidateAccountNumber(accountNumber);

            var response = await this.InvokeApiAsync(Get, $"api/accounts/balance/{accountNumber}");

            var content = await response.TryGetResponseContent();

            var balance = Double.Parse(content);

            return(balance);
        }
Esempio n. 5
0
        /// <summary>
        /// Call the Accounts API to credit an account with some amount.
        /// </summary>
        /// <param name="accountNumber">The account to be modified.</param>
        /// <param name="amount">The (unsigned) amount to be credited to the account.</param>
        /// <param name="memo">A short note to be attached to this transaction.</param>
        /// <returns>The new balance for the modified account.</returns>
        ///
        public async Task <double> CreditAccount(string accountNumber, double amount, string memo)
        {
            UserInputValidation.ValidateAccountNumber(accountNumber);
            UserInputValidation.ValidateAmount(amount);
            UserInputValidation.ValidateMemo(memo);

            var queryString = $"api/accounts/credit/{accountNumber}?sum={Math.Abs(amount)}";

            if (memo != null)
            {
                queryString += $"&memo=\"{memo}\"";
            }

            var response = await this.InvokeApiAsync(Post, queryString);

            var content = await response.TryGetResponseContent();

            var balance = Double.Parse(content);

            return(balance);
        }
Esempio n. 6
0
        public ActionResult <double> GetBalance([Required, FromRoute] string accountNumber)
        {
            double balance;

            try
            {
                UserInputValidation.ValidateAccountNumber(accountNumber);

                var userInfo = User.GetUserInfo();
                var username = userInfo.AccountsUsername;

                using (var session = Logic.Login(Repository, username))
                {
                    balance = Logic.GetBalance(session, accountNumber);
                }
            }
            catch (AccountsLibException ex)
            {
                return(new ActionFailureResult(new ApiResponse(500, ex.Message)));
            }

            return(balance);
        }