@@ -37,73 +37,57 @@ public HomeController(IBlogProvider blogProvider,
3737 _compositeViewEngine = compositeViewEngine ;
3838 }
3939
40- public async Task < IActionResult > Index ( string term , int page = 1 )
40+ public async Task < IActionResult > Index ( int page = 1 )
4141 {
42- var model = new ListModel { PostListType = PostListType . Blog } ;
43- try
44- {
45- model . Blog = await _blogProvider . GetBlogItem ( ) ;
46- }
47- catch
48- {
49- return Redirect ( "~/admin" ) ;
50- }
5142
52- model . Pager = new Pager ( page , model . Blog . ItemsPerPage ) ;
43+ var model = await getBlogPosts ( pager : page ) ;
5344
54- if ( string . IsNullOrEmpty ( term ) )
55- {
56- if ( model . Blog . IncludeFeatured )
57- model . Posts = await _postProvider . GetList ( model . Pager , 0 , "" , "FP" ) ;
58- else
59- model . Posts = await _postProvider . GetList ( model . Pager , 0 , "" , "P" ) ;
60- }
61- else
62- {
63- model . PostListType = PostListType . Search ;
64- model . Blog . Title = term ;
65- model . Blog . Description = "" ;
66- model . Posts = await _postProvider . Search ( model . Pager , term , 0 , "FP" ) ;
67- }
45+ //If no blogs are setup redirect to first time registration
46+ if ( model == null ) {
47+ return Redirect ( "~/admin/register" ) ;
48+ }
49+
50+ return View ( $ "~/Views/Themes/{ model . Blog . Theme } /Index.cshtml", model ) ;
51+ }
52+
53+ [ HttpGet ( "/{slug}" ) ]
54+ public async Task < IActionResult > Index ( string slug )
55+ {
56+ if ( ! string . IsNullOrEmpty ( slug ) )
57+ {
58+ return await getSingleBlogPost ( slug ) ;
59+ }
60+ return Redirect ( "~/" ) ;
61+ }
6862
69- if ( model . Pager . ShowOlder ) model . Pager . LinkToOlder = $ "?page={ model . Pager . Older } ";
70- if ( model . Pager . ShowNewer ) model . Pager . LinkToNewer = $ "?page={ model . Pager . Newer } ";
63+ [ HttpGet ( "/admin" ) ]
64+ public async Task < IActionResult > Admin ( )
65+ {
66+ return File ( "~/index.html" , "text/html" ) ;
67+ }
7168
69+ [ HttpPost ]
70+ public async Task < IActionResult > Search ( string term , int page = 1 )
71+ {
72+
7273 if ( ! string . IsNullOrEmpty ( term ) )
7374 {
75+ var model = await getBlogPosts ( term , page ) ;
7476 string viewPath = $ "~/Views/Themes/{ model . Blog . Theme } /Search.cshtml";
7577 if ( IsViewExists ( viewPath ) )
7678 return View ( viewPath , model ) ;
79+ else
80+ return Redirect ( "~/home" ) ;
7781 }
82+ else {
83+ return Redirect ( "~/home" ) ;
84+ }
85+ }
7886
79- return View ( $ "~/Views/Themes/{ model . Blog . Theme } /Index.cshtml", model ) ;
80- }
81-
82- [ HttpPost ]
83- public IActionResult Search ( string term )
84- {
85- return Redirect ( $ "/home?term={ term } ") ;
86- }
87-
88- [ HttpGet ( "categories/{category}" ) ]
87+ [ HttpGet ( "categories/{category}" ) ]
8988 public async Task < IActionResult > Categories ( string category , int page = 1 )
9089 {
91- var model = new ListModel { PostListType = PostListType . Category } ;
92- try
93- {
94- model . Blog = await _blogProvider . GetBlogItem ( ) ;
95- }
96- catch
97- {
98- return Redirect ( "~/admin" ) ;
99- }
100-
101- model . Pager = new Pager ( page , model . Blog . ItemsPerPage ) ;
102- model . Posts = await _postProvider . GetList ( model . Pager , 0 , category , "PF" ) ;
103-
104- if ( model . Pager . ShowOlder ) model . Pager . LinkToOlder = $ "?page={ model . Pager . Older } ";
105- if ( model . Pager . ShowNewer ) model . Pager . LinkToNewer = $ "?page={ model . Pager . Newer } ";
106-
90+ var model = await getBlogPosts ( "" , page , category ) ;
10791 string viewPath = $ "~/Views/Themes/{ model . Blog . Theme } /Category.cshtml";
10892
10993 if ( IsViewExists ( viewPath ) )
@@ -112,41 +96,11 @@ public async Task<IActionResult> Categories(string category, int page = 1)
11296 return View ( $ "~/Views/Themes/{ model . Blog . Theme } /Index.cshtml", model ) ;
11397 }
11498
115- [ HttpGet ( "posts/{slug}" ) ]
116- public async Task < IActionResult > Single ( string slug )
117- {
118- try
119- {
120- ViewBag . Slug = slug ;
121- PostModel model = await _postProvider . GetPostModel ( slug ) ;
122-
123- // If unpublished and unauthorised redirect to error / 404.
124- if ( model . Post . Published == DateTime . MinValue && ! User . Identity . IsAuthenticated )
125- {
126- return Redirect ( "~/error" ) ;
127- }
128-
129- model . Blog = await _blogProvider . GetBlogItem ( ) ;
130- model . Post . Description = model . Post . Description . MdToHtml ( ) ;
131- model . Post . Content = model . Post . Content . MdToHtml ( ) ;
132-
133- if ( ! model . Post . Author . Avatar . StartsWith ( "data:" ) )
134- model . Post . Author . Avatar = Url . Content ( $ "~/{ model . Post . Author . Avatar } ") ;
135-
136- if ( model . Post . PostType == PostType . Page )
137- {
138- string viewPath = $ "~/Views/Themes/{ model . Blog . Theme } /Page.cshtml";
139- if ( IsViewExists ( viewPath ) )
140- return View ( viewPath , model ) ;
141- }
142-
143- return View ( $ "~/Views/Themes/{ model . Blog . Theme } /Post.cshtml", model ) ;
144- }
145- catch
146- {
147- return Redirect ( "~/error" ) ;
148- }
149- }
99+ [ HttpGet ( "posts/{slug}" ) ]
100+ public async Task < IActionResult > Single ( string slug )
101+ {
102+ return await getSingleBlogPost ( slug ) ;
103+ }
150104
151105 [ HttpGet ( "error" ) ]
152106 public async Task < IActionResult > Error ( )
@@ -226,5 +180,81 @@ private bool IsViewExists(string viewPath)
226180 var result = _compositeViewEngine . GetView ( "" , viewPath , false ) ;
227181 return result . Success ;
228182 }
183+
184+
185+ public async Task < IActionResult > getSingleBlogPost ( string slug ) {
186+ try
187+ {
188+ ViewBag . Slug = slug ;
189+ PostModel model = await _postProvider . GetPostModel ( slug ) ;
190+
191+ // If unpublished and unauthorised redirect to error / 404.
192+ if ( model . Post . Published == DateTime . MinValue && ! User . Identity . IsAuthenticated )
193+ {
194+ return Redirect ( "~/error" ) ;
195+ }
196+
197+ model . Blog = await _blogProvider . GetBlogItem ( ) ;
198+ model . Post . Description = model . Post . Description . MdToHtml ( ) ;
199+ model . Post . Content = model . Post . Content . MdToHtml ( ) ;
200+
201+ if ( ! model . Post . Author . Avatar . StartsWith ( "data:" ) )
202+ model . Post . Author . Avatar = Url . Content ( $ "~/{ model . Post . Author . Avatar } ") ;
203+
204+ if ( model . Post . PostType == PostType . Page )
205+ {
206+ string viewPath = $ "~/Views/Themes/{ model . Blog . Theme } /Page.cshtml";
207+ if ( IsViewExists ( viewPath ) )
208+ return View ( viewPath , model ) ;
209+ }
210+
211+ return View ( $ "~/Views/Themes/{ model . Blog . Theme } /Post.cshtml", model ) ;
212+ }
213+ catch
214+ {
215+ return Redirect ( "~/error" ) ;
216+ }
217+ }
218+ public async Task < ListModel > getBlogPosts ( string term = "" , int pager = 1 , string category = "" , string slug = "" ) {
219+
220+ var model = new ListModel { } ;
221+
222+ try
223+ {
224+ model . Blog = await _blogProvider . GetBlogItem ( ) ;
225+ }
226+ catch
227+ {
228+ return null ;
229+ }
230+
231+ model . Pager = new Pager ( pager , model . Blog . ItemsPerPage ) ;
232+
233+ if ( ! string . IsNullOrEmpty ( category ) )
234+ {
235+ model . PostListType = PostListType . Category ;
236+ model . Posts = await _postProvider . GetList ( model . Pager , 0 , category , "PF" ) ;
237+ }
238+ else if ( string . IsNullOrEmpty ( term ) )
239+ {
240+ model . PostListType = PostListType . Blog ;
241+ if ( model . Blog . IncludeFeatured )
242+ model . Posts = await _postProvider . GetList ( model . Pager , 0 , "" , "FP" ) ;
243+ else
244+ model . Posts = await _postProvider . GetList ( model . Pager , 0 , "" , "P" ) ;
245+ }
246+ else
247+ {
248+ model . PostListType = PostListType . Search ;
249+ model . Blog . Title = term ;
250+ model . Blog . Description = "" ;
251+ model . Posts = await _postProvider . Search ( model . Pager , term , 0 , "FP" ) ;
252+ }
253+
254+ if ( model . Pager . ShowOlder ) model . Pager . LinkToOlder = $ "?page={ model . Pager . Older } ";
255+ if ( model . Pager . ShowNewer ) model . Pager . LinkToNewer = $ "?page={ model . Pager . Newer } ";
256+
257+ return model ;
258+ }
229259 }
230260}
0 commit comments