Beispiel #1
0
        private List <T> FilterTools <T>(Dictionary <FilterSearchData.Filters, object> filters, int take = 25, int skip = 0)
        {
            Dictionary <Type, KeyValuePair <ToolGetRequest, string> > requestsData =
                new Dictionary <Type, KeyValuePair <ToolGetRequest, string> >
            {
                {
                    typeof(ToolAssembly), new KeyValuePair <ToolGetRequest, string>(
                        this.ToolManagerRequests.Search.GetToolAssemblies,
                        "toolAssemblies")
                },
                {
                    typeof(CutterAssembly), new KeyValuePair <ToolGetRequest, string>(
                        this.ToolManagerRequests.Search.GetCutterAssemblies,
                        "cutterAssemblies")
                },
                {
                    typeof(Holder), new KeyValuePair <ToolGetRequest, string>(
                        this.ToolManagerRequests.Search.GetHolders,
                        "holders")
                }
            };

            ToolGetRequest request       = requestsData[typeof(T)].Key;
            string         targetSection = requestsData[typeof(T)].Value;

            var applyFilters = this.TransformFilters(filters);

            if (take > 100 && skip == 0)
            {
                return(this.ExecuteFilterRequestByPages <T>(take, applyFilters, request, targetSection));
            }
            else
            {
                var result = this.Client()
                             .PostAsync(request(applyFilters, take, skip, null, null));
                var tools = result.Result.GetDataFieldAs <List <T> >(targetSection);
                return(tools);
            }
        }
Beispiel #2
0
        private List <T> ExecuteFilterRequestByPages <T>(int take, Dictionary <FilterSearchData.Filters, object> applyFilters, ToolGetRequest request, string targetSection, int pageSize = 25)
        {
            List <T> tools = new List <T>();

            int  skipped           = 0;
            bool moresResultsExist = true;

            while (moresResultsExist && pageSize > 0)
            {
                pageSize = skipped + pageSize > take ? take - skipped : pageSize;

                if (pageSize > 0)
                {
                    int       counter = 0;
                    Exception error;
                    do
                    {
                        error = null;
                        try
                        {
                            var result       = this.Client.PostAsync(request(applyFilters, pageSize, skipped, null, null));
                            var resultsToAdd = result.Result.GetDataFieldAs <List <T> >(targetSection);
                            if (resultsToAdd.Count > 0)
                            {
                                tools.AddRange(resultsToAdd);
                            }

                            moresResultsExist = resultsToAdd.Count == pageSize;

                            skipped += pageSize;
                        }
                        catch (Exception e)
                        {
                            counter++;
                            Thread.Sleep(500);
                            error = e;
                        }
                    }while (error != null && counter < 10);

                    if (error != null)
                    {
                        throw error;
                    }
                }
            }

            return(tools);
        }