public IActionResult Put([FromBody] ProductsToCustomerViewModel productsToCustomer) { try { var productToCustomer = _productsToCustomer.GetAsync(productsToCustomer.Id).Result; var customer = _CustomerService.GetByName(productsToCustomer.CustomerName).Result.Id; var product = _productService.GetByName(productsToCustomer.ProductName).Result.Id; productToCustomer.CustomerId = customer; productToCustomer.ProductId = product; _productsToCustomer.UpdateAsync(productToCustomer); } catch (Exception) { return(BadRequest("Some Error while add Products To Customer")); } return(Ok()); }
public ActionResult Post(ProductsToCustomerViewModel productsToCustomer) { try { var customer = 0; var product = 0; if (!string.IsNullOrEmpty(productsToCustomer.CustomerName)) { customer = _CustomerService.GetByName(productsToCustomer.CustomerName).Result.Id; } if (!string.IsNullOrEmpty(productsToCustomer.ProductName)) { product = _productService.GetByName(productsToCustomer.ProductName).Result.Id; } var productToCustomer = new ProductsToCustomer { CustomerId = customer == 0 ? 0 : customer, ProductId = product == 0 ? 0 : product }; if (productToCustomer.CustomerId > 0 && productToCustomer.ProductId > 0) { _productsToCustomer.AddAsync(productToCustomer); } else { return(Ok(new { status = "Failed" })); } } catch (Exception) { return(BadRequest("Some Error while add Products To Customer")); } return(Ok(new { status = "Success" })); }