-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlogController.cs
More file actions
165 lines (133 loc) · 4.64 KB
/
Copy pathBlogController.cs
File metadata and controls
165 lines (133 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Linq;
using System.Web.Mvc;
using BlogEngine.Core.Work;
using BlogEngine.Core.ViewModels;
using BlogEngine.Web.Helpers;
using WebMatrix.WebData;
using PagedList;
using System.Collections.Generic;
using System.Web;
using System.Data.Objects.SqlClient;
namespace BlogEngine.Web.Controllers
{
public class BlogController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public BlogController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
//
// GET: /Blog/
public ActionResult Index(int? page)
{
var blogs = _unitOfWork.BlogRepository.GetAllView();
int pageSize = 5;
int pageNumber = (page ?? 1);
return View(blogs.ToPagedList(pageNumber, pageSize));
}
//
// GET: /Blog/Comment/1
public ActionResult Comment(int id, int? parent)
{
// if parent not null, create new comment with parentid == parent
var model = new CommentViewModel
{
UserId = WebSecurity.CurrentUserId,
BlogId = id,
ParentId = parent
};
return View(model);
}
//
// POST: /Blog/Comment/1
[HttpPost]
public ActionResult Comment(CommentViewModel comment)
{
if (ModelState.IsValid)
{
comment.CommentDate = DateTime.Now;
_unitOfWork.CommentRepository.Create(comment);
_unitOfWork.Save();
return RedirectToAction("Index");
}
return View(comment);
}
//
// GET: /Blog/BlogEntry/1/{blogtitle}
[HttpGet]
public ActionResult BlogEntry(int id, string blogEntryName)
{
var model = _unitOfWork.BlogRepository.GetBlogById(id);
ViewBag.totalComments = model.Comments.Count();
// just get the base comments (where there is no ParentId, and then iterate through them in the view
var comments = model.Comments.Where(c => c.ParentId == null).ToList();
model.Comments = comments;
string realTitle = UrlEncoder.ToFriendlyUrl(model.BlogTitle);
string urlTitle = (blogEntryName ?? "").Trim().ToLower();
if (realTitle != urlTitle)
{
string url = "/BlogEntry/" + model.BlogEntryId + "/" + realTitle;
return new RedirectResult(url);
}
return View(model);
}
[HttpGet]
public ActionResult GetTopBlogs()
{
var blogs = _unitOfWork.BlogRepository.GetTopBlogs(5);
ICollection<BlogSummaryView> blogSummaryList = ModelBinder.BlogSummary(blogs);
return PartialView("_getTopBlogs", blogSummaryList);
}
//
// GET: /category/{category}
public ActionResult Category(string category)
{
var blogs = _unitOfWork.BlogRepository.GetBlogsByCategory(category).ToList();
List<BlogEntryView> blogsView = ModelBinder.BlogEntryView(blogs).ToList();
if (blogsView.Count < 1)
{
throw new HttpException(404, "Category not found");
}
return View(blogsView);
}
//
// GET: /Tag/{tag}
public ActionResult Tag(string tag)
{
var blogs = _unitOfWork.BlogRepository.GetBlogsByTag(tag).ToList();
List<BlogEntryView> blogsView = ModelBinder.BlogEntryView(blogs).ToList();
if (blogsView.Count < 1)
{
throw new HttpException(404, "Category not found");
}
return View(blogsView);
}
public ActionResult GetCategories()
{
var categories = _unitOfWork.CategoryRepository.GetCategories();
List<CategoryViewModel> categoryViewModel = ModelBinder.Categories(categories);
return PartialView("_getCategories", categoryViewModel);
}
public ActionResult GetTags()
{
List<TagCheckViewModel> tags = _unitOfWork.TagRepository.GetAllTagsForVM();
return PartialView("_getTags", tags);
}
public ActionResult Search(string search)
{
var model = _unitOfWork.BlogRepository.Search(search);
// reset the search form
ModelState.Clear();
if (model.Count > 0)
{
return View(model);
}
else
{
return View("NoResults");
}
}
}
}