77
88namespace  EssentialCSharp . Web . Controllers ; 
99
10- public  class  HomeController  :  Controller 
10+ public  class  HomeController ( ILogger < HomeController >   logger ,   IWebHostEnvironment   hostingEnvironment ,   ISiteMappingService   siteMappingService ,   IHttpContextAccessor   httpContextAccessor )  :  Controller 
1111{ 
12-     private  readonly  IConfiguration  _Configuration ; 
13-     private  readonly  IWebHostEnvironment  _HostingEnvironment ; 
14-     private  readonly  ISiteMappingService  _SiteMappingService ; 
15-     private  readonly  ILogger < HomeController >  _Logger ; 
16- 
17-     public  HomeController ( ILogger < HomeController >  logger ,  IWebHostEnvironment  hostingEnvironment ,  ISiteMappingService  siteMappingService ,  IConfiguration  configuration ) 
18-     { 
19-         _Logger  =  logger ; 
20-         _HostingEnvironment  =  hostingEnvironment ; 
21-         _SiteMappingService  =  siteMappingService ; 
22-         _Configuration  =  configuration ; 
23-     } 
24- 
2512    public  IActionResult  Index ( string  key ) 
2613    { 
2714        // if no key (default case), then load up home page 
28-         SiteMapping ?  siteMapping  =  _SiteMappingService . SiteMappings . Find ( key ) ; 
15+         SiteMapping ?  siteMapping  =  siteMappingService . SiteMappings . Find ( key ) ; 
2916
3017        if  ( string . IsNullOrEmpty ( key ) ) 
3118        { 
3219            return  RedirectToAction ( nameof ( Home ) ) ; 
3320        } 
3421        else  if  ( siteMapping  is  not null ) 
3522        { 
36-             string  filePath  =  Path . Combine ( _HostingEnvironment . ContentRootPath ,  Path . Combine ( siteMapping . PagePath ) ) ; 
23+             string  filePath  =  Path . Combine ( hostingEnvironment . ContentRootPath ,  Path . Combine ( siteMapping . PagePath ) ) ; 
3724            HtmlDocument  doc  =  new ( ) ; 
3825            doc . Load ( filePath ) ; 
3926            string  headHtml  =  doc . DocumentNode . Element ( "html" ) . Element ( "head" ) . InnerHtml ; 
@@ -44,6 +31,8 @@ public IActionResult Index(string key)
4431            ViewBag . PreviousPage  =  FlipPage ( siteMapping . ChapterNumber ,  siteMapping . PageNumber ,  false ) ; 
4532            ViewBag . HeadContents  =  headHtml ; 
4633            ViewBag . Contents  =  html ; 
34+             // Set the referral Id for use in the front end if available 
35+             ViewBag . ReferralId  =  httpContextAccessor . HttpContext ? . User ? . Claims ? . FirstOrDefault ( f =>  f . Type  ==  "ReferrerId" ) ? . Value ; 
4736            return  View ( ) ; 
4837        } 
4938        else 
@@ -84,19 +73,19 @@ public IActionResult Home()
8473    public  IActionResult  Guidelines ( ) 
8574    { 
8675        ViewBag . PageTitle  =  "Coding Guidelines" ; 
87-         FileInfo  fileInfo  =  new ( Path . Combine ( _HostingEnvironment . ContentRootPath ,  "Guidelines" ,  "guidelines.json" ) ) ; 
76+         FileInfo  fileInfo  =  new ( Path . Combine ( hostingEnvironment . ContentRootPath ,  "Guidelines" ,  "guidelines.json" ) ) ; 
8877        if  ( ! fileInfo . Exists ) 
8978        { 
9079            return  RedirectToAction ( nameof ( Error ) ,  new  {  errorMessage  =  "Guidelines could not be found" ,  statusCode  =  404  } ) ; 
9180        } 
92-         ViewBag . Guidelines  =  fileInfo . ReadGuidelineJsonFromInputDirectory ( _Logger ) ; 
81+         ViewBag . Guidelines  =  fileInfo . ReadGuidelineJsonFromInputDirectory ( logger ) ; 
9382        ViewBag . GuidelinesUrl  =  Request . Path . Value ; 
9483        return  View ( ) ; 
9584    } 
9685
9786    private  string  FlipPage ( int  currentChapter ,  int  currentPage ,  bool  next ) 
9887    { 
99-         if  ( _SiteMappingService . SiteMappings . Count  ==  0 ) 
88+         if  ( siteMappingService . SiteMappings . Count  ==  0 ) 
10089        { 
10190            return  "" ; 
10291        } 
@@ -107,18 +96,18 @@ private string FlipPage(int currentChapter, int currentPage, bool next)
10796            page  =  1 ; 
10897        } 
10998
110-         SiteMapping ?  siteMap  =  _SiteMappingService . SiteMappings . FirstOrDefault ( f =>  f . ChapterNumber  ==  currentChapter  &&  f . PageNumber  ==  currentPage  +  page ) ; 
99+         SiteMapping ?  siteMap  =  siteMappingService . SiteMappings . FirstOrDefault ( f =>  f . ChapterNumber  ==  currentChapter  &&  f . PageNumber  ==  currentPage  +  page ) ; 
111100
112101        if  ( siteMap  is  null ) 
113102        { 
114103            if  ( next ) 
115104            { 
116-                 siteMap  =  _SiteMappingService . SiteMappings . FirstOrDefault ( f =>  f . ChapterNumber  ==  currentChapter  +  1  &&  f . PageNumber  ==  1 ) ; 
105+                 siteMap  =  siteMappingService . SiteMappings . FirstOrDefault ( f =>  f . ChapterNumber  ==  currentChapter  +  1  &&  f . PageNumber  ==  1 ) ; 
117106            } 
118107            else 
119108            { 
120-                 int ?  previousPage  =  _SiteMappingService . SiteMappings . LastOrDefault ( f =>  f . ChapterNumber  ==  currentChapter  -  1 ) ? . PageNumber ; 
121-                 siteMap  =  _SiteMappingService . SiteMappings . FirstOrDefault ( f =>  f . ChapterNumber  ==  currentChapter  -  1  &&  f . PageNumber  ==  previousPage ) ; 
109+                 int ?  previousPage  =  siteMappingService . SiteMappings . LastOrDefault ( f =>  f . ChapterNumber  ==  currentChapter  -  1 ) ? . PageNumber ; 
110+                 siteMap  =  siteMappingService . SiteMappings . FirstOrDefault ( f =>  f . ChapterNumber  ==  currentChapter  -  1  &&  f . PageNumber  ==  previousPage ) ; 
122111            } 
123112            if  ( siteMap  is  null ) 
124113            { 
0 commit comments