public ActionResult deleteMaterialFromProduct(long productID, long materialID) { DeleteProductMaterialModelView deleteMaterialFromProductMV = new DeleteProductMaterialModelView(); deleteMaterialFromProductMV.productId = productID; deleteMaterialFromProductMV.materialId = materialID; try { new core.application.ProductController().deleteMaterialFromProduct(deleteMaterialFromProductMV); return(NoContent()); } catch (InvalidOperationException e) { //*this exception will occur if the last material is attempted to be removed*/ return(BadRequest(new SimpleJSONMessageService(e.Message))); } catch (ResourceNotFoundException e) { return(NotFound(new SimpleJSONMessageService(e.Message))); } catch (Exception) { return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR))); } }
/// <summary> /// Deletes a Material from a Product's Collection of Material. /// </summary> /// <param name="deleteMaterialFromProductMV">DeleteMaterialFromProductDTO with the material deletion information</param> /// <exception cref="ResourceNotFoundException">Thrown when either Product or the Material could not be found.</exception> public void deleteMaterialFromProduct(DeleteProductMaterialModelView deleteMaterialFromProductMV) { ProductRepository productRepository = PersistenceContext.repositories().createProductRepository(); Product productToRemoveMaterial = productRepository.find(deleteMaterialFromProductMV.productId); if (productToRemoveMaterial == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, deleteMaterialFromProductMV.productId)); } //filter through the product's current materials Material materialBeingDeleted = productToRemoveMaterial.productMaterials .Where(productMaterial => productMaterial.materialId == deleteMaterialFromProductMV.materialId) .Select(productMaterial => productMaterial.material).SingleOrDefault(); if (materialBeingDeleted == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_MATERIAL_BY_ID, deleteMaterialFromProductMV.materialId)); } productToRemoveMaterial.removeMaterial(materialBeingDeleted); productToRemoveMaterial = productRepository.update(productToRemoveMaterial); }