22using Core . Data ;
33using Core . Services ;
44using Microsoft . AspNetCore . Mvc ;
5- using Microsoft . AspNetCore . Mvc . Rendering ;
65using System . Collections . Generic ;
6+ using System . IO ;
77using System . Linq ;
88using System . Threading . Tasks ;
99
@@ -15,8 +15,7 @@ public class ThemesModel : AdminPageModel
1515 IStorageService _storage ;
1616 INotificationService _ns ;
1717
18- [ BindProperty ]
19- public IEnumerable < SelectListItem > Themes { get ; set ; }
18+ public IEnumerable < ThemeItem > Themes { get ; set ; }
2019 public BlogItem BlogItem { get ; set ; }
2120
2221 public ThemesModel ( IDataService db , IStorageService storage , INotificationService ns )
@@ -26,7 +25,7 @@ public ThemesModel(IDataService db, IStorageService storage, INotificationServic
2625 _ns = ns ;
2726 }
2827
29- public async Task < IActionResult > OnGetAsync ( )
28+ public async Task < IActionResult > OnGetAsync ( string id , string act )
3029 {
3130 var author = await _db . Authors . GetItem ( a => a . AppUserName == User . Identity . Name ) ;
3231 IsAdmin = author . IsAdmin ;
@@ -36,15 +35,28 @@ public async Task<IActionResult> OnGetAsync()
3635 if ( ! IsAdmin )
3736 return RedirectToPage ( "../Shared/_Error" , new { code = 403 } ) ;
3837
38+ if ( act == "set" && ! string . IsNullOrEmpty ( id ) )
39+ {
40+ var theme = _db . CustomFields . Single ( f => f . AuthorId == 0 && f . Name == Constants . BlogTheme ) ;
41+ theme . Content = id ;
42+ _db . Complete ( ) ;
43+ Message = Resources . Updated ;
44+ }
45+
46+ if ( act == "del" && ! string . IsNullOrEmpty ( id ) )
47+ {
48+ Message = Resources . Removed ;
49+ }
50+
3951 BlogItem = await _db . CustomFields . GetBlogSettings ( ) ;
4052 Themes = GetThemes ( ) ;
4153
4254 return Page ( ) ;
4355 }
4456
45- List < SelectListItem > GetThemes ( )
57+ List < ThemeItem > GetThemes ( )
4658 {
47- var themes = new List < SelectListItem > ( ) ;
59+ var themes = new List < ThemeItem > ( ) ;
4860 var combined = new List < string > ( ) ;
4961
5062 var storageThemes = _storage . GetThemes ( ) ;
@@ -60,10 +72,17 @@ List<SelectListItem> GetThemes()
6072
6173 if ( combined != null && combined . Count > 0 )
6274 {
63- var current = new SelectListItem ( ) ;
75+ var current = new ThemeItem ( ) ;
6476 foreach ( var theme in combined )
6577 {
66- var item = new SelectListItem { Text = theme , Value = theme } ;
78+ var slash = Path . DirectorySeparatorChar . ToString ( ) ;
79+ var file = $ "{ AppSettings . WebRootPath } { slash } themes{ slash } { theme } { slash } { theme } .png";
80+ var item = new ThemeItem {
81+ Title = theme ,
82+ Cover = System . IO . File . Exists ( file ) ? $ "themes/{ theme } /{ theme } .png" : "lib/img/img-placeholder.png" ,
83+ IsCurrent = theme == BlogItem . Theme
84+ } ;
85+
6786 if ( theme == BlogItem . Theme )
6887 current = item ;
6988 else
@@ -75,4 +94,11 @@ List<SelectListItem> GetThemes()
7594 return themes ;
7695 }
7796 }
97+
98+ public class ThemeItem
99+ {
100+ public string Title { get ; set ; }
101+ public string Cover { get ; set ; }
102+ public bool IsCurrent { get ; set ; }
103+ }
78104}
0 commit comments