Esempio n. 1
0
        public GetPagesResponse Get(GetPagesRequest request)
        {
            request.Data.SetDefaultOrder("Title");

            var query = repository
                .AsQueryable<PageProperties>();

            if (!request.Data.IncludeArchived)
            {
                query = query.Where(b => !b.IsArchived);
            }

            if (!request.Data.IncludeUnpublished)
            {
                query = query.Where(b => b.Status == PageStatus.Published);
            }

            if (!request.Data.IncludeMasterPages)
            {
                query = query.Where(b => !b.IsMasterPage);
            }

            query = query.ApplyPageTagsFilter(request.Data);

            if (request.User != null && !string.IsNullOrWhiteSpace(request.User.Name))
            {
                var principal = new ApiPrincipal(request.User);
                IEnumerable<Guid> deniedPages = accessControlService.GetPrincipalDeniedObjects<PageProperties>(principal, false);
                foreach (var deniedPageId in deniedPages)
                {
                    var id = deniedPageId;
                    query = query.Where(f => f.Id != id);
                }
            }

            var includeMetadata = request.Data.IncludeMetadata;
            var listResponse = query
                .Select(page => new PageModel
                    {
                        Id = page.Id,
                        Version = page.Version,
                        CreatedBy = page.CreatedByUser,
                        CreatedOn = page.CreatedOn,
                        LastModifiedBy = page.ModifiedByUser,
                        LastModifiedOn = page.ModifiedOn,

                        PageUrl = page.PageUrl,
                        Title = page.Title,
                        Description = page.Description,
                        IsPublished = page.Status == PageStatus.Published,
                        PublishedOn = page.PublishedOn,
                        LayoutId = page.Layout != null && !page.Layout.IsDeleted ? page.Layout.Id : (Guid?)null,
                        MasterPageId = page.MasterPage != null && !page.MasterPage.IsDeleted ? page.MasterPage.Id : (Guid?)null,
                        CategoryId = page.Category != null && !page.Category.IsDeleted ? page.Category.Id : (Guid?)null,
                        CategoryName = page.Category != null && !page.Category.IsDeleted ? page.Category.Name : null,
                        MainImageId = page.Image != null && !page.Image.IsDeleted ? page.Image.Id : (Guid?)null,
                        MainImageUrl = page.Image != null && !page.Image.IsDeleted ? page.Image.PublicUrl : null,
                        MainImageThumbnauilUrl = page.Image != null && !page.Image.IsDeleted ? page.Image.PublicThumbnailUrl : null,
                        MainImageCaption = page.Image != null && !page.Image.IsDeleted ? page.Image.Caption : null,
                        IsArchived = page.IsArchived,
                        IsMasterPage = page.IsMasterPage,
                        LanguageId = page.Language != null ? page.Language.Id : (Guid?)null,
                        LanguageCode = page.Language != null ? page.Language.Code : null,
                        LanguageGroupIdentifier = page.LanguageGroupIdentifier,
                        Metadata = includeMetadata 
                            ? new MetadataModel
                                  {
                                      MetaDescription = page.MetaDescription,
                                      MetaTitle = page.MetaTitle,
                                      MetaKeywords = page.MetaKeywords,
                                      UseNoFollow = page.UseNoFollow,
                                      UseNoIndex = page.UseNoIndex,
                                      UseCanonicalUrl = page.UseCanonicalUrl
                                  } : null
                    }).ToDataListResponse(request);

            foreach (var model in listResponse.Items)
            {
                model.MainImageUrl = fileUrlResolver.EnsureFullPathUrl(model.MainImageUrl);
                model.MainImageThumbnauilUrl = fileUrlResolver.EnsureFullPathUrl(model.MainImageThumbnauilUrl);
            }

            if (listResponse.Items.Count > 0
                && (request.Data.IncludePageOptions || request.Data.IncludeTags || request.Data.IncludeAccessRules))
            {
                LoadInnerCollections(listResponse, request.Data.IncludePageOptions, request.Data.IncludeTags, request.Data.IncludeAccessRules);
            }

            return new GetPagesResponse
            {
                Data = listResponse
            };
        }
