/// <summary> /// Finds a Product's Material's Collection of Restriction. /// </summary> /// <param name="productMaterialModelView">GetProductMaterialModelView with the Product and Material persistence identifiers.</param> /// <returns>An instance of GetAllRestrictionsModelView containing the information of all the Materials's restrictions.</returns> /// <exception cref="ResourceNotFoundException">Thrown when either the Product or the Material could not be found.</exception> public GetAllRestrictionsModelView findMaterialRestrictions(FindProductMaterialModelView productMaterialModelView) { Product product = PersistenceContext.repositories().createProductRepository().find(productMaterialModelView.productId); if (product == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, productMaterialModelView.productId)); } ProductMaterial productMaterial = product.productMaterials .Where(pm => pm.materialId == productMaterialModelView.materialId).SingleOrDefault(); if (productMaterial == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_MATERIAL_BY_ID, productMaterialModelView.materialId)); } //if no restrictions are found, throw an exception so that a 404 code is sent if (!productMaterial.restrictions.Any()) { throw new ResourceNotFoundException(ERROR_UNABLE_TO_FIND_RESTRICTIONS); } return(RestrictionModelViewService.fromCollection(productMaterial.restrictions)); }
public ActionResult findMaterialRestrictions(long productId, long materialId) { FindProductMaterialModelView productMaterialModelView = new FindProductMaterialModelView(); productMaterialModelView.productId = productId; productMaterialModelView.materialId = materialId; try { GetAllRestrictionsModelView restrictionModelViews = new core.application.ProductController().findMaterialRestrictions(productMaterialModelView); return(Ok(restrictionModelViews)); } catch (ResourceNotFoundException e) { return(NotFound(new SimpleJSONMessageService(e.Message))); } catch (Exception) { return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR))); } }