public IActionResult AddNewItem(int productId, IEnumerable <int> optionalParameters) { bool success; try { var item = new Item { ProductId = productId }; UnitOfWork.Items.Insert(item); foreach (var parameter in optionalParameters) { if (UnitOfWork.OptionalParameters.FindById(parameter) is OptionalParameter optionalParameter) { var optionalParameterProduct = new OptionalParameterProduct { ItemId = item.Id, OptionalParameterId = optionalParameter.Id, Value = string.Empty }; UnitOfWork.OptionalParameterProducts.Insert(optionalParameterProduct); } } success = true; } catch (Exception ex) { success = false; } return(Json(new { success })); }
public IActionResult UpdateSpecifications(int productId, int optionalParameterId, bool toDelete = false) { bool success; try { var items = UnitOfWork.Items.Find(x => x.ProductId == productId, true); foreach (var item in items) { if (UnitOfWork.OptionalParameterProducts.FirstOrDefault(x => x.ItemId == item.Id && x.OptionalParameterId == optionalParameterId, true) is OptionalParameterProduct optionalParameterProduct) { if (toDelete) { UnitOfWork.OptionalParameterProducts.Delete(optionalParameterProduct); } else { UnitOfWork.OptionalParameterProducts.Restore(optionalParameterProduct); } } else { if (!toDelete) { optionalParameterProduct = new OptionalParameterProduct { ItemId = item.Id, OptionalParameterId = optionalParameterId, Value = string.Empty }; UnitOfWork.OptionalParameterProducts.Insert(optionalParameterProduct); } } } success = true; } catch (Exception ex) { success = false; } return(Json(new { success })); }