/// <summary> /// Adds a complementary Product to a Product. /// </summary> /// <param name="addComponentToProductMV">AddComponentToProductModelView containing the data of the complementary Product being added.</param> /// <returns>GetProductModelView with updated Product information.</returns> /// <exception cref="ResourceNotFoundException">Throw when either of the Products could not be found.</exception> public GetProductModelView addComponentToProduct(AddComponentModelView addComponentToProductMV) { ProductRepository productRepository = PersistenceContext.repositories().createProductRepository(); Product productToAddComponent = productRepository.find(addComponentToProductMV.fatherProductId); if (productToAddComponent == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, addComponentToProductMV.fatherProductId)); } Product componentBeingAdded = productRepository.find(addComponentToProductMV.childProductId); if (componentBeingAdded == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, addComponentToProductMV.childProductId)); } if (addComponentToProductMV.mandatory) { productToAddComponent.addMandatoryComplementaryProduct(componentBeingAdded); } else { productToAddComponent.addComplementaryProduct(componentBeingAdded); } productToAddComponent = productRepository.update(productToAddComponent); return(ProductModelViewService.fromEntity(productToAddComponent)); }
public ActionResult addComponentToProduct(long id, [FromBody] AddComponentModelView addComponentToProductMV) { if (addComponentToProductMV == null) { return(BadRequest(new SimpleJSONMessageService(INVALID_COMPONENT_DATA))); } addComponentToProductMV.fatherProductId = id; try { GetProductModelView productModelView = new core.application.ProductController().addComponentToProduct(addComponentToProductMV); return(Created(Request.Path, productModelView)); } catch (ResourceNotFoundException e) { return(NotFound(new SimpleJSONMessageService(e.Message))); } catch (ArgumentException e) { return(BadRequest(new SimpleJSONMessageService(e.Message))); } catch (Exception) { return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR))); } }