public async Task <IEnumerable <ActivityRecord> > BrowseAsync(BrowseQuery query)
        {
            IQueryable <ActivityRecord> contextQuery = _lifeLogContext.ActivityRecords;

            if (query.DayRecordId.HasValue)
            {
                contextQuery = contextQuery.Where(activityRecord => activityRecord.DayRecordId == query.DayRecordId.Value);
            }

            return(await contextQuery.ToListAsync());
        }
Beispiel #2
0
        public async Task <IEnumerable <DayRecord> > BrowseAsync(BrowseQuery query)
        {
            IQueryable <DayRecord> contextQuery = _lifeLogContext.DayRecords;

            if (query.Year.HasValue)
            {
                contextQuery = contextQuery.Where(dayRecord => dayRecord.Date.Year == query.Year.Value);
            }

            if (query.Month.HasValue)
            {
                contextQuery = contextQuery.Where(dayRecord => dayRecord.Date.Month == query.Month.Value);
            }

            return(await contextQuery.ToListAsync());
        }
        /// <summary>
        ///     List items matching the given query
        /// </summary>
        public virtual async Task <ProductSearchResult> GetProductsAsync(
            string store,
            string language,
            BrowseQuery query,
            ItemResponseGroups?responseGroup = null)
        {
            var parameters = new
            {
                store,
                language,
                responseGroup = responseGroup.GetHashCode()
                                .ToString(CultureInfo.InvariantCulture)
            };

            return
                (await GetAsync <ProductSearchResult>(
                     CreateRequestUri(RelativePaths.ProductsSearch, query.GetQueryString(parameters))).ConfigureAwait(false));
        }
Beispiel #4
0
        private void LoadSearchResults()
        {
            if (!this.Performed)
            {
                return;
            }

            var pageSize = this.Context == null ? 20 : this.Context["paginate.page_size"].ToInt(20);
            var skip     = this.Context == null ? 0 : this.Context["paginate.current_offset"].ToInt();
            var terms    = this.Terms; //this.Context["current_query"] as string;
            var type     = this.Context == null ? "product" : this.Context["current_type"] as string;

            var siteContext = SiteContext.Current;
            var service     = CommerceService.Create();
            var searchQuery = new BrowseQuery()
            {
                Skip = skip, Take = pageSize, Search = terms
            };
            var response = Task.Run(() => service.SearchAsync <object>(siteContext, searchQuery)).Result;

            this.Results = response;

            this.Performed = false;
        }
Beispiel #5
0
        /// <summary>
        ///     Gets an HTTP query string serialization of this query compatible with FromQueryString
        /// </summary>
        public static string GetQueryString(this BrowseQuery query, object additionalParameters = null)
        {
            var parts = new List <string>();

            if (query.Skip != null)
            {
                parts.Add("skip=" + HttpUtility.UrlEncode(query.Skip.Value.ToString(CultureInfo.InvariantCulture)));
            }

            if (query.Take != null)
            {
                parts.Add("take=" + HttpUtility.UrlEncode(query.Take.Value.ToString(CultureInfo.InvariantCulture)));
            }

            if (!string.IsNullOrEmpty(query.SortProperty))
            {
                parts.Add("sort=" + HttpUtility.UrlEncode(query.SortProperty));
            }

            if (!string.IsNullOrEmpty(query.SortDirection))
            {
                parts.Add("sortorder=" + HttpUtility.UrlEncode(query.SortDirection));
            }

            if (!string.IsNullOrEmpty(query.Search))
            {
                parts.Add("searchPhrase=" + HttpUtility.UrlEncode(query.Search));
            }

            if (!string.IsNullOrEmpty(query.Outline))
            {
                parts.Add("outline=" + HttpUtility.UrlEncode(query.Outline));
            }

            if (query.StartDateFrom.HasValue)
            {
                parts.Add(
                    "startDateFrom="
                    + HttpUtility.UrlEncode(query.StartDateFrom.Value.ToString("s", CultureInfo.InvariantCulture)));
            }

            if (query.Filters != null && query.Filters.Count > 0)
            {
                parts.AddRange(
                    query.Filters.Select(
                        filter => string.Format("terms={0}:{1}", filter.Key, string.Join(",", filter.Value))));
            }

            if (query.PriceLists != null && query.PriceLists.Length > 0)
            {
                parts.AddRange(
                    query.PriceLists.Select(
                        pricelist => string.Format("pricelists={0}", pricelist)));
            }

            if (additionalParameters != null)
            {
                var parameters = additionalParameters.ToPropertyDictionary();
                parts.AddRange(parameters.Select(parameter => string.Format("{0}={1}", parameter.Key, parameter.Value)));
            }

            return(string.Join("&", parts));
        }
