private async Task <object> GetWishlist(dynamic o)
        {
            var searchTemplate = this.templateFactory.CreateIriTemplate <WishlistFilter, Collection <WishlistItem> >();
            var uriTemplate    = new UriTemplate.Core.UriTemplate(searchTemplate.Template);

            var templateParams = new Dictionary <string, object>((DynamicDictionary)this.Context.Request.Query);

            var collectionId = uriTemplate.BindByName(templateParams);
            var canonical    = uriTemplate.BindByName(templateParams).ToString();
            var filter       = this.Bind <WishlistFilter>();

            WishlistItem[] wishlistItems = new WishlistItem[0];
            if (this.Context.CurrentUser.HasPermission(Permissions.WriteSources))
            {
                LogTo.Debug("Getting all unique wishlist items");
                wishlistItems = await this.wishlistRepository.FindAdminWishlist(filter.ShowAll.GetValueOrDefault(false));
            }
            else if (this.Context.CurrentUser.IsAuthenticated())
            {
                var user = this.Context.CurrentUser.GetNameClaim();
                LogTo.Debug("Getting items for user {0}", user);
                wishlistItems = await this.wishlistRepository.FindUsersWishlist(user, filter.ShowAll.GetValueOrDefault(true));
            }

            var collection = new Wishlist(searchTemplate)
            {
                Id              = collectionId,
                Members         = wishlistItems,
                TotalItems      = wishlistItems.Length,
                CurrentMappings = templateParams.ToTemplateMappings(searchTemplate),
            };

            return(this.Negotiate.WithModel(collection).WithHeader("Link", $"<{canonical}>; rel=\"canonical\""));
        }
Example #2
0
        private string PrepareUri(IDictionary <string, string> sendPairs, IHypertextControl hypertextControl)
        {
            var pairs = new Dictionary <string, string>(sendPairs);

            var uriTemplate = new UriTemplate.Core.UriTemplate(hypertextControl.HRef);
            var uri         = uriTemplate.BindByName(pairs);
            var bindings    = uriTemplate.Match(uri, pairs.Keys).Bindings;

            foreach (string bound in bindings.Keys)
            {
                pairs.Remove(bound);
            }

            if (pairs.Count > 0)
            {
                string queryString  = String.Join("&", pairs.Select(v => $"{v.Key}={v.Value}"));
                string questionMark = uri.OriginalString.Contains("?") ? String.Empty : "?";

                return(Flurl.Url.Combine(uri.OriginalString, questionMark, queryString));
            }
            else
            {
                return(uri.OriginalString);
            }
        }
Example #3
0
        private async Task <dynamic> GetPage <T, TFilter>(int?page, Func <Uri, TFilter, int, int, Task <SearchableCollection <T> > > getPage)
            where T : class
            where TFilter : ITemplateParameters <Collection <T> >
        {
            if (page == null)
            {
                page = 1;
            }

            if (page < 0)
            {
                return(400);
            }

            var searchTemplate = this.templateFactory.CreateIriTemplate <TFilter, Collection <T> >();
            var uriTemplate    = new UriTemplate.Core.UriTemplate(searchTemplate.Template);

            var templateParams = new Dictionary <string, object>((DynamicDictionary)this.Context.Request.Query);

            templateParams.Remove("page");
            var collectionId = uriTemplate.BindByName(templateParams);

            templateParams["page"] = page;
            var canonical = uriTemplate.BindByName(templateParams).ToString();

            var filter     = this.Bind <TFilter>();
            var collection = await getPage(collectionId, filter, page.Value, PageSize);

            collection.Title = this.supportedClass.GetMeta(typeof(T)).Description + " collection";

            collection.Views = new IView[]
            {
                new TemplatedPartialCollectionView(uriTemplate, "page", collection.TotalItems, page.Value, PageSize, templateParams),
            };

            collection.Search          = searchTemplate;
            collection.CurrentMappings = templateParams.ToTemplateMappings(searchTemplate);

            return(this.Negotiate.WithModel(collection).WithHeader("Link", $"<{canonical}>; rel=\"canonical\""));
        }