Example #1
0
        public async Task <HttpResponseMessage> GetAncestors(int id,
                                                             [ModelBinder(typeof(PagedQueryModelBinder))]
                                                             PagedRequest query)
        {
            if (!await AuthorizationService.AuthorizeAsync(ClaimsPrincipal, new ContentResourceAccess(id), AuthorizationPolicies.ContentRead))
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized));
            }

            var items  = Services.ContentService.GetAncestors(id).ToArray();
            var total  = items.Length;
            var pages  = (total + query.PageSize - 1) / query.PageSize;
            var paged  = items.Skip(ContentControllerHelper.GetSkipSize(query.Page - 1, query.PageSize)).Take(query.PageSize);
            var mapped = Mapper.Map <IEnumerable <ContentRepresentation> >(paged).ToList();

            // this seems stupid since we usually end up in here by request via guid from the other overload...
            var key = Services.EntityService.GetKeyForId(id, UmbracoObjectTypes.Document);

            if (key.Result == Guid.Empty)
            {
                Request.CreateResponse(HttpStatusCode.NotFound);
            }

            var result = new ContentPagedListRepresentation(mapped, total, pages, query.Page, query.PageSize, LinkTemplates.Content.PagedAncestors, new { id = key.Result });

            FilterAllowedOutgoingContent(result);

            return(Request.CreateResponse(HttpStatusCode.OK, result));
        }
Example #2
0
        public async Task <HttpResponseMessage> Search(
            [ModelBinder(typeof(PagedQueryModelBinder))]
            PagedQuery query)
        {
            if (!await AuthorizationService.AuthorizeAsync(ClaimsPrincipal, ContentResourceAccess.Empty(), AuthorizationPolicies.ContentRead))
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized));
            }

            //TODO: Authorize this! how? Same as core, i guess we just filter the results

            if (query.Query.IsNullOrWhiteSpace())
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            //Query prepping - ensure that we only search for content items...
            var contentQuery = "__IndexType:content AND " + query.Query;

            //search
            var result = SearchProvider.Search(
                SearchProvider.CreateSearchCriteria().RawQuery(contentQuery),
                ContentControllerHelper.GetMaxResults(query.Page, query.PageSize));

            //paging
            var paged = result.Skip(ContentControllerHelper.GetSkipSize(query.Page - 1, query.PageSize)).ToArray();
            var pages = (result.TotalItemCount + query.PageSize - 1) / query.PageSize;

            var foundContent = Enumerable.Empty <IContent>();

            //Map to Imedia
            if (paged.Any())
            {
                foundContent = Services.ContentService.GetByIds(paged.Select(x => x.Id)).WhereNotNull();
            }

            //Map to representation
            var items = Mapper.Map <IEnumerable <ContentRepresentation> >(foundContent).ToList();

            //return as paged list of media items
            var representation = new ContentPagedListRepresentation(items, result.TotalItemCount, pages, query.Page, query.PageSize, LinkTemplates.Content.Search, new { query = query.Query, pageSize = query.PageSize });

            //TODO: Enable this
            //FilterAllowedOutgoingContent(result);

            return(Request.CreateResponse(HttpStatusCode.OK, representation));
        }
        public async Task <HttpResponseMessage> GetDescendants(int id,
                                                               [ModelBinder(typeof(PagedQueryModelBinder))]
                                                               PagedQuery query)
        {
            if (!await AuthorizationService.AuthorizeAsync(ClaimsPrincipal, new ContentResourceAccess(id), AuthorizationPolicies.ContentRead))
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized));
            }

            var items  = Services.ContentService.GetPagedDescendants(id, query.Page - 1, query.PageSize, out var total, filter: query.Query);
            var pages  = ContentControllerHelper.GetTotalPages(total, query.PageSize);
            var mapped = Mapper.Map <IEnumerable <ContentRepresentation> >(items).ToList();

            var result = new ContentPagedListRepresentation(mapped, total, pages, query.Page - 1, query.PageSize, LinkTemplates.Content.PagedDescendants, new { id = id });

            FilterAllowedOutgoingContent(result);

            return(Request.CreateResponse(HttpStatusCode.OK, result));
        }
        public async Task <HttpResponseMessage> GetAncestors(int id,
                                                             [ModelBinder(typeof(PagedQueryModelBinder))]
                                                             PagedRequest query)
        {
            if (!await AuthorizationService.AuthorizeAsync(ClaimsPrincipal, new ContentResourceAccess(id), AuthorizationPolicies.ContentRead))
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized));
            }

            var items  = Services.ContentService.GetAncestors(id).ToArray();
            var total  = items.Length;
            var pages  = (total + query.PageSize - 1) / query.PageSize;
            var paged  = items.Skip(ContentControllerHelper.GetSkipSize(query.Page - 1, query.PageSize)).Take(query.PageSize);
            var mapped = Mapper.Map <IEnumerable <ContentRepresentation> >(paged).ToList();

            var result = new ContentPagedListRepresentation(mapped, total, pages, query.Page - 1, query.PageSize, LinkTemplates.Content.PagedAncestors, new { id = id });

            FilterAllowedOutgoingContent(result);

            return(Request.CreateResponse(HttpStatusCode.OK, result));
        }