public async Task <ActionResult> Edit(IFormCollection collection) { int CustomerId = Convert.ToInt32(collection["CustomerId"]); ///Checks for Model State if (!ModelState.IsValid) { RedirectToAction("Index", new { customerId = CustomerId }); } //Validates Model Data, can be achieved through IValidator DataAnnotations or Tag Helpers //Validation Messages can be added to assist the user ValueObjectsHelper valueObjectsHelper = new ValueObjectsHelper(); if (valueObjectsHelper.TryGetCurrencySymbol(collection["Currency"], out string symbol) || valueObjectsHelper.TryGetValidDecimal(collection["Amount"])) { /// Updates Transaction Entity on Db. Can be decoupled and injected to Service var updatedTransaction = await _transactionRepository.GetByIdCompositeAsync(Convert.ToInt32(collection["TransactionId"]), collection["accountName"]); updatedTransaction.Description = collection["Description"].ToString(); updatedTransaction.Amount = Decimal.Parse(collection["Amount"], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture); updatedTransaction.CurrencyCode = collection["Currency"]; await _asyncTransactionRepository.UpdateAsync(updatedTransaction); } var transactionViewModel = _dataManagerViewService.GetTransactionViewModel(Convert.ToInt32(collection["TransactionId"])); return(View("Edit", transactionViewModel)); }
/// <summary> ///Retrieves the URL of the submitted temp File and creates IDisposable ExcelPackage in Memory. ///Loops Excel Row and Validates row data. ///Validated row data through the /// /// </summary> /// <param name="urlPath"></param> /// <param name="clientId"></param> /// <returns></returns> public async Task <IEnumerable <FileUploadSummary> > ValidateAndSaveTransactions(string urlPath, int clientId) { FileInfo file = new FileInfo(urlPath); ValidateCurrencyCodes _validateCurrencyCodes; ValidateTransactionAccount _validateTransactionAccount; ValueObjectsHelper valueObjectsHelper = new ValueObjectsHelper(); List <FileUploadSummary> fileUploadSummaries = new List <FileUploadSummary>(); var accounts = await _asyncAccountsRepository.GetAccountsByCustomer(clientId); try { List <Transaction> transactionsList = new List <Transaction>(); //Creates IDisposable Excel Package using (ExcelPackage package = new ExcelPackage(file)) { FileUploadSummary fileUploadSummary = new FileUploadSummary(); StringBuilder sb = new StringBuilder(); ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; int rowCount = worksheet.Dimension.Rows; int ColCount = worksheet.Dimension.Columns; //Calls Header Validator Method For Exchel Header Validation if (!AreExcelColumnsHeadersValid(worksheet.Cells[1, 1].Value ?? "", worksheet.Cells[1, 2].Value ?? "", worksheet.Cells[1, 3].Value.ToString() ?? "", worksheet.Cells[1, 4].Value.ToString() ?? "")) { fileUploadSummaries.Add(GetExceptionSummaryMessage("1", "NO VALID HEADERS")); return(fileUploadSummaries); } for (int row = 2; row <= rowCount; row++) { //Validates if Rows are Not Null or Contain Empty Values Transaction transactions = new Transaction(); if (worksheet.Cells[row, 1].Value == null || String.IsNullOrEmpty(worksheet.Cells[row, 1].Value.ToString())) { fileUploadSummaries.Add(GetExceptionSummaryMessage(row.ToString(), "No Value for Account Number")); continue; } if ((worksheet.Cells[row, 2].Value == null || String.IsNullOrEmpty(worksheet.Cells[row, 2].Value.ToString()))) { fileUploadSummaries.Add(GetExceptionSummaryMessage(row.ToString(), "No Value for Transaction Description")); continue; } if (worksheet.Cells[row, 3].Value == null || String.IsNullOrEmpty(worksheet.Cells[row, 3].Value.ToString())) { fileUploadSummaries.Add(GetExceptionSummaryMessage(row.ToString(), "No Value on Currency Code Row")); continue; } if (worksheet.Cells[row, 4].Value != null && !String.IsNullOrEmpty(worksheet.Cells[row, 4].Value.ToString())) { ///Validates If Row Decimal Value is Valid Decimal if (!valueObjectsHelper.TryGetValidDecimal(worksheet.Cells[row, 4].Value.ToString())) { fileUploadSummaries.Add(GetExceptionSummaryMessage(row.ToString(), "Not a Valid Decimal Amount for Transactions")); continue; } } else { fileUploadSummaries.Add(GetExceptionSummaryMessage(row.ToString(), "No Value for Transaction Amount")); continue; } transactions.AccountId = worksheet.Cells[row, 1].Value.ToString(); transactions.Description = worksheet.Cells[row, 2].Value.ToString(); transactions.CurrencyCode = worksheet.Cells[row, 3].Value.ToString(); transactions.Amount = Convert.ToDecimal(worksheet.Cells[row, 4].Value.ToString()); //Currency and Account Validation _validateCurrencyCodes = new ValidateCurrencyCodes(transactions); _validateTransactionAccount = new ValidateTransactionAccount(transactions, accounts); //validate Currency Through Currency Validator Class if (!_validateCurrencyCodes.IsValid) { fileUploadSummary.RowNumber = row.ToString(); fileUploadSummary.Message = _validateCurrencyCodes.Message; sb.Append("Line: " + row.ToString() + _validateCurrencyCodes.Message); fileUploadSummaries.Add(GetExceptionSummaryMessage(row.ToString(), _validateCurrencyCodes.Message)); continue; } //Validate Transaction Account through Account Validator Class if (!_validateTransactionAccount.IsValid) { fileUploadSummaries.Add(GetExceptionSummaryMessage(row.ToString(), _validateTransactionAccount.Message)); continue; } transactionsList.Add(transactions); } } //Calls EFCore.Bulks Extensions handling bulk Updates. _asyncTransactionRepository.UpdateBulkAsync(transactionsList); //deletes file file.Delete(); } catch (Exception ex) { } return(fileUploadSummaries); }