/// <summary> /// Finds a Product's Measurement's Collection of Restriction. /// </summary> /// <param name="productMeasurementModelView">GetProductMeasurementModelView with the Product's and the Measurement's persistence identifier.</param> /// <returns>An instance of GetAllRestrictionsModelView containing the information of all the Measurement's restrictions.</returns> /// <exception cref="ResourceNotFoundException">Thrown when either the Product or the Measurement could not be found.</exception> public GetAllRestrictionsModelView findMeasurementRestrictions(FindMeasurementModelView productMeasurementModelView) { Product product = PersistenceContext.repositories().createProductRepository().find(productMeasurementModelView.productId); if (product == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, productMeasurementModelView.productId)); } //filter through the product's measurements Measurement measurement = product.productMeasurements .Where(productMeasurement => productMeasurement.measurementId == productMeasurementModelView.measurementId) .Select(productMeasurement => productMeasurement.measurement).SingleOrDefault(); if (measurement == null) { throw new ResourceNotFoundException( string.Format(ERROR_UNABLE_TO_FIND_MEASUREMENT_BY_ID, productMeasurementModelView.measurementId) ); } //if no restrictions are found, throw an exception so that a 404 code is sent if (!measurement.restrictions.Any()) { throw new ResourceNotFoundException(ERROR_UNABLE_TO_FIND_RESTRICTIONS); } return(RestrictionModelViewService.fromCollection(measurement.restrictions)); }
public ActionResult findMeasurementRestrictions(long productId, long measurementId) { FindMeasurementModelView productMeasurementMV = new FindMeasurementModelView(); productMeasurementMV.productId = productId; productMeasurementMV.measurementId = measurementId; try { GetAllRestrictionsModelView restrictionModelViews = new core.application.ProductController().findMeasurementRestrictions(productMeasurementMV); return(Ok(restrictionModelViews)); } catch (ResourceNotFoundException e) { return(NotFound(new SimpleJSONMessageService(e.Message))); } catch (Exception) { return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR))); } }