11using Microsoft . AspNetCore . Mvc ;
2+ using Microsoft . AspNetCore . Mvc . Rendering ;
23using Serilog . Ui . Core ;
34using Serilog . Ui . Web . ViewModel ;
5+ using System . Collections . Generic ;
46using System . IO ;
7+ using System . Linq ;
58using System . Text ;
69using System . Threading . Tasks ;
710
811namespace Serilog . Ui . Web . Controllers
912{
1013 public class LogsController : Controller
1114 {
12- private static string _scripts ;
13- private static string _styles ;
15+ private static readonly string Scripts ;
16+ private static readonly string Styles ;
17+ private static readonly IEnumerable < SelectListItem > SelectListItems ;
1418 private readonly IDataProvider _dataProvider ;
1519
20+ static LogsController ( )
21+ {
22+ Styles = SetResource ( "Serilog.Ui.Web.wwwroot.css.main.css" ) ;
23+ Scripts = SetResource ( "Serilog.Ui.Web.wwwroot.js.main.js" ) ;
24+ SelectListItems = new List < SelectListItem >
25+ {
26+ new SelectListItem { Text = "10" , Value = "10" } ,
27+ new SelectListItem { Text = "25" , Value = "25" } ,
28+ new SelectListItem { Text = "50" , Value = "50" } ,
29+ new SelectListItem { Text = "100" , Value = "100" }
30+ } ;
31+ }
32+
1633 public LogsController ( IDataProvider dataProvider )
1734 {
1835 _dataProvider = dataProvider ;
@@ -26,39 +43,31 @@ public async Task<IActionResult> Index(int page = 1, int count = 10)
2643 if ( count > 100 )
2744 count = 100 ;
2845
46+ var x = SelectListItems . FirstOrDefault ( i => i . Value == count . ToString ( ) ) ;
47+ if ( x != null )
48+ x . Selected = true ;
49+ else
50+ SelectListItems . First ( ) . Selected = true ;
51+
2952 var ( logs , logCount ) = await _dataProvider . FetchDataAsync ( page , count ) ;
3053 var viewModel = new LogViewModel
3154 {
3255 LogCount = logCount ,
3356 Logs = logs ,
3457 Page = page ,
35- Count = count
58+ Count = count ,
59+ CountSelectListItems = SelectListItems
3660 } ;
3761
38- GetResources ( ) ;
62+ ViewData [ "Styles" ] = Styles ;
63+ ViewData [ "Scripts" ] = Scripts ;
3964
4065 return View ( viewModel ) ;
4166 }
4267
43- private void GetResources ( )
44- {
45- if ( _styles != null )
46- {
47- ViewData [ "Styles" ] = _styles ;
48- ViewData [ "Scripts" ] = _scripts ;
49- return ;
50- }
51-
52- _styles = SetResource ( "Serilog.Ui.Web.wwwroot.css.main.css" ) ;
53- ViewData [ "Styles" ] = _styles ;
54-
55- _scripts = SetResource ( "Serilog.Ui.Web.wwwroot.js.main.js" ) ;
56- ViewData [ "Scripts" ] = _scripts ;
57- }
58-
59- private string SetResource ( string resourceName )
68+ private static string SetResource ( string resourceName )
6069 {
61- var resourceStream = GetType ( ) . Assembly . GetManifestResourceStream ( resourceName ) ;
70+ var resourceStream = typeof ( LogsController ) . Assembly . GetManifestResourceStream ( resourceName ) ;
6271 var resource = string . Empty ;
6372
6473 using var reader = new StreamReader ( resourceStream , Encoding . UTF8 ) ;
0 commit comments