public async Task <ActionResult> Blog(int userId, int id) { BlogModel blog = this.Service.GetBlogDetail(id); if (blog == null || blog.AuthorId != userId) { return(this.NotFoundView()); } bool isAuthor = this.CurrentSession != null && this.CurrentSession.UserId == blog.AuthorId; if (blog.Status == BlogStatus.Unpublished && !isAuthor) { return(this.NotFoundView()); } //增加阅读数 if (this.CurrentSession != null) { await _mediator.Publish(new ReadBlogEvent() { BlogId = blog.Id, ReaderId = this.CurrentSession.UserId.Value, ReadTime = DateTime.Now }); } BlogNeighbour blogNeighbour = this.Service.GetNeighbours(blog.Id, blog.AuthorId); this.ViewBag.Blog = blog; this.ViewBag.BlogNeighbour = blogNeighbour; return(this.View()); }
public BlogNeighbour GetNeighbours(int id, int authorId) { BlogNeighbour ret = new BlogNeighbour(); var q = this.Query().Where(a => a.AuthorId == authorId && a.Status == BlogStatus.Published); ret.Prev = BlogModel.Create(q.Where(a => a.Id < id).OrderByDesc(a => a.Id).FirstOrDefault()); ret.Next = BlogModel.Create(q.Where(a => a.Id > id).OrderBy(a => a.Id).FirstOrDefault()); return(ret); }