public Result CreateNewCustomer(Customer newCustomer, IRepositoriesHandler repositoriesHandler, IPasswordService passwordService, IResourcesProvider resourcesProvider) { var result = new Result(); var isLoginExist = repositoriesHandler.СustomerRepository.GetCollection().Any(x => x.Login == newCustomer.Login); if (isLoginExist) { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("LogExist"); return(result); } if (!IsCutomerDataValid(newCustomer.Login, newCustomer.Password)) { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("NoLogPass"); return(result); } newCustomer.Password = passwordService.HashPassword(newCustomer.Password); newCustomer.DateOfRegistration = DateTime.Now; repositoriesHandler.СustomerRepository.Insert(newCustomer); result.Success = true; result.SuccessMessage = resourcesProvider.GetGeneralResource("CusCreat"); return(result); }
public Result CreateNewCreditCard(CreditCard newCreditCard, IRepositoriesHandler repositoriesHandler, IPasswordService passwordService, IResourcesProvider resourcesProvider) { var result = new Result(); var isCardNumExist = repositoriesHandler.СreditCardRepository.GetCollection().Any(x => x.CardNumber == newCreditCard.CardNumber); if (isCardNumExist) { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("CCNumExist"); return(result); } if (!IsCreditCardDataValid(newCreditCard.CardNumber, newCreditCard.PIN)) { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("NoNumPin"); return(result); } newCreditCard.PIN = passwordService.HashPassword(newCreditCard.PIN); newCreditCard.DateOfCreation = DateTime.Now; repositoriesHandler.СreditCardRepository.Insert(newCreditCard); result.Success = true; result.SuccessMessage = resourcesProvider.GetGeneralResource("CCCreated"); return(result); }
public async Task <Result> PerformTransactionAsync(Domain.Models.Transaction newTransaction, IRepositoriesHandler repositoriesHandler, IResourcesProvider resourcesProvider) { var result = new Result(); result.Success = newTransaction.Amount > 0; if (!result.Success) { result.Error = resourcesProvider.GetGeneralResource("IncAmount"); return(result); } var fromCreditCard = await repositoriesHandler.СreditCardRepository.GetByIdAsync(newTransaction.FromCreditCardID); result.Success = fromCreditCard.Amount > newTransaction.Amount; if (!result.Success) { result.Error = resourcesProvider.GetGeneralResource("TranOutOfFunds"); return(result); } var toCreditCard = await repositoriesHandler.СreditCardRepository.GetByIdAsync(newTransaction.ToCreditCardID); if (fromCreditCard.CreditCardID == toCreditCard.CreditCardID) { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("SelectDifCards"); return(result); } try { using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { fromCreditCard.Amount -= newTransaction.Amount; await repositoriesHandler.СreditCardRepository.UpdateAsync(fromCreditCard); toCreditCard.Amount += newTransaction.Amount; await repositoriesHandler.СreditCardRepository.UpdateAsync(toCreditCard); newTransaction.Date = DateTime.Now; await repositoriesHandler.TransactionRepository.InsertAsync(newTransaction); scope.Complete(); } result.Success = true; result.SuccessMessage = resourcesProvider.GetGeneralResource("TranOk"); return(result); } catch (Exception ex) { result.Success = false; result.Error = $"{resourcesProvider.GetGeneralResource("TranFail")} {ex.Message}."; return(result); } }
public async Task <Result> GenerateCardRandomTransactionsAsync(int toCardID, IRepositoriesHandler repositoriesHandler, IResourcesProvider resourcesProvider) { var result = new Result(); if (toCardID == 0) { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("CCIdNull"); return(result); } var cards = await repositoriesHandler.СreditCardRepository.GetCollectionForRandomTranAsync(toCardID); if (cards.Count == 0) { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("NotEnCards"); return(result); } int counter = 0; var transactionResult = new Result(); foreach (var card in cards) { Random rnd = new Random(); var transaction = new Domain.Models.Transaction(); transaction.Amount = rnd.Next(1, (int)card.Amount); transaction.Comment = resourcesProvider.GetGeneralResource("RndTran"); transaction.Date = DateTime.Now; transaction.FromCreditCardID = card.CreditCardID; transaction.ToCreditCardID = toCardID; transactionResult = await this.PerformTransactionAsync(transaction, repositoriesHandler, resourcesProvider); if (transactionResult.Success) { counter++; } } if (counter > 0) { result.Success = true; result.SuccessMessage = resourcesProvider.GetGeneralResource("TransGenDone"); return(result); } else { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("TranAllCCOutOfFunds"); return(result); } }
public Result UpdateCustomer(EditedCustomer editedCustomer, IRepositoriesHandler repositoriesHandler, IPasswordService passwordService, IResourcesProvider resourcesProvider) { var result = new Result(); var isLoginExist = repositoriesHandler.СustomerRepository.GetCollection().Any(x => x.Login == editedCustomer.Login && x.CustomerID != editedCustomer.CustomerID); if (isLoginExist) { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("LogExist"); return(result); } if (editedCustomer.CustomerID == 0) { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("CusIdNull"); return(result); } if (!IsCutomerDataValid(editedCustomer.Login, editedCustomer.CurrentPassword)) { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("NoLogPass"); return(result); } var storedCustomer = repositoriesHandler.СustomerRepository.GetById(editedCustomer.CustomerID); if (passwordService.ComparePasswords(editedCustomer.CurrentPassword, storedCustomer.Password)) { storedCustomer.CustomerID = editedCustomer.CustomerID; storedCustomer.FirstName = editedCustomer.FirstName; storedCustomer.LastName = editedCustomer.LastName; storedCustomer.Login = editedCustomer.Login; storedCustomer.Password = string.IsNullOrEmpty(editedCustomer.NewPassword) ? storedCustomer.Password : passwordService.HashPassword(editedCustomer.NewPassword); repositoriesHandler.СustomerRepository.Update(storedCustomer); result.Success = true; result.SuccessMessage = resourcesProvider.GetGeneralResource("CusUpdt"); return(result); } else { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("PassDontMatch"); return(result); } }
public Result UpdateCreditCard(EditedCreditCard editedCreditCard, IRepositoriesHandler repositoriesHandler, IPasswordService passwordService, IResourcesProvider resourcesProvider) { var result = new Result(); var isCardNumExist = repositoriesHandler.СreditCardRepository.GetCollection().Any(x => x.CardNumber == editedCreditCard.CardNumber && x.CreditCardID != editedCreditCard.CreditCardID); if (isCardNumExist) { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("CCNumExist"); return(result); } if (!IsCreditCardDataValid(editedCreditCard.CardNumber, editedCreditCard.CurrentPIN)) { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("NoNumPin"); return(result); } var storedCreditCard = repositoriesHandler.СreditCardRepository.GetById(editedCreditCard.CreditCardID); if (passwordService.ComparePasswords(editedCreditCard.CurrentPIN, storedCreditCard.PIN)) { storedCreditCard.CardNumber = editedCreditCard.CardNumber; storedCreditCard.Amount = editedCreditCard.Amount; storedCreditCard.PIN = string.IsNullOrEmpty(editedCreditCard.NewPIN) ? storedCreditCard.PIN : passwordService.HashPassword(editedCreditCard.NewPIN); repositoriesHandler.СreditCardRepository.Update(storedCreditCard); result.Success = true; result.SuccessMessage = resourcesProvider.GetGeneralResource("CCUpdt"); return(result); } else { result.Success = false; result.Error = resourcesProvider.GetGeneralResource("PinsDontMatch"); return(result); } }