public ActionResult deleteComponentFromProduct(long productID, long componentID) { DeleteComponentModelView deleteComponentFromProductMV = new DeleteComponentModelView(); deleteComponentFromProductMV.fatherProductId = productID; deleteComponentFromProductMV.childProductId = componentID; try { new core.application.ProductController().deleteComponentFromProduct(deleteComponentFromProductMV); return(NoContent()); } catch (ResourceNotFoundException e) { return(NotFound(new SimpleJSONMessageService(e.Message))); } catch (Exception) { return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR))); } }
/// <summary> /// Deletes a Product's complementary Product. /// </summary> /// <param name="deleteComponentFromProductMV">DeleteComponentFromProductDTO with the component deletion information<</param> ///<exception cref="ResourceNotFoundException">Thrown when either of the Products could not be found.</exception> public void deleteComponentFromProduct(DeleteComponentModelView deleteComponentFromProductMV) { ProductRepository productRepository = PersistenceContext.repositories().createProductRepository(); Product productToRemoveComponent = productRepository.find(deleteComponentFromProductMV.fatherProductId); if (productToRemoveComponent == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, deleteComponentFromProductMV.fatherProductId)); } //filter product's components rather than accessing the repository Product productBeingDeleted = productToRemoveComponent.components .Where(component => component.complementaryProduct.Id == deleteComponentFromProductMV.childProductId) .Select(component => component.complementaryProduct).SingleOrDefault(); if (productBeingDeleted == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, deleteComponentFromProductMV.childProductId)); } productToRemoveComponent.removeComplementaryProduct(productBeingDeleted); productToRemoveComponent = productRepository.update(productToRemoveComponent); }