Beispiel #6
0
        public void LoadSlice(int from, int?to)
        {
            var pageSize = to == null ? 50 : to - from;

            var tags = this.Context["current_tags"] as SelectedTagCollection;

            var filters = new Dictionary <string, string[]>();

            if (tags != null && tags.Any())
            {
                // split tags to field=value using "_", if there is no "_", then simply add them to "tags"=values
                var tagsMultiArray = tags.Select(t => t.Split(new[] { '_' }));

                var tagsArray = new List <Tuple <string, string> >();

                // add tags that have "_"
                tagsArray.AddRange(tagsMultiArray.Where(x => x.Length > 1).Select(x => new Tuple <string, string>(x[0], x[1])));

                // add the rest that don't have "_" as tags, will sort them out on the server api
                tagsArray.AddRange(tagsMultiArray.Where(x => x.Length == 1).Select(x => new Tuple <string, string>("tags", x[0])));

                foreach (var tagsGroup in tagsArray.GroupBy(x => x.Item1))
                {
                    filters.Add(tagsGroup.Key, tagsGroup.Select(g => g.Item2).ToArray());
                }
            }

            var service = CommerceService.Create();
            var context = SiteContext.Current;

            var sortProperty  = String.Empty;
            var sortDirection = "ascending";

            var sort = string.IsNullOrEmpty(this.SortBy) ? this.DefaultSortBy : this.SortBy;

            if (sort.Equals("manual"))
            {
                sortProperty = "position";
            }
            else if (sort.Equals("best-selling"))
            {
                sortProperty = "position";
            }
            else
            {
                var sortArray = sort.Split(new[] { '-' });
                if (sortArray.Length > 1)
                {
                    sortProperty  = sortArray[0];
                    sortDirection = sortArray[1];
                }
            }

            var searchQuery = new BrowseQuery()
            {
                SortProperty = sortProperty, SortDirection = sortDirection, Filters = filters, Skip = from, Take = pageSize.Value, Outline = Id == "All" ? "" : this.BuildSearchOutline()
            };
            var response =
                Task.Run(() => service.SearchAsync <Product>(context,
                                                             searchQuery, this, responseGroups: ItemResponseGroups.ItemSmall | ItemResponseGroups.Variations)).Result;

            // populate tags with facets returned
            if (response.Facets != null && response.Facets.Any())
            {
                var values = response.Facets.SelectMany(f => f.Values.Select(v => v.AsWebModel(f.Field)));
                this.Tags = new TagCollection(values);
            }

            this.AllProductsCount = response.TotalCount;

            this.Products = response;
        }
        public async Task <SearchResults <T> > SearchAsync <T>(SiteContext context, BrowseQuery query, Collection parentCollection = null, ItemResponseGroups?responseGroups = ItemResponseGroups.ItemMedium)
        {
            var priceLists = context.PriceLists;

            query.PriceLists = priceLists;

            var response =
                await
                this._browseClient.GetProductsAsync(
                    context.StoreId,
                    context.Language,
                    query,
                    responseGroups);

            if (!response.Items.Any()) // no results found
            {
                return(new SearchResults <T>(Enumerable.Empty <T>()));
            }

            var allIds = response.Items.ToArray().GetAllVariationIds();
            var prices = await GetProductPricesAsync(priceLists, allIds.ToArray());

            var promoContext = new PromotionEvaluationContext
            {
                CustomerId       = context.CustomerId,
                CartTotal        = context.Cart.TotalPrice,
                Currency         = context.Shop.Currency,
                IsRegisteredUser = context.Customer != null,
                StoreId          = context.StoreId
            };

            var promoEntries = new List <ProductPromoEntry>();

            foreach (var item in response.Items)
            {
                var price = prices.FirstOrDefault(p => p.ProductId == item.Code);
                if (item.Variations != null)
                {
                    foreach (var variation in item.Variations)
                    {
                        price = prices.FirstOrDefault(p => p.ProductId == variation.Code);
                    }
                }

                promoEntries.Add(new ProductPromoEntry
                {
                    CatalogId = item.CatalogId,
                    Price     = price != null ? (price.Sale.HasValue ? price.Sale.Value : price.List) : 0M,
                    ProductId = item.Id,
                    Quantity  = 1
                });
            }
            promoContext.PromoEntries = promoEntries;

            var rewards = await _marketingClient.GetPromotionRewardsAsync(promoContext);

            var result = new SearchResults <T>(response.Items.Select(i => i.AsWebModel(prices, rewards, parentCollection)).OfType <T>())
            {
                TotalCount = response.TotalCount
            };

            if (response.Facets != null && response.Facets.Any())
            {
                result.Facets = response.Facets.Select(x => x.AsWebModel()).ToArray();
            }

            return(result);
        }
