private async Task <Order> RecalcOrderAsync(Guid id) { var order = orderRepository.GetById(id); if (order != null) { var lines = await orderLineRepository.GetByOrderIdAsync(id); order.LinesCount = Convert.ToInt16(lines.Count()); order.Amount = lines.Sum(r => r.Amount); order = await orderRepository.UpdateAsync(order); } return(order); }
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)); }