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 #2 from CMS18/feature/deposit-withdraw
Feature/deposit withdraw implemented with tests
- Loading branch information
Showing
10 changed files
with
228 additions
and
5 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 |
---|---|---|
@@ -1,14 +1,34 @@ | ||
using AlmLabb.Business; | ||
using AlmLabb.Models.ViewModels; | ||
using System; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace AlmLabb.Tests | ||
{ | ||
public class UnitTest1 | ||
{ | ||
[Fact] | ||
public void Test1() | ||
[Theory] | ||
[InlineData(-100, "Deposit")] | ||
[InlineData(-100, "Withdraw")] | ||
[InlineData(0, "Withdraw")] | ||
public async Task TransactionHandlerTests(decimal amount, string type) | ||
{ | ||
var Repo = new MockDb(); | ||
var _handler = new TransactionHandler(Repo); | ||
|
||
var transaction = new TransactionViewModel(); | ||
transaction.AccountID = 1; | ||
transaction.Amount = amount; | ||
transaction.TransactionType = type; | ||
|
||
var result = _handler.Handle(transaction); | ||
|
||
var expected = 100; | ||
|
||
Assert.False(result.IsSuccessful); | ||
Assert.Equal(expected, Repo.Accounts[0].Balance); | ||
} | ||
|
||
} | ||
} |
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,74 @@ | ||
using AlmLabb.Business.Interfaces; | ||
using AlmLabb.Models.ViewModels; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace AlmLabb.Business | ||
{ | ||
public class TransactionHandler : ITransactionHandler | ||
{ | ||
private IMockDb _context; | ||
public TransactionHandler(IMockDb context) | ||
{ | ||
_context = context; | ||
} | ||
public TransactionResult Handle(TransactionViewModel transaction) | ||
{ | ||
if (transaction.Amount <= 0) | ||
{ | ||
return new TransactionResult(false, "Amount must be positive."); | ||
} | ||
if (transaction.TransactionType == "Deposit") | ||
{ | ||
var result = this.Deposit(transaction); | ||
return result; | ||
} | ||
else if (transaction.TransactionType == "Withdraw") | ||
{ | ||
var result = this.Withdraw(transaction); | ||
return result; | ||
} | ||
return new TransactionResult(false, "Something went wrong :("); | ||
} | ||
|
||
private TransactionResult Deposit(TransactionViewModel model) | ||
{ | ||
foreach (var item in _context.Accounts) | ||
{ | ||
if (item.AccountID == model.AccountID) | ||
{ | ||
item.Balance += model.Amount; | ||
return new TransactionResult(true, model.Amount + " was deposited into Account " + item.AccountID); | ||
} | ||
} | ||
|
||
return new TransactionResult(false,"Accountnumber is not valid."); | ||
} | ||
|
||
private TransactionResult Withdraw(TransactionViewModel model) | ||
{ | ||
foreach (var item in _context.Accounts) | ||
{ | ||
if (item.AccountID == model.AccountID) | ||
{ | ||
if (item.Balance < model.Amount) | ||
{ | ||
return new TransactionResult(false, "Balance of account " + item.AccountID + " is to low."); | ||
} | ||
item.Balance -= model.Amount; | ||
|
||
return new TransactionResult(true, model.Amount + " was withdrawed from Account " + item.AccountID); | ||
} | ||
} | ||
return new TransactionResult(false, "Something went wrong :("); | ||
} | ||
} | ||
|
||
public interface ITransactionHandler | ||
{ | ||
TransactionResult Handle(TransactionViewModel transaction); | ||
|
||
} | ||
} |
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,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using AlmLabb.Business; | ||
using AlmLabb.Business.Interfaces; | ||
using AlmLabb.Models.ViewModels; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace AlmLabb.Controllers | ||
{ | ||
public class TransactionController : Controller | ||
{ | ||
private ITransactionHandler _handler; | ||
public TransactionController(ITransactionHandler handler) | ||
{ | ||
_handler = handler; | ||
} | ||
public IActionResult Index() | ||
{ | ||
return View(new TransactionViewModel()); | ||
} | ||
|
||
[HttpPost] | ||
[ValidateAntiForgeryToken] | ||
public IActionResult CreateTransaction(TransactionViewModel transaction) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
transaction.Result = _handler.Handle(transaction); | ||
} | ||
return View("Index", transaction); | ||
} | ||
} | ||
} |
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,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace AlmLabb.Models.ViewModels | ||
{ | ||
public class TransactionViewModel | ||
{ | ||
[Required] | ||
public string TransactionType { get; set; } | ||
[Required] | ||
public int AccountID { get; set; } | ||
[Range(0.001, (double)decimal.MaxValue)] | ||
public decimal Amount { get; set; } | ||
|
||
public TransactionResult Result { get; set; } | ||
|
||
} | ||
|
||
public class TransactionResult | ||
{ | ||
public bool IsSuccessful { get; set; } | ||
public string Message { get; set; } | ||
|
||
public TransactionResult() | ||
{ | ||
|
||
} | ||
public TransactionResult(bool success, string message) | ||
{ | ||
IsSuccessful = success; | ||
Message = 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
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,51 @@ | ||
@model AlmLabb.Models.ViewModels.TransactionViewModel | ||
|
||
@{ | ||
ViewData["Title"] = "Index"; | ||
} | ||
|
||
<h1>Transactions</h1> | ||
<hr /> | ||
@if (Model.Result != null) | ||
{ | ||
if (Model.Result.IsSuccessful) | ||
{ | ||
<div class="alert alert-success" role="alert"> | ||
<p>@Model.Result.Message</p> | ||
</div> | ||
} | ||
else if (!Model.Result.IsSuccessful) | ||
{ | ||
<div class="alert alert-danger" role="alert"> | ||
<p>@Model.Result.Message</p> | ||
</div> | ||
} | ||
} | ||
<div class="row"> | ||
<div class="col-md-4"> | ||
<form asp-action="CreateTransaction"> | ||
<div asp-validation-summary="ModelOnly" class="text-danger"></div> | ||
<div class="form-group"> | ||
<select asp-for="TransactionType" class="form-control"> | ||
<option name="Deposit" value="Deposit">Deposit</option> | ||
<option name="Whitdraw" value="Withdraw">Withdraw</option> | ||
</select> | ||
</div> | ||
<div class="form-group"> | ||
<label asp-for="AccountID" class="control-label"></label> | ||
<input asp-for="AccountID" class="form-control" /> | ||
<span asp-validation-for="AccountID" class="text-danger"></span> | ||
</div> | ||
<div class="form-group"> | ||
<label asp-for="Amount" class="control-label"></label> | ||
<input asp-for="Amount" class="form-control" /> | ||
<span asp-validation-for="Amount" class="text-danger"></span> | ||
</div> | ||
<div class="form-group"> | ||
<input type="submit" value="Submit" class="btn btn-primary" /> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
|
||
|
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 @@ | ||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers |