public async Task <IActionResult> Post(ClientOperationDto clientOperation)
        {
            try
            {
                decimal currentBalance;
                switch (clientOperation.Type)
                {
                case OperationType.Enroll:
                    currentBalance = await FinanceService.EnrollAsync(clientOperation);

                    return(Ok($"Money was succesfully enrolled. Current Balance = {currentBalance}"));

                case OperationType.Withdraw:
                    currentBalance = await FinanceService.WithdrawAsync(clientOperation);

                    return(Ok($"Money was succesfully withdrawed. Current Balance = {currentBalance}"));

                default:
                    return(BadRequest("Operation type not recognized"));
                }
            }
            catch (KeyNotFoundException ex)
            {
                return(NotFound(ex.Message));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
Exemple #2
0
        public async Task <decimal> EnrollAsync(ClientOperationDto clientOperation)
        {
            var client = await GetClientAsync(clientOperation.Id);

            if (clientOperation.TransactionAmount <= 0)
            {
                throw new ArgumentException("The enrollment amount must be greater than zero");
            }
            client.AccountBalance += clientOperation.TransactionAmount;
            await DbContext.SaveChangesAsync();

            return(client.AccountBalance);
        }
Exemple #3
0
        public async Task <decimal> WithdrawAsync(ClientOperationDto clientOperation)
        {
            var client = await GetClientAsync(clientOperation.Id);

            if (clientOperation.TransactionAmount <= 0)
            {
                throw new ArgumentException("The withdrawal amount must be greater than zero");
            }
            if (clientOperation.TransactionAmount > client.AccountBalance)
            {
                throw new ArgumentException("Not enough money in the account");
            }
            client.AccountBalance -= clientOperation.TransactionAmount;
            await DbContext.SaveChangesAsync();

            return(client.AccountBalance);
        }