Example #1
0
        private void SetPost(Post post)
        {
            if (post == null)
            {
                return;
            }

            post.Url = cacheManager.Get(CacheConstant.POST_URL_ITEM.FormatInvariant(post.Id), () =>
            {
                return(urlService.GetUrl(post.Id, nameof(Post)));
            });
            post.Picture = cacheManager.Get(CacheConstant.MEDIA_STORAGE_ITEM.FormatInvariant(post.PictureId), () =>
            {
                return(mediaStorageService.GetById(post.PictureId));
            });
            post.Category = cacheManager.Get(CacheConstant.CATEGORY_ITEM.FormatInvariant(post.CategoryId), () =>
            {
                return(categoryService.GetById(post.CategoryId));
            });
            post.PostTags = cacheManager.Get(CacheConstant.POST_TAG_ITEM.FormatInvariant(post.Id), () =>
            {
                return(tagService.GetTagsByPostId(post.Id).ToList());
            });
            post.User = cacheManager.Get(CacheConstant.USER_ID.FormatInvariant(post.UserId), () =>
            {
                return(userService.GetById(post.UserId));
            });

            if (post.User != null)
            {
                post.UserName = post.User.UserName;
            }

            post.Comments = new List <Comment>();
        }
Example #2
0
        public ActionResult Edit(int?id)
        {
            if (!id.HasValue)
            {
                return(RedirectToAction("List"));
            }

            var post = postService.GetById(id.Value);

            if (post == null)
            {
                this.NotifyError("Item not found.");
                return(RedirectToAction("List"));
            }

            var model = new PostModel
            {
                Id                 = post.Id,
                Title              = post.Title,
                CategoryId         = post.CategoryId,
                AllowComment       = post.AllowComment,
                CreateDate         = post.CreateDateUtc,
                FullContent        = post.Description,
                ViewCount          = post.ViewCount,
                ViewName           = post.ViewName,
                IsDelete           = post.IsDelete,
                UpdateDate         = post.UpdateDateUtc,
                IsActive           = post.IsActive,
                ApproveComment     = post.ApproveComment,
                PostFormat         = GetPostFormat(post.Format),
                PostFormatValue    = post.FormatValue,
                PictureId          = post.PictureId,
                Categories         = categoryService.GetActives(),
                Url                = urlService.GetUrl(post.Id, nameof(Post)),
                Tags               = postTagService.GetTagsByPostId(post.Id).Select(s => s.Name).ToArray(),
                AvailableViewNames = this.GetViewNames()
            };

            model.AvailableTags = new MultiSelectList(postTagService.GetAllTags().Select(s => s.Name), model.Tags);

            return(View(model));
        }
Example #3
0
        public ActionResult Post(int?Id)
        {
            if (!Id.HasValue)
            {
                return(HomePage());
            }

            var post = postService.GetById(Id.Value);

            if (post == null)
            {
                return(NotFound());
            }

            if (!post.IsActive || post.IsDelete)
            {
                return(NotActive());
            }

            string cookieKey = string.Format(CookieConstant.POST_VIEW, post.Id);

            if (!CookieHelper.Exists(cookieKey))
            {
                CookieHelper.Set(cookieKey, WebHelper.IpAddress, 1);
                post.ViewCount += 1;
                postService.Update(post);
            }

            var model = new PostDetailModel
            {
                Id              = post.Id,
                Title           = post.Title,
                Content         = post.Description,
                PicturePath     = post.Picture.FilePath,
                Url             = urlService.GetUrl(post.Id, nameof(Post)),
                User            = post.User,
                CreateDate      = post.CreateDateUtc,
                UpdateDate      = post.UpdateDateUtc,
                ViewCount       = post.ViewCount,
                CategoryName    = post.Category.Name,
                CategoryUrl     = post.Category.Url,
                AllowComment    = post.AllowComment,
                PostFormat      = post.Format,
                PostFormatValue = post.FormatValue,
                ApproveComment  = post.ApproveComment,
                CommentCount    = postService.GetCommentCount(post.Id, true),
                Tags            = tagService.GetTagsByPostId(postId: post.Id).Select(x => new TagModel
                {
                    Name = x.Name
                }).ToList()
            };

            model.User.SocialNetworks = post.User.SocialNetworks;

            if (settingService.GetByName("post.related.view").BoolValue)
            {
                model.RelatedPosts = postService.GetRelatedPosts(post.CategoryId, post.Id).Select(x => new PostModel
                {
                    Id           = x.Id,
                    Title        = x.Title,
                    PicturePath  = mediaStorageService.GetPictureUrl(x.PictureId),
                    CreateDate   = x.CreateDateUtc,
                    CategoryName = categoryService.GetById(x.CategoryId).Name,
                    User         = userService.FindByIdAsync(x.UserId).Result,
                    CommentCount = 0,
                    ViewCount    = x.ViewCount,
                    Url          = urlService.GetUrl(x.Id, nameof(Post))
                }).ToList();
            }

            if (settingService.GetByName("post.comment.enabled").BoolValue)
            {
                model.Comments = postService.GetComments(post.Id, true).Select(x => new CommentListModel
                {
                    Id            = x.Id,
                    ParentId      = x.ParentId,
                    FullName      = x.FullName,
                    Comment       = x.Commentary,
                    UserId        = x.UserId,
                    UserAvatarUrl = userService.GetById(x.UserId).Avatar.FilePath,
                    Approved      = x.Approved,
                    CreateDate    = x.CommentDateUtc.ToRelativeFormat(),
                    ChildComments = postService.GetChildComments(post.Id, x.Id, approved: true).Select(c => new CommentListModel
                    {
                        Id            = c.Id,
                        ParentId      = c.ParentId,
                        FullName      = c.FullName,
                        Comment       = c.Commentary,
                        UserId        = c.UserId,
                        UserAvatarUrl = userService.GetById(x.UserId).Avatar.FilePath,
                        Approved      = c.Approved,
                        CreateDate    = c.CommentDateUtc.ToRelativeFormat(),
                    }).ToList()
                }).ToList();
            }

            return(View(this.GetViewName(post), model));
        }