Esempio n. 1
0
        public PFOCollection <CommentaryDto> GetCommentaries(long postId, int page, int itemsPerPage, long?commentaryId = null)
        {
            var result = new PFOCollection <CommentaryDto>();

            var commentaries = _dbContext.Commentaries.Where(x => x.Post.PostId == postId).OrderByDescending(x => x.CreationDate).AsQueryable();

            if (!commentaries.Any())
            {
                throw new CommentaryException(CommentaryError.DoesNotExist);
            }

            if (!commentaryId.HasValue)
            {
                commentaries = commentaries.Where(x => x.ResponseTo == null);
            }
            else
            {
                commentaries = commentaries.Where(x => x.ResponseTo.CommentaryId == commentaryId);
            }

            #region pagination
            result.ItemsPerPage = itemsPerPage > 10 ? itemsPerPage : 50;
            result.TotalPages   = (commentaries.Count() / result.ItemsPerPage) + 1;
            result.Page         = (page > result.TotalPages || page < 1) ? 1 : result.TotalPages;
            var itemsToSkip = (result.Page - 1) * result.ItemsPerPage;
            var itemsToTake = (commentaries.Count() - itemsToSkip < result.ItemsPerPage) ? commentaries.Count() - itemsToSkip : result.ItemsPerPage;
            result.Items = (itemsToSkip > 0 ? commentaries.Skip(itemsToSkip) : commentaries).Take(itemsToTake).Select(x =>
                                                                                                                      new CommentaryDto
            {
                CommentaryId   = x.CommentaryId,
                Content        = x.Content,
                CreationDate   = x.CreationDate.ToUniversalTime(),
                Author         = x.Author.Alias,
                AuthorId       = x.Author.UserId,
                HasAnyResponse = _dbContext.Commentaries.Any(y => y.Post.PostId == postId && y.ResponseTo.CommentaryId == x.CommentaryId),
                ProfilePicture = x.Author.ProfilePicture.HasValue ? $"https://gunetbergstorage.blob.core.windows.net/profilepictures/{x.Author.ProfilePicture}.png" : "https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png"
            }).ToList();
            #endregion

            return(result);
        }
Esempio n. 2
0
        protected override async Task OnInitializedAsync()
        {
            Posts = await _postService.GetPostList();

            FeaturedPost = await _postService.GetFeaturedPost();
        }
Esempio n. 3
0
        public PFOCollection <PostDto> GetPosts(string title, DateTime?from, DateTime?to, string orderBy, bool orderByDescending, int page, int?itemsPerPage)
        {
            var result = new PFOCollection <PostDto>();

            result.FilteredBy = new List <string>();

            var posts = _dbContext.Posts.AsQueryable();

            #region filter
            if (!string.IsNullOrWhiteSpace(title))
            {
                posts = posts.Where(x => x.Title.Contains(title));
                result.FilteredBy.Add("title");
            }
            if (from.HasValue)
            {
                posts = posts.Where(x => x.CreationDate >= from.Value);
                result.FilteredBy.Add("from");
            }
            if (to.HasValue)
            {
                posts = posts.Where(x => x.CreationDate <= from.Value);
                result.FilteredBy.Add("to");
            }

            #endregion

            #region order
            switch (orderBy)
            {
            case "Title":
                posts = orderByDescending ? posts.OrderByDescending(x => x.Title) : posts.OrderBy(x => x.Title);
                break;

            case "CreationDate":
                posts = orderByDescending ? posts.OrderByDescending(x => x.CreationDate) : posts.OrderBy(x => x.CreationDate);
                break;

            default:
                posts = orderByDescending ? posts.OrderByDescending(x => x.CreationDate) : posts.OrderBy(x => x.CreationDate);
                break;
            }

            #endregion

            #region pagination
            result.ItemsPerPage = itemsPerPage ?? 10;
            result.TotalPages   = (int)Math.Ceiling(posts.Count() / (decimal)result.ItemsPerPage);
            result.Page         = (page > result.TotalPages || page < 1) ? 1 : result.TotalPages;
            var itemsToSkip = (result.Page - 1) * result.ItemsPerPage;
            var itemsToTake = (posts.Count() - itemsToSkip < result.ItemsPerPage) ? posts.Count() - itemsToSkip : result.ItemsPerPage;
            result.Items = (itemsToSkip > 0 ? posts.Skip(itemsToSkip) : posts).Take(itemsToTake).Select(x =>
                                                                                                        new PostDto
            {
                PostId       = x.PostId,
                Title        = x.Title,
                HeaderImage  = x.HeaderImage,
                CreationDate = x.CreationDate.ToUniversalTime(),
                Author       = new AuthorDto
                {
                    Alias = x.Author.Alias
                },
                Description = x.Sections.Where(x => x.Type == SectionType.Markdown).Select(y => y.Content.Substring(0, 150)).FirstOrDefault() + "..."
            }).ToList();
            #endregion

            return(result);
        }