Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from CMS18/feature/2-deposit-withdraw
Did the things and the whatnot
- Loading branch information
Showing
16 changed files
with
318 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using ALM.Web; | ||
using ALM.Web.Models; | ||
using Shouldly; | ||
using System; | ||
using Xunit; | ||
|
||
namespace ALM.Test | ||
{ | ||
//public class Fixture | ||
//{ | ||
// public BankRepository Repository { get; set; } | ||
// public Transactioner TransactionManager { get; set; } | ||
|
||
// public Fixture() | ||
// { | ||
// Repository = new BankRepository(); | ||
// Repository.Initialize(); | ||
// TransactionManager = new Transactioner(Repository); | ||
// } | ||
//} | ||
|
||
public class TransactionTests | ||
{ | ||
[Theory] | ||
[InlineData(1337)] | ||
[InlineData(9000)] | ||
[InlineData(80085)] | ||
public void DepositTest(decimal amount) | ||
{ | ||
var account = new Account(); | ||
var transactioner = new Transactioner(); | ||
decimal originalBalance = account.Balance; | ||
|
||
transactioner.Deposit(account, amount); | ||
|
||
account.Balance.ShouldBe(originalBalance + amount); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1337, 500)] | ||
[InlineData(9000, 100)] | ||
[InlineData(80085, 80085)] | ||
public void WithdrawTest(decimal initial, decimal amount) | ||
{ | ||
var account = new Account(); | ||
account.Credit(initial); | ||
var transactioner = new Transactioner(); | ||
decimal originalBalance = account.Balance; | ||
|
||
transactioner.Deposit(account, amount); | ||
|
||
account.Balance.ShouldBe(originalBalance + amount); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1337, 1338)] | ||
[InlineData(1111.111, 9999.999)] | ||
[InlineData(1, 1.000000001)] | ||
[InlineData(0, 1)] | ||
public void WithdrawMoreThanBalance(decimal initial, decimal amount) | ||
{ | ||
var account = new Account(); | ||
account.Credit(initial); | ||
var transactioner = new Transactioner(); | ||
decimal originalBalance = account.Balance; | ||
|
||
Should.Throw<InvalidTransactionException>(() => transactioner.Withdraw(account, amount)); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using ALM.Web.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace ALM.Web.Controllers | ||
{ | ||
public class TransactionController : Controller | ||
{ | ||
private readonly Transactioner _transactioner; | ||
private readonly BankRepository _repository; | ||
|
||
public TransactionController(Transactioner transactioner, BankRepository repository) | ||
{ | ||
_transactioner = transactioner; | ||
_repository = repository; | ||
} | ||
|
||
[HttpGet] | ||
public IActionResult Index() | ||
{ | ||
return RedirectToAction("Transact"); | ||
} | ||
|
||
[HttpGet] | ||
public IActionResult Transact() | ||
{ | ||
var model = new TransactionViewModel(); | ||
return View(model); | ||
} | ||
|
||
[HttpPost] | ||
public IActionResult Transact(TransactionViewModel model, TransactionType type) | ||
{ | ||
if (!ModelState.IsValid) | ||
{ | ||
return View(model); | ||
} | ||
try | ||
{ | ||
var account = _repository.GetAccount(model.AccountId); | ||
model.Account = account; | ||
switch (type) | ||
{ | ||
case TransactionType.Deposit: | ||
_transactioner.Deposit(account, model.Amount); | ||
break; | ||
case TransactionType.Withdrawal: | ||
_transactioner.Withdraw(account, model.Amount); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
catch (AccountNotFoundException ex) | ||
{ | ||
ModelState.AddModelError("account", ex.Message); | ||
return View(model); | ||
} | ||
catch (InvalidTransactionException ex) | ||
{ | ||
ModelState.AddModelError("balance", ex.Message); | ||
return View(model); | ||
} | ||
|
||
return View(model); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace ALM.Web | ||
{ | ||
public class AccountNotFoundException : Exception | ||
{ | ||
public AccountNotFoundException(int accountId) : base($"Account #{accountId} not found") | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace ALM.Web | ||
{ | ||
public class InvalidTransactionException:Exception | ||
{ | ||
public InvalidTransactionException(string message) : base(message) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace ALM.Web.Models | ||
{ | ||
public class TransactionViewModel | ||
{ | ||
[Required] | ||
[Display(Name = "Account ID")] | ||
public int AccountId { get; set; } = 0; | ||
|
||
[Required] | ||
[Display(Name = "Amount")] | ||
[DataType(DataType.Currency)] | ||
public decimal Amount { get; set; } = 0; | ||
|
||
public Account Account { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace ALM.Web | ||
{ | ||
public enum TransactionType | ||
{ | ||
Deposit, | ||
Withdrawal | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using ALM.Web.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace ALM.Web | ||
{ | ||
public class Transactioner | ||
{ | ||
private readonly BankRepository _repository; | ||
|
||
public Transactioner(BankRepository repository) | ||
{ | ||
_repository = repository; | ||
} | ||
|
||
public Transactioner() | ||
{ | ||
|
||
} | ||
|
||
public decimal Deposit(Account account, decimal amount) | ||
{ | ||
account.Credit(amount); | ||
return account.Balance; | ||
} | ||
|
||
public decimal Deposit(int accountId, decimal amount) | ||
{ | ||
var account = _repository.GetAccount(accountId); | ||
if (account == null) | ||
{ | ||
throw new AccountNotFoundException(accountId); | ||
} | ||
return Deposit(account, amount); | ||
} | ||
|
||
|
||
public decimal Withdraw(Account account, decimal amount) | ||
{ | ||
if (account.Balance < amount) | ||
{ | ||
throw new InvalidTransactionException($"Cant Withdraw {amount} from account #{account.AccountId}. Balance is only {account.Balance}."); | ||
} | ||
account.Debit(amount); | ||
return account.Balance; | ||
} | ||
|
||
public decimal Withdraw(int accountId, decimal amount) | ||
{ | ||
var account = _repository.GetAccount(accountId); | ||
if (account == null) | ||
{ | ||
throw new AccountNotFoundException(accountId); | ||
} | ||
return Withdraw(account, amount); | ||
} | ||
|
||
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.