Esempio n. 2
0
        /// <summary>
        /// Gets files list.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <c>GetFilesResponse</c> with files list.
        /// </returns>
        public GetFilesResponse Get(GetFilesRequest request)
        {
            request.Data.SetDefaultOrder("Title");

            var query =
                repository.AsQueryable<Media>()
                    .Where(m => m.Original == null && m.Type == Module.MediaManager.Models.MediaType.File)
                    .Where(f => !(f is MediaFile) || (!((MediaFile)f).IsTemporary && ((MediaFile)f).IsUploaded == true));

            if (request.Data.FolderId == null)
            {
                query = query.Where(m => m.Folder == null);
            }
            else
            {
                query = query.Where(m => m.Folder.Id == request.Data.FolderId && !m.Folder.IsDeleted);
            }

            if (!request.Data.IncludeFolders)
            {
                query = query.Where(media => media.ContentType != Module.MediaManager.Models.MediaContentType.Folder);
            }

            if (!request.Data.IncludeFiles)
            {
                query = query.Where(media => media.ContentType != Module.MediaManager.Models.MediaContentType.File);
            }

            if (!request.Data.IncludeArchived)
            {
                query = query.Where(m => !m.IsArchived);
            }

            query = query.ApplyMediaTagsFilter(request.Data)
                         .ApplyCategoriesFilter(categoryService, request.Data);

            if (request.User != null && !string.IsNullOrWhiteSpace(request.User.Name))
            {
                var principal = new ApiPrincipal(request.User);
                IEnumerable<Guid> deniedPages = accessControlService.GetPrincipalDeniedObjects<MediaFile>(principal, false);
                foreach (var deniedPageId in deniedPages)
                {
                    var id = deniedPageId;
                    query = query.Where(f => f.Id != id);
                }
            }

            var listResponse =
                query.Select(
                    media =>
                    new MediaModel
                        {
                            Id = media.Id,
                            Version = media.Version,
                            CreatedBy = media.CreatedByUser,
                            CreatedOn = media.CreatedOn,
                            LastModifiedBy = media.ModifiedByUser,
                            LastModifiedOn = media.ModifiedOn,
                            Title = media.Title,
                            Description = media.Description,
                            MediaContentType =
                                media is MediaFolder ? (MediaContentType)((int)MediaContentType.Folder) : (MediaContentType)((int)MediaContentType.File),
                            FileExtension = media is MediaFile ? ((MediaFile)media).OriginalFileExtension : null,
                            FileSize = media is MediaFile ? ((MediaFile)media).Size : (long?)null,
                            FileUrl = media is MediaFile ? ((MediaFile)media).PublicUrl : null,
                            IsArchived = media.IsArchived,
                            ThumbnailId = media.Image != null && !media.Image.IsDeleted ? media.Image.Id : (Guid?)null,
                            ThumbnailCaption = media.Image != null && !media.Image.IsDeleted ? media.Image.Caption : null,
                            ThumbnailUrl = media.Image != null && !media.Image.IsDeleted ? media.Image.PublicThumbnailUrl : null
                        }).ToDataListResponse(request);

            var ids = new List<Guid>();

            listResponse.Items.ToList().ForEach(
                media =>
                    {
                        if (media.MediaContentType == MediaContentType.File)
                        {
                            media.FileUrl = this.mediaFileService.GetDownloadFileUrl(Module.MediaManager.Models.MediaType.File, media.Id, media.FileUrl);
                            ids.Add(media.Id);
                        }
                        media.FileUrl = fileUrlResolver.EnsureFullPathUrl(media.FileUrl);
                        media.ThumbnailUrl = fileUrlResolver.EnsureFullPathUrl(media.ThumbnailUrl);
                    });

            if (request.Data.IncludeAccessRules)
            {
                (from file in repository.AsQueryable<MediaFile>()
                 from accessRule in file.AccessRules
                 where ids.Contains(file.Id)
                 orderby accessRule.IsForRole, accessRule.Identity
                 select
                     new AccessRuleModelEx
                         {
                             AccessRule =
                                 new AccessRuleModel
                                     {
                                         AccessLevel = (AccessLevel)(int)accessRule.AccessLevel,
                                         Identity = accessRule.Identity,
                                         IsForRole = accessRule.IsForRole
                                     },
                             FileId = file.Id
                         }).ToList()
                    .ForEach(
                        rule => listResponse.Items.Where(file => file.Id == rule.FileId).ToList().ForEach(
                            file =>
                                {
                                    if (file.AccessRules == null)
                                    {
                                        file.AccessRules = new List<AccessRuleModel>();
                                    }
                                    file.AccessRules.Add(rule.AccessRule);
                                }));
            }

            if (request.Data.IncludeCategories)
            {
                listResponse.Items.ForEach(
                    item =>
                    {
                        item.Categories = (from media in repository.AsQueryable<MediaFile>()
                                           from category in media.Categories
                                           where media.Id == item.Id && !category.IsDeleted
                                           select new CategoryNodeModel
                                           {
                                               Id = category.Category.Id,
                                               Version = category.Version,
                                               CreatedBy = category.CreatedByUser,
                                               CreatedOn = category.CreatedOn,
                                               LastModifiedBy = category.ModifiedByUser,
                                               LastModifiedOn = category.ModifiedOn,
                                               Name = category.Category.Name,
                                               CategoryTreeId = category.Category.CategoryTree.Id
                                           }).ToList();
                    });
            }

            return new GetFilesResponse { Data = listResponse };
        }
        /// <summary>
        /// Gets the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns><c>GetBlogPostsResponse</c> with a list of blog posts.</returns>
        public GetBlogPostsResponse Get(GetBlogPostsRequest request)
        {
            request.Data.SetDefaultOrder("Title");

            var query = repository
                .AsQueryable<Module.Blog.Models.BlogPost>();

            if (!request.Data.IncludeArchived)
            {
                query = query.Where(b => !b.IsArchived);
            }

            if (!request.Data.IncludeUnpublished)
            {
                query = query.Where(b => b.Status == PageStatus.Published);
            }

            if (request.User != null && !string.IsNullOrWhiteSpace(request.User.Name))
            {
                var principal = new ApiPrincipal(request.User);
                IEnumerable<Guid> deniedPages = accessControlService.GetPrincipalDeniedObjects<PageProperties>(principal, false);
                foreach (var deniedPageId in deniedPages)
                {
                    var id = deniedPageId;
                    query = query.Where(f => f.Id != id);
                }
            }

            query = query.ApplyPageTagsFilter(request.Data);

            var listResponse = query
                .Select(blogPost => new BlogPostModel
                    {
                        Id = blogPost.Id,
                        Version = blogPost.Version,
                        CreatedBy = blogPost.CreatedByUser,
                        CreatedOn = blogPost.CreatedOn,
                        LastModifiedBy = blogPost.ModifiedByUser,
                        LastModifiedOn = blogPost.ModifiedOn,

                        BlogPostUrl = blogPost.PageUrl,
                        Title = blogPost.Title,
                        IntroText = blogPost.Description,
                        IsPublished = blogPost.Status == PageStatus.Published,
                        PublishedOn = blogPost.PublishedOn,
                        LayoutId = blogPost.Layout != null && !blogPost.Layout.IsDeleted ? blogPost.Layout.Id : (Guid?)null,
                        MasterPageId = blogPost.MasterPage != null && !blogPost.MasterPage.IsDeleted ? blogPost.MasterPage.Id : (Guid?)null,
                        CategoryId = blogPost.Category != null && !blogPost.Category.IsDeleted ? blogPost.Category.Id : (Guid?)null,
                        CategoryName = blogPost.Category != null && !blogPost.Category.IsDeleted ? blogPost.Category.Name : null,
                        AuthorId = blogPost.Author != null && !blogPost.Author.IsDeleted ? blogPost.Author.Id : (Guid?)null,
                        AuthorName = blogPost.Author != null && !blogPost.Author.IsDeleted ? blogPost.Author.Name : null,
                        MainImageId = blogPost.Image != null && !blogPost.Image.IsDeleted ? blogPost.Image.Id : (Guid?)null,
                        MainImageUrl = blogPost.Image != null && !blogPost.Image.IsDeleted ? blogPost.Image.PublicUrl : null,
                        MainImageThumbnauilUrl = blogPost.Image != null && !blogPost.Image.IsDeleted ? blogPost.Image.PublicThumbnailUrl : null,
                        MainImageCaption = blogPost.Image != null && !blogPost.Image.IsDeleted ? blogPost.Image.Caption : null,
                        ActivationDate = blogPost.ActivationDate,
                        ExpirationDate = blogPost.ExpirationDate,
                        IsArchived = blogPost.IsArchived
                    })
                    .ToDataListResponse(request);

            foreach (var model in listResponse.Items)
            {
                model.MainImageUrl = fileUrlResolver.EnsureFullPathUrl(model.MainImageUrl);
                model.MainImageThumbnauilUrl = fileUrlResolver.EnsureFullPathUrl(model.MainImageThumbnauilUrl);
            }

            if (listResponse.Items.Count > 0 && (request.Data.IncludeTags || request.Data.IncludeAccessRules))
            {
                LoadTags(listResponse, request.Data.IncludeTags, request.Data.IncludeAccessRules);
            }

            return new GetBlogPostsResponse
                       {
                           Data = listResponse
                       };
        }