public async Task <IActionResult> AddEditTransaction(int?id) { TransactionForCreateUpdateViewModel transactionForCreateUpdateViewModel = new TransactionForCreateUpdateViewModel(); if (id.HasValue) { var transactionFromRepo = await _uow.CustomerTransactions.GetByIdAsync(id.Value); if (transactionFromRepo == null) { return(NotFound()); } transactionForCreateUpdateViewModel = _mapper.Map <TransactionForCreateUpdateViewModel>(transactionFromRepo); if (transactionFromRepo.SoldAmount > 0) { transactionForCreateUpdateViewModel.Amount = transactionFromRepo.SoldAmount; transactionForCreateUpdateViewModel.TransactionType = 1; } else if (transactionFromRepo.ReceivedAmount > 0) { transactionForCreateUpdateViewModel.Amount = transactionFromRepo.ReceivedAmount; transactionForCreateUpdateViewModel.TransactionType = 2; } transactionForCreateUpdateViewModel.CustomerSelectList = new SelectList(await _uow.Customers.GetAllCustomerFullNameAsync(), "CustomerId", "FullName", transactionForCreateUpdateViewModel.CustomerId); } else { transactionForCreateUpdateViewModel.CustomerSelectList = new SelectList(await _uow.Customers.GetAllCustomerFullNameAsync(), "CustomerId", "FullName"); } return(View(transactionForCreateUpdateViewModel)); }
public async Task <IActionResult> AddEditTransaction(int?id, TransactionForCreateUpdateViewModel transactionForCreateUpdateViewModel) { string message = string.Empty; try { if (ModelState.IsValid) { bool isNew = !id.HasValue; if (isNew) { var transactionToCreate = _mapper.Map <CustomerTransaction>(transactionForCreateUpdateViewModel); await _uow.CustomerTransactions.AddAsync(transactionToCreate); await _uow.SaveAsync(); TempData["Message"] = "Saved Successfully"; TempData["Status"] = "success"; } else { var transactionToUpdate = _mapper.Map <CustomerTransaction>(transactionForCreateUpdateViewModel); if (transactionForCreateUpdateViewModel.TransactionType == 1) { transactionToUpdate.SoldAmount = transactionForCreateUpdateViewModel.Amount; } else if (transactionForCreateUpdateViewModel.TransactionType == 2) { transactionToUpdate.ReceivedAmount = transactionForCreateUpdateViewModel.Amount; } _uow.CustomerTransactions.Update(transactionToUpdate); _uow.Save(); TempData["Message"] = "Updated Successfully"; TempData["Status"] = "success"; } } } catch (DbUpdateConcurrencyException ce) { _logger.LogError(ce.Message, id); TempData["Message"] = "Failed to update for concurrency"; TempData["Status"] = "danger"; } catch (Exception ex) { _logger.LogError(ex.Message, ex.StackTrace); } return(RedirectToAction(nameof(Index))); }