public async Task<ActionResult> Archive() { List<BlogPostViewModel> model = new List<BlogPostViewModel>(); using (var context = _contextFactory()) { // Show up to the last five posts with the latest post first var posts = await context.Posts .OrderByDescending((p) => p.PublishedAt) .Take(5) .ToListAsync(); foreach (BlogPost entity in posts) { var item = new BlogPostViewModel() { Body = entity.Body, Id = entity.Id, Preview = entity.Preview, PublishedAt = entity.PublishedAt, Title = entity.Title, }; model.Add(item); } } return View(model); }
public async Task<ActionResult> Latest() { BlogPostViewModel model; using (var context = _contextFactory()) { // Get the latest post BlogPost latestPost = await context.Posts .OrderByDescending((p) => p.PublishedAt) .FirstOrDefaultAsync(); if (latestPost == null) { return HttpNotFound(); } model = new BlogPostViewModel() { Body = latestPost.Body, Id = latestPost.Id, Preview = latestPost.Preview, PublishedAt = latestPost.PublishedAt, Title = latestPost.Title, }; } return View("Post", model); }
public async Task<ActionResult> Post(int id) { BlogPostViewModel model; using (var context = _contextFactory()) { // Get the first post with the specified Id BlogPost post = await context.Posts .Where((p) => p.Id == id) .FirstOrDefaultAsync(); if (post == null) { return HttpNotFound(); } model = new BlogPostViewModel() { Body = post.Body, Id = post.Id, Preview = post.Preview, PublishedAt = post.PublishedAt, Title = post.Title, }; } return View(model); }