11using Core . Services ;
22using Microsoft . AspNetCore . Mvc ;
3+ using System ;
4+ using System . Linq ;
35
46namespace Common . Widgets
57{
@@ -15,9 +17,73 @@ public PostList(IDataService db)
1517
1618 public IViewComponentResult Invoke ( string theme , string widget )
1719 {
18- var model = _db . BlogPosts . All ( ) ;
20+ var keyAuth = $ "{ theme } -{ widget } -auth";
21+ var keyCat = $ "{ theme } -{ widget } -cat";
22+ var keyMax = $ "{ theme } -{ widget } -max";
23+
24+ var selectedAuth = _db . CustomFields . GetCustomValue ( keyAuth ) ;
25+ var selectedCat = _db . CustomFields . GetCustomValue ( keyCat ) ;
26+ var maxRecords = _db . CustomFields . GetCustomValue ( keyMax ) ;
27+
28+ if ( selectedAuth == "All" ) { selectedAuth = "" ; }
29+ if ( selectedCat == "All" ) { selectedCat = "" ; }
30+ if ( string . IsNullOrEmpty ( maxRecords ) ) { maxRecords = "10" ; }
31+
32+ var posts = _db . BlogPosts . All ( )
33+ . Where ( p => p . Published > DateTime . MinValue )
34+ . OrderByDescending ( p => p . Published ) . ToList ( ) ;
35+
36+ var auth = _db . Authors . All ( ) . Where ( a => a . DisplayName == selectedAuth ) . FirstOrDefault ( ) ;
37+
38+ if ( ! string . IsNullOrEmpty ( selectedAuth ) && ! string . IsNullOrEmpty ( selectedCat ) )
39+ {
40+ var p1 = posts . Where ( p => p . Categories != null && p . Categories . Contains ( selectedCat ) && p . AuthorId == auth . Id ) ;
41+ if ( p1 != null ) posts = p1 . ToList ( ) ;
42+ }
43+ else if ( ! string . IsNullOrEmpty ( selectedAuth ) )
44+ {
45+ var p2 = posts . Where ( p => p . AuthorId == auth . Id ) . ToList ( ) ;
46+ if ( p2 != null ) posts = p2 . ToList ( ) ;
47+ }
48+ else if ( ! string . IsNullOrEmpty ( selectedCat ) )
49+ {
50+ var p3 = posts . Where ( p => p . Categories != null && p . Categories . Contains ( selectedCat ) ) . ToList ( ) ;
51+ if ( p3 != null ) posts = p3 . ToList ( ) ;
52+ }
53+
54+ int maxRec ;
55+ if ( ! int . TryParse ( maxRecords , out maxRec ) )
56+ maxRec = 10 ;
57+
58+ var model = posts . Take ( maxRec ) . ToList ( ) ;
1959
2060 return View ( "~/Views/Widgets/PostList/Index.cshtml" , model ) ;
2161 }
2262 }
63+
64+ [ Route ( "widgets/api/postlist" ) ]
65+ public class PostListController : Controller
66+ {
67+ IDataService _db ;
68+
69+ public PostListController ( IDataService db )
70+ {
71+ _db = db ;
72+ }
73+
74+ [ HttpPost ]
75+ [ Route ( "edit" ) ]
76+ public IActionResult Edit ( string selAuthors , string selCats , string txtMaxRecords , string hdnWidget , string hdnTheme )
77+ {
78+ var keyAuth = $ "{ hdnTheme } -{ hdnWidget } -auth";
79+ var keyCat = $ "{ hdnTheme } -{ hdnWidget } -cat";
80+ var keyMax = $ "{ hdnTheme } -{ hdnWidget } -max";
81+
82+ _db . CustomFields . SaveCustomValue ( keyAuth , selAuthors ) ;
83+ _db . CustomFields . SaveCustomValue ( keyCat , selCats ) ;
84+ _db . CustomFields . SaveCustomValue ( keyMax , txtMaxRecords ) ;
85+
86+ return Redirect ( "~/admin/settings/themes" ) ;
87+ }
88+ }
2389}
0 commit comments