public IActionResult ClientProducts(ClientProductsEditViewModel model) { _logger.LogInformation($"Editing Customer PRODUCTS, ID: {model.Id}, user: {User.Identity.Name}"); if (ModelState.IsValid) { // Get current Domain Entity object Point client = _context.Set <Point>() ?.FindBy(p => p.PointNumber == model.Id)?.FirstOrDefault(); if (client != null) { // Update modified properties using (var transaction = _context.Set <ProductClient>().BeginTransaction()) { try { //Delete all existing records in ProductClient database var existingProductClients = _context.Set <ProductClient>() ?.FindBy(pc => pc.PointNumber == model.Id).ToList(); if (existingProductClients != null) { foreach (var productClient in existingProductClients) { _context.Set <ProductClient>() .Delete(productClient); } } //Insert new records from posted model var postedProductClients = model.ProductsAll?.Where(p => p.Active == true)?.ToList(); if (postedProductClients != null) { foreach (var product in postedProductClients) { _context.Set <ProductClient>() .Add(new ProductClient { PointNumber = model.Id, TovarNumber = product.ProductId }); } } _logger.LogInformation($"UPDATED Customer PRODUCTS, ID: {model.Id}, user: {User.Identity.Name}"); transaction.Commit(); } catch (Exception ex) { _logger.LogError($"ERROR on UPDATING Customer PRODUCTS, ID: {model.Id}, user: {User.Identity.Name}," + $"ERROR: {ex.Message}"); transaction.Rollback(); } } } else { _logger.LogWarning($"Customer with ID: {model.Id} NOT FOUND!"); ModelState.AddModelError("ClientEdit", $"Клиент с данным id: {model.Id} отсутствует в БД"); } } return(View(model)); }
public IActionResult ClientProducts(int id) { _logger.LogInformation($"Getting Customer PRODUCTS, ID: {id}, user: {User.Identity.Name}"); if (_context.Set <Point>().FindBy(p => p.PointNumber == id) != null) { var model = new ClientProductsEditViewModel(); model.Id = id; var productsAll = _context.Set <Tovar>() ?.FindBy(t => t.KatNumberNavigation.TopKat == 3) ?.Include(t => t.KatNumberNavigation) ?.Select(t => new ProductDto { ProductId = t.TovarNumber, Name = t.TovarName, Active = false }); var productsClient = _context.Set <ProductClient>() ?.FindBy(pc => pc.PointNumber == model.Id) ?.Select(pc => new ProductDto { ProductId = pc.TovarNumber }); model.ProductsAll = productsAll?.ToList() ?? model.ProductsAll; model.ProductsClient = productsClient?.ToList() ?? model.ProductsClient; return(PartialView(model)); } else { _logger.LogWarning($"Customer with ID: {id} NOT FOUND!"); return(NotFound($"Клиент с id: {id} не найден.")); } }