/// <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)); }
/// <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)); }
/// <summary> /// Converts an instance of Component into an instance of GetComponentModelView. /// </summary> /// <param name="component">Instance of Component.</param> /// <returns>An instance of GetComponentModelView.</returns> /// <exception cref="System.ArgumentNullException"> /// Thrown when the provided instance of Component is null. /// </exception> public static GetComponentModelView fromEntity(Component component, string unit) { if (component == null) { throw new ArgumentNullException(ERROR_NULL_COMPONENT); } GetComponentModelView componentModelView = new GetComponentModelView(); componentModelView.productId = component.complementaryProductId; componentModelView.reference = component.complementaryProduct.reference; componentModelView.designation = component.complementaryProduct.designation; componentModelView.modelFilename = component.complementaryProduct.modelFilename; componentModelView.mandatory = component.mandatory; componentModelView.category = ProductCategoryModelViewService.fromEntityAsBasic(component.complementaryProduct.productCategory); if (component.complementaryProduct.components.Any()) { componentModelView.components = ComponentModelViewService.fromCollection(component.complementaryProduct.components); } //no need to check if the product has materials and measurements, since they're mandatory componentModelView.materials = ProductMaterialModelViewService.fromCollection(component.complementaryProduct.productMaterials); componentModelView.measurements = MeasurementModelViewService.fromCollection(component.complementaryProduct.productMeasurements.Select(pm => pm.measurement), unit); if (component.complementaryProduct.supportsSlots) { componentModelView.slotWidths = ProductSlotWidthsModelViewService.fromEntity(component.complementaryProduct.slotWidths, unit); } /*Skip converting Restrictions if the Component has none, * since null GetAllRestrictionsModelView won't be serialized */ if (component.restrictions.Any()) { componentModelView.restrictions = RestrictionModelViewService.fromCollection(component.restrictions); } return(componentModelView); }
/// <summary> /// Converts an instance of ProductMaterial into an instance of GetProductMaterialModelView. /// </summary> /// <param name="productMaterial">Instance of ProductMaterial.</param> /// <returns>Instance of GetProductMaterialModelView.</returns> /// <exception cref="System.ArgumentException">Thrown when the provided instance of ProductMaterial is null.</exception> public static GetProductMaterialModelView fromEntity(ProductMaterial productMaterial) { if (productMaterial == null) { throw new ArgumentNullException(ERROR_NULL_PRODUCT_MATERIAL); } GetProductMaterialModelView productMaterialModelView = new GetProductMaterialModelView(); productMaterialModelView.id = productMaterial.materialId; productMaterialModelView.reference = productMaterial.material.reference; productMaterialModelView.designation = productMaterial.material.designation; productMaterialModelView.imageFilename = productMaterial.material.image; /*Skip converting Restrictions if the ProductMaterial has none, * since null GetAllRestrictionsModelView won't be serialized */ if (productMaterial.restrictions.Any()) { productMaterialModelView.restrictions = RestrictionModelViewService.fromCollection(productMaterial.restrictions); } return(productMaterialModelView); }
/// <summary> /// Finds a Product's Component's Collection of Restriction. /// </summary> /// <param name="componentModelView">GetComponentModelView with the parent and child Products' persistence identifiers.</param> /// <returns>An instance of GetAllRestrictionsModelView containing the information of all the Component's restrictions.</returns> /// <exception cref="ResourceNotFoundException">Thrown when either of the Products could not be found.</exception> public GetAllRestrictionsModelView findComponentRestrictions(FindComponentModelView componentModelView) { Product parentProduct = PersistenceContext.repositories().createProductRepository().find(componentModelView.fatherProductId); if (parentProduct == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, componentModelView.fatherProductId)); } Component component = parentProduct.components.Where(c => c.complementaryProductId == componentModelView.childProductId).SingleOrDefault(); if (component == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, componentModelView.childProductId)); } //if no restrictions are found, throw an exception so that a 404 code is sent if (!component.restrictions.Any()) { throw new ResourceNotFoundException(ERROR_UNABLE_TO_FIND_RESTRICTIONS); } return(RestrictionModelViewService.fromCollection(component.restrictions)); }