1+ using jcdcdev . Umbraco . Core . Extensions ;
2+ using Microsoft . AspNetCore . Authorization ;
3+ using Microsoft . AspNetCore . Mvc ;
4+ using Microsoft . AspNetCore . Mvc . ViewEngines ;
5+ using Microsoft . Extensions . Logging ;
6+ using Umbraco . Cms . Api . Common . Attributes ;
7+ using Umbraco . Cms . Api . Common . Filters ;
8+ using Umbraco . Cms . Api . Management . Filters ;
9+ using Umbraco . Cms . Web . Common . Authorization ;
10+ using Umbraco . Community . SimpleTrees . Web . Models ;
11+
12+ namespace Umbraco . Community . SimpleTrees . Web . Controllers ;
13+
14+ [ ApiExplorerSettings ( GroupName = Constants . Api . GroupName ) ]
15+ [ SimpleTreesVersionedRoute ( "tree" ) ]
16+ [ MapToApi ( Constants . Api . ApiName ) ]
17+ [ JsonOptionsName ( Cms . Core . Constants . JsonOptionsNames . BackOffice ) ]
18+ [ ApiController ]
19+ [ Authorize ( Policy = AuthorizationPolicies . BackOfficeAccess ) ]
20+ [ AppendEventMessages ]
21+ [ Produces ( "application/json" ) ]
22+ public class SimpleTreesRenderController ( ICompositeViewEngine viewEngine , ILogger < SimpleTreesRenderController > logger ) : Controller
23+ {
24+ [ HttpGet ( "render" ) ]
25+ [ Produces < SimpleTreeRenderModel > ]
26+ public async Task < IActionResult > Render ( string unique , string entityType )
27+ {
28+ var model = new SimpleTreeViewModel ( unique , entityType ) ;
29+ var path = model . ViewPath ;
30+ var result = viewEngine . GetView ( null , path , false ) ;
31+ if ( result . Success )
32+ {
33+ logger . LogDebug ( "Rendering view {ViewPath} for unique {Unique} and entity type {EntityType}" , path , unique , entityType ) ;
34+ var partialViewResult = await this . RenderViewResultToStringAsync ( result , model ) ;
35+ return Ok ( SimpleTreeRenderModel . Create ( partialViewResult ) ) ;
36+ }
37+
38+ var viewComponentName = model . ViewComponent ;
39+ if ( ! this . ViewComponentExists ( viewComponentName ) )
40+ {
41+ logger . LogDebug ( "ViewComponent {ViewComponent} not found" , viewComponentName ) ;
42+ return await ReturnError ( model ) ;
43+ }
44+
45+ var viewComponentResult = await this . RenderViewComponentToStringAsync ( viewComponentName , model ) ;
46+ logger . LogDebug ( "ViewComponent {ViewComponent} result for unique {Unique} and entity type {EntityType} {Body}" , viewComponentName , unique , entityType , viewComponentResult ) ;
47+ return Ok ( SimpleTreeRenderModel . Create ( viewComponentResult ) ) ;
48+ }
49+
50+ private async Task < IActionResult > ReturnError ( SimpleTreeViewModel viewModel )
51+ {
52+ var errorViewResult = viewEngine . GetView ( null , Constants . ErrorViewPath , false ) ;
53+ if ( errorViewResult . Success )
54+ {
55+ var body = await this . RenderViewResultToStringAsync ( errorViewResult , viewModel ) ;
56+ return Ok ( SimpleTreeRenderModel . Create ( body ) ) ;
57+ }
58+
59+ return Ok ( SimpleTreeRenderModel . Error ) ;
60+ }
61+ }
0 commit comments