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)); }
public ActionResult findProductComponent(long productId, long componentId, [FromQuery] string unit) { try { FindComponentModelView findComponentModelView = new FindComponentModelView(); findComponentModelView.fatherProductId = productId; findComponentModelView.childProductId = componentId; findComponentModelView.unit = unit; GetComponentModelView componentModelView = new core.application.ProductController().findProductComponent(findComponentModelView); return(Ok(componentModelView)); } catch (ResourceNotFoundException e) { return(NotFound(new SimpleJSONMessageService(e.Message))); } catch (Exception) { return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR))); } }
public ActionResult findComponentRestrictions(long parentProductId, long complementaryProductId) { FindComponentModelView componentModelView = new FindComponentModelView(); componentModelView.fatherProductId = parentProductId; componentModelView.childProductId = complementaryProductId; try { GetAllRestrictionsModelView restrictionModelViews = new core.application.ProductController().findComponentRestrictions(componentModelView); return(Ok(restrictionModelViews)); } catch (ResourceNotFoundException e) { return(NotFound(new SimpleJSONMessageService(e.Message))); } catch (Exception) { return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR))); } }
/// <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)); }