/// <summary> /// Adds a Restriction to a Product's complementary Product. /// </summary> /// <param name="addRestrictionToProductComponentMV">AddRestrictionToProductComponentModelView containing the data of the Restriction instance being added.</param> /// <returns>GetProductModelView with updated Product information.</returns> /// <exception cref="ResourceNotFoundException">Thrown when either of the Products could not be found.</exception> public GetProductModelView addRestrictionToProductComponent(AddComponentRestrictionModelView addRestrictionToProductComponentMV) { ProductRepository productRepository = PersistenceContext.repositories().createProductRepository(); Product parentProduct = productRepository.find(addRestrictionToProductComponentMV.fatherProductId); if (parentProduct == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, addRestrictionToProductComponentMV.fatherProductId)); } //filter product's components rather than accessing the repository Product childProduct = parentProduct.components .Where(component => component.complementaryProduct.Id == addRestrictionToProductComponentMV.childProductId) .Select(component => component.complementaryProduct).SingleOrDefault(); if (childProduct == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, addRestrictionToProductComponentMV.childProductId)); } Algorithm algorithm = new AlgorithmFactory().createAlgorithm(addRestrictionToProductComponentMV.restriction.algorithmId); if (addRestrictionToProductComponentMV.restriction.inputValues != null) { algorithm.setInputValues(InputValueModelViewService.toDictionary(addRestrictionToProductComponentMV.restriction.inputValues)); } algorithm.ready(); Restriction restriction = new Restriction(addRestrictionToProductComponentMV.restriction.description, algorithm); parentProduct.addComplementaryProductRestriction(childProduct, restriction); parentProduct = productRepository.update(parentProduct); return(ProductModelViewService.fromEntity(parentProduct)); }
public ActionResult addRestrictionToProductComponent(long productID, long componentID, [FromBody] AddRestrictionModelView restrictionMV) { if (restrictionMV == null) { return(BadRequest(new SimpleJSONMessageService(INVALID_RESTRICTION_DATA))); } AddComponentRestrictionModelView addRestrictionToProductComponentDTO = new AddComponentRestrictionModelView(); addRestrictionToProductComponentDTO.fatherProductId = productID; addRestrictionToProductComponentDTO.childProductId = componentID; addRestrictionToProductComponentDTO.restriction = restrictionMV; try { GetProductModelView appliedRestrictionModelView = new core.application.ProductController().addRestrictionToProductComponent(addRestrictionToProductComponentDTO); return(Created(Request.Path, appliedRestrictionModelView)); } 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))); } }