/// <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);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a model view with a product information.
        /// </summary>
        /// <param name="product">Product with the product being created the model view.</param>
        /// <param name="unit">Unit to which all the dimension data will be converted to.</param>
        /// <returns>GetProductModelView with the product information model view</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when the provided instance of Product is null.</exception>
        public static GetProductModelView fromEntity(Product product, string unit)
        {
            if (product == null)
            {
                throw new ArgumentNullException(ERROR_NULL_PRODUCT);
            }

            GetProductModelView productModelView = new GetProductModelView();

            productModelView.productId     = product.Id;
            productModelView.reference     = product.reference;
            productModelView.designation   = product.designation;
            productModelView.modelFilename = product.modelFilename;
            productModelView.category      = ProductCategoryModelViewService.fromEntityAsBasic(product.productCategory);
            if (product.components.Any())
            {
                productModelView.components = ComponentModelViewService.fromCollection(product.components);
            }
            //no need to check if the product has materials and measurements, since they're mandatory
            productModelView.materials    = ProductMaterialModelViewService.fromCollection(product.productMaterials);
            productModelView.measurements = MeasurementModelViewService.fromCollection(product.productMeasurements.Select(pm => pm.measurement), unit);
            if (product.supportsSlots)
            {
                productModelView.slotWidths = ProductSlotWidthsModelViewService.fromEntity(product.slotWidths, unit);
            }
            return(productModelView);
        }
        public void ensureFromEntityWithEmptyUnitUsesMinimumUnit()
        {
            double minWidth         = 25;
            double maxWidth         = 50;
            double recommendedWidth = 35;

            ProductSlotWidths slotWidths = ProductSlotWidths.valueOf(minWidth, maxWidth, recommendedWidth);

            GetProductSlotWidthsModelView result = ProductSlotWidthsModelViewService.fromEntity(slotWidths, "");

            string expectedUnit = MeasurementUnitService.getMinimumUnit();

            Assert.Equal(expectedUnit, result.unit);
        }
        /// <summary>
        /// Finds a Product's ProductSlotWidths.
        /// </summary>
        /// <param name="fetchProductDTO">DTO containing information used for querying.</param>
        /// <returns>GetProductSlotWidthsModelView representing the Product's ProductSlotWidths.</returns>
        /// <exception cref="ResourceNotFoundException">Thrown when the Product could not be found.</exception>
        public GetProductSlotWidthsModelView findProductSlotWidths(FetchProductDTO fetchProductDTO)
        {
            Product product = PersistenceContext.repositories().createProductRepository().find(fetchProductDTO.id);

            if (product == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, fetchProductDTO.id));
            }

            //?Should this be a BadRequest or a NotFound?
            if (!product.supportsSlots)
            {
                throw new InvalidOperationException(string.Format(ERROR_SLOTS_NOT_SUPPORTED, fetchProductDTO.id));
            }

            return(ProductSlotWidthsModelViewService.fromEntity(product.slotWidths, fetchProductDTO.productDTOOptions.requiredUnit));
        }
        public void ensureFromEntityCreatesModelViewWithExpectedData()
        {
            double minWidth         = 25;
            double maxWidth         = 50;
            double recommendedWidth = 35;

            ProductSlotWidths slotWidths = ProductSlotWidths.valueOf(minWidth, maxWidth, recommendedWidth);

            GetProductSlotWidthsModelView result = ProductSlotWidthsModelViewService.fromEntity(slotWidths);

            GetProductSlotWidthsModelView expected = new GetProductSlotWidthsModelView();

            expected.minWidth         = minWidth;
            expected.maxWidth         = maxWidth;
            expected.recommendedWidth = recommendedWidth;
            expected.unit             = MeasurementUnitService.getMinimumUnit();

            Assert.Equal(expected.minWidth, result.minWidth);
            Assert.Equal(expected.maxWidth, result.maxWidth);
            Assert.Equal(expected.recommendedWidth, result.recommendedWidth);
            Assert.Equal(expected.unit, result.unit);
        }
        public void ensureFromEntityWithUnitConvertsValues()
        {
            double minWidth         = 25;
            double maxWidth         = 50;
            double recommendedWidth = 35;

            ProductSlotWidths slotWidths = ProductSlotWidths.valueOf(minWidth, maxWidth, recommendedWidth);

            string unit = "dm";

            GetProductSlotWidthsModelView result = ProductSlotWidthsModelViewService.fromEntity(slotWidths, unit);

            GetProductSlotWidthsModelView expected = new GetProductSlotWidthsModelView();

            expected.minWidth         = MeasurementUnitService.convertToUnit(minWidth, unit);
            expected.maxWidth         = MeasurementUnitService.convertToUnit(maxWidth, unit);
            expected.recommendedWidth = MeasurementUnitService.convertToUnit(recommendedWidth, unit);
            expected.unit             = unit;

            Assert.Equal(expected.minWidth, result.minWidth);
            Assert.Equal(expected.maxWidth, result.maxWidth);
            Assert.Equal(expected.recommendedWidth, result.recommendedWidth);
            Assert.Equal(expected.unit, result.unit);
        }
        public void ensureFromEntityThrowsExceptionIfProductSlotWidthsIsNull()
        {
            Action fromEntity = () => ProductSlotWidthsModelViewService.fromEntity(null);

            Assert.Throws <ArgumentNullException>(fromEntity);
        }