Beispiel #1
0
        public IHttpActionResult Get(ODataQueryOptions <Product> queryOptions)
        {
            logger.Trace("Call ProductsController GetProducts");

            try
            {
                queryOptions.Validate(_validationSettings);
            }
            catch (ODataException ex)
            {
                return(BadRequest(ex.Message));
            }

            var data = productRepository.Get();

            data = data.Select(r =>
            {
                r.Price      = productPriceService.GetByProductId(r.Id);
                r.Categories = productCategoryService.GetCategoriesByProductId(r.Id).ToList();

                return(r);
            });

            var query = queryOptions
                        .ApplyTo(data.AsQueryable());

            //data = query.ToList();

            return(Ok(data));
        }
        public async Task <IHttpActionResult> Get(ODataQueryOptions <Product> queryOptions)
        {
            logger.Trace("Call ProductsController GetProducts");

            try
            {
                queryOptions.Validate(_validationSettings);
            }
            catch (ODataException ex)
            {
                return(BadRequest(ex.Message));
            }

            var allProducts = productRepository.Get();

            var task = allProducts.Select(async r =>
            {
                var links = await linkRepository.GetByReferenceIdAsync(r.Id);

                var specifications = await productSpecificationRepository.GetByParentIdAsync(r.Id);

                var specifications_task = specifications.Select(async s =>
                {
                    s.Child    = productRepository.GetById(s.ChildId);
                    s.ChildUom = await uomRepository.GetByIdAsync(s.ChildUomId);
                    return(s);
                });

                var specifications_list = await Task.WhenAll(specifications_task);

                r.Price       = productPriceService.GetByProductId(r.Id);
                r.Categories  = productCategoryService.GetCategoriesByProductId(r.Id).ToList();
                r.Links       = links.ToList();
                r.Ingredients = specifications_list;

                return(r);
            });

            var data = await Task.WhenAll(task);

            //var query = queryOptions
            //    .ApplyTo(data.AsQueryable());

            //data = query.ToList();

            return(Ok(data));
        }
Beispiel #3
0
        public IHttpActionResult Post([FromBody] OrderLine entity)
        {
            logger.Trace("Call OrderLineController Post");

            var price = productPriceRepository.GetByProductId(entity.ItemId);

            if (price != null)
            {
                entity.Price  = (float)price.Price;
                entity.Amount = entity.Qty * entity.Price;
            }

            var record = orderLineRepository.Create(entity);

            Task.Factory.StartNew(() => RecalcOrderAsync(entity.OrderId));

            return(Created(record));
        }
Beispiel #4
0
        public async Task <IHttpActionResult> Get(ODataQueryOptions <Order> queryOptions)
        {
            logger.Trace("Call OrderController Get All");

            try
            {
                _validationSettings.MaxExpansionDepth = 5;
                queryOptions.Validate(_validationSettings);
            }
            catch (ODataException ex)
            {
                return(BadRequest(ex.Message));
            }

            if (queryOptions.SelectExpand != null)
            {
                foreach (Microsoft.OData.Core.UriParser.Semantic.SelectItem item in queryOptions.SelectExpand.SelectExpandClause.SelectedItems)
                {
                    if (item.GetType() == typeof(Microsoft.OData.Core.UriParser.Semantic.ExpandedNavigationSelectItem))
                    {
                        Microsoft.OData.Core.UriParser.Semantic.ExpandedNavigationSelectItem navigationProperty = (Microsoft.OData.Core.UriParser.Semantic.ExpandedNavigationSelectItem)item;

                        // Get the name of the property expanded (this way you can control which navigation property you are about to expand)
                        var propertyName = (navigationProperty.PathToNavigationProperty.FirstSegment as Microsoft.OData.Core.UriParser.Semantic.NavigationPropertySegment).NavigationProperty.Name.ToLowerInvariant();

                        // Get skip and top nested filters:
                        // var skip = navigationProperty.SkipOption;
                        // var top = navigationProperty.TopOption;

                        /* Here you should retrieve from your DB the entities that you
                         * will return as a result of the requested expand clause with nested filters
                         * ... */
                    }
                }
            }

            var data = await orderRepository.GetAllAsync();

            var expandedDataTask = data
                                   .Select(async r =>
            {
                var lineQuery = await orderLineRepository.GetByOrderIdAsync(r.Id);

                r.Lines = lineQuery
                          .Select(l =>
                {
                    l.Item = productRepository.GetById(l.ItemId);
                    if (l.Item != null)
                    {
                        l.Item.Price      = productPriceService.GetByProductId(l.ItemId);
                        l.Item.Categories = productCategoryService.GetCategoriesByProductId(l.ItemId).ToList();
                    }
                    return(l);
                })
                          .ToList();

                return(r);
            })
                                   .ToList();

            var expandedData = await Task.WhenAll(expandedDataTask);

            //var query = queryOptions.ApplyTo(data.AsQueryable());

            return(Ok(expandedData));
        }