public ActionResult addMaterialToProduct(long id, [FromBody] AddProductMaterialModelView addMaterialToProductMV) { if (addMaterialToProductMV == null) { return(BadRequest(new SimpleJSONMessageService(INVALID_MATERIAL_DATA))); } addMaterialToProductMV.productId = id; try { GetProductModelView productModelView = new core.application.ProductController().addMaterialToProduct(addMaterialToProductMV); 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))); } }
/// <summary> /// Adds a Material to a Product. /// </summary> /// <param name="addMaterialToProductMV">AddMaterialToProductModelView with the material addition information</param> /// <returns>GetProductModelView with updated Product information.</returns> ///<exception cref="ResourceNotFoundException">Thrown when either the Product or the Material could not be found.</exception> public GetProductModelView addMaterialToProduct(AddProductMaterialModelView addMaterialToProductMV) { ProductRepository productRepository = PersistenceContext.repositories().createProductRepository(); Product productToAddMaterial = productRepository.find(addMaterialToProductMV.productId); if (productToAddMaterial == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, addMaterialToProductMV.productId)); } Material materialBeingAdded = PersistenceContext.repositories().createMaterialRepository().find(addMaterialToProductMV.materialId); if (materialBeingAdded == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_MATERIAL_BY_ID, addMaterialToProductMV.materialId)); } productToAddMaterial.addMaterial(materialBeingAdded); productToAddMaterial = productRepository.update(productToAddMaterial); return(ProductModelViewService.fromEntity(productToAddMaterial)); }