Skip to content

Commit

Permalink
Merge pull request #2 from CMS18/feature/deposit-withdraw
Browse files Browse the repository at this point in the history
Feature/deposit withdraw implemented with tests
  • Loading branch information
RickardUttermalm committed Nov 13, 2019
2 parents 463385e + b8212a9 commit e880f48
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 5 deletions.
4 changes: 4 additions & 0 deletions AlmLabb.Tests/AlmLabb.Tests.csproj
Expand Up @@ -13,4 +13,8 @@
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AlmLabb\AlmLabb.csproj" />
</ItemGroup>

</Project>
24 changes: 22 additions & 2 deletions AlmLabb.Tests/UnitTest1.cs
@@ -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);
}

}
}
74 changes: 74 additions & 0 deletions AlmLabb/Business/TransactionHandler.cs
@@ -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);

}
}
2 changes: 1 addition & 1 deletion AlmLabb/Controllers/HomeController.cs
Expand Up @@ -11,7 +11,7 @@ namespace AlmLabb.Controllers
{
public class HomeController : Controller
{
private IMockDb _context;
private readonly IMockDb _context;
public HomeController(IMockDb context)
{
_context = context;
Expand Down
35 changes: 35 additions & 0 deletions AlmLabb/Controllers/TransactionController.cs
@@ -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);
}
}
}
37 changes: 37 additions & 0 deletions AlmLabb/Models/ViewModels/TransactionViewModel.cs
@@ -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;
}
}
}
1 change: 1 addition & 0 deletions AlmLabb/Startup.cs
Expand Up @@ -21,6 +21,7 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddSingleton(typeof(IMockDb), typeof(MockDb));
services.AddScoped(typeof(ITransactionHandler), typeof(TransactionHandler));
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
4 changes: 2 additions & 2 deletions AlmLabb/Views/Shared/Layouts/_Root.cshtml
Expand Up @@ -22,10 +22,10 @@
<!-- Links -->
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#"><h2>Rickards Bank</h2></a>
<a class="nav-link" asp-controller="Home" asp-action="Index"><h2>Rickards Bank</h2></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 2</a>
<a class="nav-link" asp-controller="Transaction" asp-action="Index">Transactions</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 3</a>
Expand Down
51 changes: 51 additions & 0 deletions AlmLabb/Views/Transaction/Index.cshtml
@@ -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>


1 change: 1 addition & 0 deletions AlmLabb/Views/_ViewImports.cshtml
@@ -0,0 +1 @@
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

0 comments on commit e880f48

Please sign in to comment.