@@ -36,39 +36,53 @@ public async Task<IActionResult> Index()
3636 return View ( items ) ;
3737 }
3838
39- public IActionResult Create ( )
39+ public async Task < IActionResult > Create ( )
4040 {
41+ await LoadPagesAsync ( ) ;
4142 return View ( new BlockTemplate ( ) ) ;
4243 }
4344
4445 [ HttpPost ]
4546 [ ValidateAntiForgeryToken ]
46- public async Task < IActionResult > Create ( BlockTemplate model )
47+ public async Task < IActionResult > Create ( BlockTemplate model , List < int > ? pageIds , string ? zone , string ? role )
4748 {
48- if ( ! ModelState . IsValid ) return View ( model ) ;
49+ if ( ! ModelState . IsValid )
50+ {
51+ await LoadPagesAsync ( ) ;
52+ return View ( model ) ;
53+ }
4954 model . Html = _sanitizer . Sanitize ( model . Html ) ;
5055 _db . BlockTemplates . Add ( model ) ;
5156 _db . BlockTemplateVersions . Add ( new BlockTemplateVersion { Template = model , Html = model . Html } ) ;
5257 await _db . SaveChangesAsync ( ) ;
58+ await AddSectionsAsync ( model , pageIds , zone , role ) ;
59+ await _db . SaveChangesAsync ( ) ;
5360 return RedirectToAction ( nameof ( Index ) ) ;
5461 }
5562
5663 public async Task < IActionResult > Edit ( int id )
5764 {
5865 var item = await _db . BlockTemplates . FindAsync ( id ) ;
5966 if ( item == null ) return NotFound ( ) ;
67+ await LoadPagesAsync ( ) ;
6068 return View ( item ) ;
6169 }
6270
6371 [ HttpPost ]
6472 [ ValidateAntiForgeryToken ]
65- public async Task < IActionResult > Edit ( BlockTemplate model )
73+ public async Task < IActionResult > Edit ( BlockTemplate model , List < int > ? pageIds , string ? zone , string ? role )
6674 {
67- if ( ! ModelState . IsValid ) return View ( model ) ;
75+ if ( ! ModelState . IsValid )
76+ {
77+ await LoadPagesAsync ( ) ;
78+ return View ( model ) ;
79+ }
6880 model . Html = _sanitizer . Sanitize ( model . Html ) ;
6981 _db . Update ( model ) ;
7082 _db . BlockTemplateVersions . Add ( new BlockTemplateVersion { BlockTemplateId = model . Id , Html = model . Html } ) ;
7183 await _db . SaveChangesAsync ( ) ;
84+ await AddSectionsAsync ( model , pageIds , zone , role ) ;
85+ await _db . SaveChangesAsync ( ) ;
7286 return RedirectToAction ( nameof ( Index ) ) ;
7387 }
7488
@@ -208,4 +222,34 @@ public async Task<IActionResult> AddToPage(int id, List<int> pageIds, string zon
208222 return RedirectToAction ( nameof ( Index ) ) ;
209223 }
210224
225+ private async Task AddSectionsAsync ( BlockTemplate template , List < int > ? pageIds , string ? zone , string ? role )
226+ {
227+ if ( pageIds == null || pageIds . Count == 0 || string . IsNullOrWhiteSpace ( zone ) )
228+ return ;
229+ var roleEntity = await _db . Roles . FirstOrDefaultAsync ( r => r . Name == role ) ;
230+ int ? roleId = roleEntity ? . Id ;
231+ zone = zone ! . Trim ( ) ;
232+ if ( pageIds . Contains ( 0 ) )
233+ {
234+ pageIds = await _db . Pages . Select ( p => p . Id ) . ToListAsync ( ) ;
235+ }
236+ foreach ( var pageId in pageIds )
237+ {
238+ var sort = await _db . PageSections
239+ . Where ( s => s . PageId == pageId && s . Zone == zone )
240+ . Select ( s => s . SortOrder )
241+ . DefaultIfEmpty ( - 1 )
242+ . MaxAsync ( ) + 1 ;
243+ var section = new PageSection
244+ {
245+ PageId = pageId ,
246+ Zone = zone ,
247+ SortOrder = sort ,
248+ Html = template . Html ,
249+ Type = PageSectionType . Html ,
250+ RoleId = roleId
251+ } ;
252+ _db . PageSections . Add ( section ) ;
253+ }
254+ }
211255}
0 commit comments