public Task <HttpResponseMessage> Search(
            [ModelBinder(typeof(PagedQueryModelBinder))]
            PagedQuery query)
        {
            if (query.Query.IsNullOrWhiteSpace())
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }


            //search
            var result = SearchProvider.Search(
                SearchProvider.CreateSearchCriteria().RawQuery(query.Query),
                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 <IMedia>();

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

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

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

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

            var items  = Services.MediaService.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 <MediaRepresentation> >(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.Media);

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

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

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

            var items  = Services.MediaService.GetPagedDescendants(id, query.Page - 1, query.PageSize, out var total, filter: query.Query);
            var pages  = (total + query.PageSize - 1) / query.PageSize;
            var mapped = Mapper.Map <IEnumerable <MediaRepresentation> >(items).ToList();

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

            return(Request.CreateResponse(HttpStatusCode.OK, result));
        }
Beispiel #4
0
        public async Task <HttpResponseMessage> Search(
            [ModelBinder(typeof(PagedQueryModelBinder))]
            PagedQuery query)
        {
            if (!await AuthorizationService.AuthorizeAsync(ClaimsPrincipal, ContentResourceAccess.Empty(), AuthorizationPolicies.MediaRead))
            {
                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 media items...
            var mediaQuery = "__IndexType:media AND " + query.Query;

            //search
            var result = SearchProvider.Search(
                SearchProvider.CreateSearchCriteria().RawQuery(mediaQuery),
                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 <IMedia>();

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

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

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

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

            var items  = Services.MediaService.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 <MediaRepresentation> >(paged).ToList();

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

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