/// <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);
        }
        public void ensureFromModelViewCreatesInstance()
        {
            double minWidth         = 25;
            double maxWidth         = 50;
            double recommendedWidth = 35;

            string unit = "cm";

            AddProductSlotWidthsModelView productSlotWidthsModelView = new AddProductSlotWidthsModelView();

            productSlotWidthsModelView.minWidth         = minWidth;
            productSlotWidthsModelView.maxWidth         = maxWidth;
            productSlotWidthsModelView.recommendedWidth = recommendedWidth;
            productSlotWidthsModelView.unit             = unit;

            ProductSlotWidths slotWidths = ProductSlotWidthsModelViewService.fromModelView(productSlotWidthsModelView);

            double expectedMinWidth         = MeasurementUnitService.convertFromUnit(minWidth, unit);
            double expectedMaxWidth         = MeasurementUnitService.convertFromUnit(maxWidth, unit);
            double expectedRecommendedWidth = MeasurementUnitService.convertFromUnit(recommendedWidth, unit);

            Assert.Equal(expectedMinWidth, slotWidths.minWidth);
            Assert.Equal(expectedMaxWidth, slotWidths.maxWidth);
            Assert.Equal(expectedRecommendedWidth, slotWidths.recommendedWidth);
        }
Esempio n. 3
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);
        }
        public void ensureFromModelViewThrowsExceptionIfModelViewIsNull()
        {
            Action fromModelView = () => ProductSlotWidthsModelViewService.fromModelView(null);

            Assert.Throws <ArgumentNullException>(fromModelView);
        }
Esempio n. 10
0
        //TODO: use ProductBuilder here

        /// <summary>
        /// Creates a new instance of Product and saves it to the Repository.
        /// </summary>
        /// <param name="addProductMV">AddProductModelView containing the new Product's information.</param>
        /// <returns>Created instance of Product.</returns>
        /// <exception cref="System.ArgumentException">Throw </exception>
        public static Product create(AddProductModelView addProductMV)
        {
            string reference         = addProductMV.reference;
            string designation       = addProductMV.designation;
            string modelFilename     = addProductMV.modelFilename;
            long   productCategoryId = addProductMV.categoryId;

            List <AddMeasurementModelView> measurementModelViews = addProductMV.measurements;

            //NOTE: these checks are made here in order to avoid making requests to repositories unnecessarily
            if (measurementModelViews == null || !measurementModelViews.Any())
            {
                throw new ArgumentException(ERROR_NO_MEASUREMENTS_DEFINED);
            }

            List <AddProductMaterialModelView> materialViews = addProductMV.materials;

            //NOTE: these checks are made here in order to avoid making requests to repositories unnecessarily
            if (materialViews == null || !materialViews.Any())
            {
                throw new ArgumentException(ERROR_NO_MATERIALS_DEFINED);
            }

            ProductCategoryRepository categoryRepository = PersistenceContext.repositories().createProductCategoryRepository();

            ProductCategory category = categoryRepository.find(productCategoryId);

            if (category == null)
            {
                throw new ArgumentException(ERROR_CATEGORY_NOT_FOUND);
            }

            if (!categoryRepository.isLeaf(category))
            {
                throw new ArgumentException(ERROR_CATEGORY_NOT_LEAF);
            }

            List <Material>    materials          = new List <Material>();
            MaterialRepository materialRepository = PersistenceContext.repositories().createMaterialRepository();

            foreach (AddProductMaterialModelView materialModelView in materialViews)
            {
                if (materialModelView == null)
                {
                    throw new ArgumentException(ERROR_NULL_MATERIAL);
                }

                long materialId = materialModelView.materialId;

                Material material = materialRepository.find(materialId);
                if (material == null)
                {
                    throw new ArgumentException(string.Format(ERROR_MATERIAL_NOT_FOUND, materialId));
                }
                materials.Add(material);
            }

            IEnumerable <Measurement> measurements = MeasurementModelViewService.fromModelViews(addProductMV.measurements);

            List <AddComponentModelView>  componentModelViews = addProductMV.components;
            AddProductSlotWidthsModelView slotWidthsModelView = addProductMV.slotWidths;

            bool hasComponents = componentModelViews != null && componentModelViews.Any();
            bool hasSlots      = slotWidthsModelView != null;

            Product product = null;

            if (hasSlots)
            {
                ProductSlotWidths slotWidths = ProductSlotWidthsModelViewService.fromModelView(slotWidthsModelView);
                if (hasComponents)
                {
                    product = new Product(reference, designation, modelFilename, category, materials, measurements, slotWidths);
                    return(addComplementaryProducts(product, componentModelViews));
                }
                else
                {
                    return(new Product(reference, designation, modelFilename, category, materials, measurements, slotWidths));
                }
            }
            else
            {
                if (hasComponents)
                {
                    product = new Product(reference, designation, modelFilename, category, materials, measurements);
                    return(addComplementaryProducts(product, componentModelViews));
                }
                else
                {
                    return(new Product(reference, designation, modelFilename, category, materials, measurements));
                }
            }
        }