public GetComponentModelView findProductComponent(FindComponentModelView findComponentModelView) { ProductRepository productRepository = PersistenceContext.repositories().createProductRepository(); Product product = productRepository.find(findComponentModelView.fatherProductId); if (product == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, findComponentModelView.fatherProductId)); } //if no components are found, throw an exception so that a 404 code is sent if (!product.components.Any()) { throw new ResourceNotFoundException(ERROR_UNABLE_TO_FIND_COMPONENTS); } Component component = product.components.Where(c => c.complementaryProductId == findComponentModelView.childProductId).SingleOrDefault(); if (component == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, findComponentModelView.childProductId)); } return(ComponentModelViewService.fromEntity(component, findComponentModelView.unit)); }
/// <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); }
/// <summary> /// Finds a Product's collection of Component. /// </summary> /// <param name="findComponentsModelView">DTO containing information used for querying.</param> /// <returns>GetAllComponentsModelView with all of the elements in the Product's Collection of Component.</returns> /// <exception cref="ResourceNotFoundException">Thrown when the Product could not be found.</exception> public GetAllComponentsModelView findProductComponents(FindComponentsModelView findComponentsModelView) { Product product = PersistenceContext.repositories().createProductRepository().find(findComponentsModelView.fatherProductId); if (product == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, findComponentsModelView.fatherProductId)); } //if no components are found, throw an exception so that a 404 code is sent if (!product.components.Any()) { throw new ResourceNotFoundException(ERROR_UNABLE_TO_FIND_COMPONENTS); } if (findComponentsModelView.option == FindComponentsOptions.CATEGORY) { return(ComponentModelViewService.fromCollectionGroupedByCategory(product.components)); } else { return(ComponentModelViewService.fromCollection(product.components)); } }