public ActionResult Register(RegisterViewModel viewModel) { using (CryptoWalletDBContext ctx = new CryptoWalletDBContext()) { CryptoWalletDB.Domain.User user = new CryptoWalletDB.Domain.User(); if( ctx.Users.AsNoTracking().FirstOrDefault(u => u.Email == viewModel.Email) ==null) { ModelState.AddModelError("", "The email already exist!"); if (ctx.Users.AsNoTracking().FirstOrDefault(u => u.Email == viewModel.Name) == null) { ModelState.AddModelError("", "The name already exist!"); } } return View(viewModel); } user.Email = viewModel.Email; user.Name = viewModel.Name; user.Password = viewModel.Password; ctx.Users.Add(user); ctx.SaveChanges(); }
public ActionResult Deposit(DepositViewModel viewModel) { if (ModelState.IsValid) { using (CryptoWalletDBContext ctx = new CryptoWalletDBContext()) { User currentUser = ctx.Users.AsNoTracking().FirstOrDefault(u => u.Email == User.Identity.Name); UserBankAccount eurAccount = ctx.UsersBankAccounts.FirstOrDefault(u => u.Currency == "EUR" && u.UserId == currentUser.UserId); if (eurAccount == null) { eurAccount = new UserBankAccount { Currency = "EUR", UserId = currentUser.UserId, Amount = 0 }; ctx.UsersBankAccounts.Add(eurAccount); } eurAccount.Amount += viewModel.Amount; ctx.SaveChanges(); UserTransaction transaction = new UserTransaction { Amount = viewModel.Amount, FromAccountId = eurAccount.AccountId, ToAccountId = eurAccount.AccountId, TransactionDate = DateTime.Now }; ctx.UsersTransactions.Add(transaction); ctx.SaveChanges(); } return(RedirectToAction("Index")); } return(View(viewModel)); }
public ActionResult Convert(ConvertViewModel viewModel) { if (ModelState.IsValid) { using (CryptoWalletDBContext ctx = new CryptoWalletDBContext()) { User CurrentUser = ctx.Users.AsNoTracking().FirstOrDefault(u => u.Email == User.Identity.Name); UserBankAccount toAccount = ctx.UsersBankAccounts.FirstOrDefault(u => u.Currency == viewModel.IntoCurrency && u.UserId == CurrentUser.UserId); UserBankAccount fromAccount = ctx.UsersBankAccounts.FirstOrDefault(u => u.Currency == viewModel.Currency && u.UserId == CurrentUser.UserId); if (toAccount.Currency == fromAccount.Currency) { ModelState.AddModelError("", "Choose other currency!"); SetupConvertViewModel(viewModel); return(View(viewModel)); } if (fromAccount.Amount < viewModel.Amount) { ModelState.AddModelError("", "Insufficient funds!"); SetupConvertViewModel(viewModel); return(View(viewModel)); } ExchangeService cryptoExchange = new ExchangeService(); List <CurrencyRate> currencyRates = cryptoExchange.GetConversionRate(findCurrency(viewModel.Currency), new Currency[] { Currency.USD, Currency.GBP, Currency.BTC, Currency.XRP, Currency.EUR }); decimal rate = currencyRates.FirstOrDefault(u => u.Currency.ToString() == viewModel.IntoCurrency).Rate; fromAccount.Amount -= viewModel.Amount; toAccount.Amount += (viewModel.Amount * rate); UserTransaction transaction = new UserTransaction { Amount = viewModel.Amount, ToAccountId = toAccount.AccountId, FromAccountId = fromAccount.AccountId, TransactionDate = DateTime.Now, TransactionRate = (decimal)rate, ToAccount = toAccount, FromAccount = fromAccount }; ctx.UsersTransactions.Add(transaction); ctx.SaveChanges(); return(RedirectToAction("Index")); } } SetupConvertViewModel(viewModel); return(View(viewModel)); }
public ActionResult Send(SendViewModel viewModel) { if (ModelState.IsValid) { using (CryptoWalletDBContext ctx = new CryptoWalletDBContext()) { User fromUser = ctx.Users.AsNoTracking().FirstOrDefault(u => u.Email == User.Identity.Name); User toUser = ctx.Users.AsNoTracking().FirstOrDefault(u => u.Email == viewModel.ToUserEmail); if (toUser != null) { if (fromUser.UserId == toUser.UserId) { ModelState.AddModelError("", "You can not send yourself!"); SetupSendViewModel(viewModel); return(View(viewModel)); } UserBankAccount toAccount = ctx.UsersBankAccounts.FirstOrDefault(u => u.Currency == viewModel.Currency && u.UserId == toUser.UserId); UserBankAccount fromAccount = ctx.UsersBankAccounts.FirstOrDefault(u => u.Currency == viewModel.Currency && u.UserId == fromUser.UserId); if (toAccount == null) { ModelState.AddModelError("", "The receiver does not have an account!"); SetupSendViewModel(viewModel); return(View(viewModel)); } if (fromAccount.Amount > viewModel.Amount) { fromAccount.Amount -= viewModel.Amount; toAccount.Amount += viewModel.Amount; UserTransaction transaction = new UserTransaction { Amount = viewModel.Amount, ToAccountId = toAccount.AccountId, FromAccountId = fromAccount.AccountId, TransactionDate = DateTime.Now, TransactionRate = 1, ToAccount = toAccount, FromAccount = fromAccount }; ctx.UsersTransactions.Add(transaction); ctx.SaveChanges(); return(RedirectToAction("Index")); } else { ModelState.AddModelError("", "Insufficient funds!"); SetupSendViewModel(viewModel); return(View(viewModel)); } } else { ModelState.AddModelError("", "Invalid friend's email!"); SetupSendViewModel(viewModel); return(View(viewModel)); } } } SetupSendViewModel(viewModel); return(View(viewModel)); }