public override void ExecuteCmdlet()
        {
            FilterGalleryTemplatesOptions options = new FilterGalleryTemplatesOptions()
            {
                Category = Category,
                Identity = Identity,
                Publisher = Publisher
            };

            List<PSGalleryItem> galleryItems = GalleryTemplatesClient.FilterGalleryTemplates(options);

            if (galleryItems != null)
            {
                if (galleryItems.Count == 1 && !string.IsNullOrEmpty(Identity))
                {
                    WriteObject(galleryItems[0]);
                }
                else
                {
                    List<PSObject> output = new List<PSObject>();
                    galleryItems.Where(gi => !gi.Identity.EndsWith("-placeholder"))
                    .OrderBy(gi => gi.Identity)
                    .ToList()
                    .ForEach(gi => output.Add(base.ConstructPSObject(
                        null,
                        "Publisher", gi.Publisher,
                        "Identity", gi.Identity)));
                    WriteObject(output, true);
                }
            }
        }
        /// <summary>
        /// Filters gallery templates based on the passed options.
        /// </summary>
        /// <param name="options">The filter options</param>
        /// <returns>The filtered list</returns>
        public virtual List <PSGalleryItem> FilterGalleryTemplates(FilterGalleryTemplatesOptions options)
        {
            List <string>      filterStrings = new List <string>();
            ItemListParameters parameters    = null;
            List <GalleryItem> result        = new List <GalleryItem>();

            if (!string.IsNullOrEmpty(options.Identity))
            {
                result.Add(GalleryClient.Items.Get(options.Identity).Item);
            }
            else
            {
                result.AddRange(QueryGalleryTemplates(options, filterStrings, parameters));
            }

            return(result.Select(i => i.ToPSGalleryItem()).ToList());
        }
        /// <summary>
        /// Filters gallery templates based on the passed options.
        /// </summary>
        /// <param name="options">The filter options</param>
        /// <returns>The filtered list</returns>
        public virtual List<PSGalleryItem> FilterGalleryTemplates(FilterGalleryTemplatesOptions options)
        {
            List<string> filterStrings = new List<string>();
            ItemListParameters parameters = null;
            List<GalleryItem> result = new List<GalleryItem>();

            if (!string.IsNullOrEmpty(options.Identity))
            {
                result.Add(GalleryClient.Items.Get(options.Identity).Item);
            }
            else
            {
                result.AddRange(QueryGalleryTemplates(options, filterStrings, parameters));
            }

            return result.Select(i => i.ToPSGalleryItem()).ToList();
        }
        private List <GalleryItem> QueryGalleryTemplates(FilterGalleryTemplatesOptions options, List <string> filterStrings, ItemListParameters parameters)
        {
            if (!string.IsNullOrEmpty(options.Publisher))
            {
                filterStrings.Add(FilterString.Generate <ItemListFilter>(f => f.Publisher == options.Publisher));
            }

            if (!string.IsNullOrEmpty(options.Category))
            {
                filterStrings.Add(FilterString.Generate <ItemListFilter>(f => f.CategoryIds.Contains(options.Category)));
            }

            if (filterStrings.Count > 0)
            {
                parameters = new ItemListParameters()
                {
                    Filter = string.Join(" and ", filterStrings)
                };
            }

            return(GalleryClient.Items.List(parameters).Items.ToList());
        }
        public void GetsGalleryTemplates()
        {
            FilterGalleryTemplatesOptions options = new FilterGalleryTemplatesOptions()
            {
                Category = "category",
                Identity = "hobba",
                Publisher = "Microsoft"
            };
            FilterGalleryTemplatesOptions actual = new FilterGalleryTemplatesOptions();
            List<PSGalleryItem> result = new List<PSGalleryItem>()
            {
                new PSGalleryItem()
                {
                    Publisher = "Microsoft",
                    Identity = "T1"
                },
                new PSGalleryItem()
                {
                    Publisher = "Microsoft",
                    Identity = "T2"
                },
            };
            galleryTemplatesClientMock.Setup(f => f.FilterGalleryTemplates(It.IsAny<FilterGalleryTemplatesOptions>()))
                .Returns(result)
                .Callback((FilterGalleryTemplatesOptions o) => actual = o);

            cmdlet.Category = options.Category;
            cmdlet.Identity = options.Identity;
            cmdlet.Publisher = options.Publisher;

            cmdlet.ExecuteCmdlet();

            Assert.Equal(2, result.Count);
            Assert.True(result.All(g => g.Publisher == "Microsoft"));

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<List<PSObject>>(), true), Times.Once());
        }
        private List<GalleryItem> QueryGalleryTemplates(FilterGalleryTemplatesOptions options, List<string> filterStrings, ItemListParameters parameters)
        {
            if (!string.IsNullOrEmpty(options.Publisher))
            {
                filterStrings.Add(FilterString.Generate<ItemListFilter>(f => f.Publisher == options.Publisher));
            }

            if (!string.IsNullOrEmpty(options.Category))
            {
                filterStrings.Add(FilterString.Generate<ItemListFilter>(f => f.CategoryIds.Contains(options.Category)));
            }

            if (filterStrings.Count > 0)
            {
                parameters = new ItemListParameters() { Filter = string.Join(" and ", filterStrings) };
            }

            return GalleryClient.Items.List(parameters).Items.ToList();
        }
        public void FiltersGalleryTemplatesUsingComplexQuery()
        {
            string filterString = "Publisher eq 'Microsoft' and CategoryIds/any(c: c eq 'awesome')";
            ItemListParameters actual = new ItemListParameters();
            galleryClientMock.Setup(f => f.Items.ListAsync(It.IsAny<ItemListParameters>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ItemListResult
                {
                    Items = new List<GalleryItem>()
                    {
                        new GalleryItem()
                        {
                            Name = "Template1",
                            Publisher = "Microsoft"
                        },
                        new GalleryItem()
                        {
                            Name = "Template2",
                            Publisher = "Microsoft"
                        }
                    }
                }))
                .Callback((ItemListParameters p, CancellationToken c) => actual = p);

            FilterGalleryTemplatesOptions options = new FilterGalleryTemplatesOptions()
            {
                Publisher = "Microsoft",
                Category = "awesome"
            };

            List<PSGalleryItem> result = galleryTemplatesClient.FilterGalleryTemplates(options);

            Assert.Equal(2, result.Count);
            Assert.Equal(filterString, actual.Filter);
        }
        public void FiltersGalleryTemplates()
        {
            string filterString = FilterString.Generate<ItemListFilter>(f => f.Publisher == "Microsoft");
            ItemListParameters actual = new ItemListParameters();
            galleryClientMock.Setup(f => f.Items.ListAsync(It.IsAny<ItemListParameters>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ItemListResult
                {
                    Items = new List<GalleryItem>()
                    {
                        new GalleryItem()
                        {
                            Name = "Template1",
                            Publisher = "Microsoft"
                        },
                        new GalleryItem()
                        {
                            Name = "Template2",
                            Publisher = "Microsoft"
                        }
                    }
                }))
                .Callback((ItemListParameters p, CancellationToken c) => actual = p);

            FilterGalleryTemplatesOptions options = new FilterGalleryTemplatesOptions()
            {
                Publisher = "Microsoft"
            };

            List<PSGalleryItem> result = galleryTemplatesClient.FilterGalleryTemplates(options);

            Assert.Equal(2, result.Count);
            Assert.True(result.All(g => g.Publisher == "Microsoft"));
            Assert.Equal(filterString, actual.Filter);
        }