public ActionResult findProductComponents(long productId, [FromQuery] FindComponentsOptions groupBy) { try { FindComponentsModelView findComponentsModel = new FindComponentsModelView(); findComponentsModel.fatherProductId = productId; findComponentsModel.option = groupBy; GetAllComponentsModelView allComponentsByCategory = new core.application.ProductController().findProductComponents(findComponentsModel); return(Ok(allComponentsByCategory)); } catch (ResourceNotFoundException e) { return(NotFound(new SimpleJSONMessageService(e.Message))); } catch (Exception) { return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR))); } }
/// <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)); } }