Automated teller machine emulation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using SergeyDrozdovATM.BLL;
using SergeyDrozdovATM.Web.Models;
namespace SergeyDrozdovATM.Web.Controllers
{
[Authentication]
public class ATMController : Controller
{
private readonly IATMService _atmService;
private const string OperationSuccessMessage = "Operation completed.";
public ATMController()
{
_atmService = new ATMService();
}
public ActionResult Index()
{
return RedirectToAction("Index", "Home");
}
public ActionResult CreateAccount()
{
var model = new CreateAccountViewModel();
var currencies = new List<Currency>();
var accounts = _atmService.GetAccountsList(AuthenticationHelper.GetLoggedUser().Id);
foreach (var currency in _atmService.GetCurrenciesList())
{
if (accounts.All(x => x.Currency.Id != currency.Id))
currencies.Add(currency);
}
if (currencies.Count == 0)
{
model.NoAvailableCurrencies = true;
}
else
{
model.CurrenciesList = new SelectList(currencies, "Id", "Name");
}
return View(model);
}
[HttpPost]
public ActionResult CreateAccount(CreateAccountViewModel model)
{
if (ModelState.IsValid)
{
try
{
_atmService.CreateAccount(AuthenticationHelper.GetLoggedUser().Id, model.CurrencyId);
TempData["Message"] = OperationSuccessMessage;
return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
TempData["MessageType"] = "error";
TempData["Message"] = ex.Message;
return RedirectToAction("Index", "Home");
}
}
return View();
}
public ActionResult Balance()
{
var model = new BalanceViewModel();
var userAccounts = _atmService.GetAccountsList(AuthenticationHelper.GetLoggedUser().Id).ToList();
if (userAccounts.Count == 0)
{
model.NoAccounts = true;
}
else
{
model.AccountsList = userAccounts;
}
return View(model);
}
public ActionResult Deposit()
{
var model = new CashTransferViewModel();
var userAccounts = _atmService.GetAccountsList(AuthenticationHelper.GetLoggedUser().Id).ToList();
if (userAccounts.Count == 0)
{
model.NoAccounts = true;
}
else
{
var accounts = new List<Account>();
foreach (var accountInfo in userAccounts)
{
accounts.Add(accountInfo);
}
model.AccountsList = new SelectList(accounts, "Id", "Currency.Name");
}
return View(model);
}
[HttpPost]
public ActionResult Deposit(CashTransferViewModel model)
{
if (ModelState.IsValid)
{
try
{
var accountInfo = _atmService.GetAccountById(model.AccountId);
_atmService.Deposit(accountInfo, model.CashAmount);
}
catch (Exception ex)
{
TempData["MessageType"] = "error";
TempData["Message"] = ex.Message;
return RedirectToAction("Index", "Home");
}
TempData["Message"] = OperationSuccessMessage;
return RedirectToAction("Index", "Home");
}
return View();
}
public ActionResult Withdraw()
{
var model = new CashTransferViewModel();
var userAccounts = _atmService.GetAccountsList(AuthenticationHelper.GetLoggedUser().Id).ToList();
if (userAccounts.Count == 0)
{
model.NoAccounts = true;
}
else
{
var accounts = new List<Account>();
foreach (var accountInfo in userAccounts)
{
accounts.Add(accountInfo);
}
model.AccountsList = new SelectList(accounts, "Id", "Currency.Name");
}
return View(model);
}
[HttpPost]
public ActionResult Withdraw(CashTransferViewModel model)
{
if (ModelState.IsValid)
{
try
{
var accountInfo = _atmService.GetAccountById(model.AccountId);
_atmService.Withdraw(accountInfo, model.CashAmount);
}
catch (Exception ex)
{
TempData["MessageType"] = "error";
TempData["Message"] = ex.Message;
return RedirectToAction("Index", "Home");
}
TempData["Message"] = OperationSuccessMessage;
return RedirectToAction("Index", "Home");
}
return View();
}
public ActionResult ConvertCurrency()
{
var model = new ConvertCurrencyViewModel();
var userAccounts = _atmService.GetAccountsList(AuthenticationHelper.GetLoggedUser().Id).ToList();
if (userAccounts.Count <= 1)
{
model.NotEnoughAccounts = true;
}
else
{
var accounts = new List<Account>();
foreach (var accountInfo in userAccounts)
{
accounts.Add(accountInfo);
}
model.SourceAccountsList = new SelectList(accounts, "Id", "Currency.Name");
model.DestinationAccountsList = new SelectList(accounts, "Id", "Currency.Name");
}
return View(model);
}
[HttpPost]
public ActionResult ConvertCurrency(ConvertCurrencyViewModel model)
{
if (ModelState.IsValid)
{
try
{
var sourceAccount = _atmService.GetAccountById(model.SourceAccountId);
var destinationAccount = _atmService.GetAccountById(model.DestinationAccountId);
if (_atmService.ValidateAccountBalance(sourceAccount, model.CashAmount))
{
_atmService.ConvertCurrency(sourceAccount, destinationAccount, model.CashAmount);
TempData["Message"] = OperationSuccessMessage;
return RedirectToAction("Index", "Home");
}
}
catch (Exception ex)
{
TempData["MessageType"] = "error";
TempData["Message"] = ex.Message;
return RedirectToAction("Index", "Home");
}
}
TempData["Message"] = OperationSuccessMessage;
return RedirectToAction("Index", "Home");
}
}
}