Beispiel #8
0
        public async Task <IEnumerable <Activity> > BrowseAsync(BrowseQuery query)
        {
            IQueryable <Activity> contextQuery = _lifeLogContext.Activities;

            return(await contextQuery.ToListAsync());
        }
Beispiel #9
0
        public void LoadSlice(int from, int?to)
        {
            var pageSize = to == null ? 50 : to - from;

            var tags = this.Context["current_tags"] as string[];

            var filters = new Dictionary <string, string[]>();

            if (tags != null && tags.Any())
            {
                var tagsArray =
                    tags.Select(t => t.Split(new[] { '_' })).Select(x => new Tuple <string, string>(x[0], x[1]));

                foreach (var tagsGroup in tagsArray.GroupBy(x => x.Item1))
                {
                    filters.Add(tagsGroup.Key, tagsGroup.Select(g => g.Item2).ToArray());
                }
            }

            var service = new CommerceService();
            var context = SiteContext.Current;

            var sortProperty  = String.Empty;
            var sortDirection = "ascending";

            var sort = string.IsNullOrEmpty(this.SortBy) ? this.DefaultSortBy : this.SortBy;

            if (sort.Equals("manual"))
            {
                sortProperty = "position";
            }
            else if (sort.Equals("best-selling"))
            {
                sortProperty = "position";
            }
            else
            {
                var sortArray = sort.Split(new[] { '-' });
                if (sortArray.Length > 1)
                {
                    sortProperty  = sortArray[0];
                    sortDirection = sortArray[1];
                }
            }

            var searchQuery = new BrowseQuery()
            {
                SortProperty = sortProperty, SortDirection = sortDirection, Filters = filters, Skip = from, Take = pageSize.Value, Outline = this.BuildSearchOutline()
            };
            var response =
                Task.Run(() => service.SearchAsync <Product>(context,
                                                             searchQuery, this, responseGroups: ItemResponseGroups.ItemSmall | ItemResponseGroups.Variations)).Result;

            // populate tags with facets returned
            if (response.Facets != null && response.Facets.Any())
            {
                var values = response.Facets.SelectMany(f => f.Values.Select(v => v.AsWebModel(f.Field)));
                this.Tags = new TagCollection(values);
            }

            this.Products = response;
        }
Beispiel #10
0
        public BrowseResult Read(BrowseQuery query)
        {
            var response = processXml.Process(query.ToXml());

            return(BrowseResult.FromXml(response));
        }