1- namespace Neolution . OrchardCoreModules . PageViewStats . Controllers
1+ namespace Neolution . OrchardCoreModules . PageViewStats . Controllers ;
2+
3+ using System ;
4+ using System . Threading . Tasks ;
5+ using Dapper ;
6+ using Microsoft . AspNetCore . Mvc ;
7+ using Neolution . OrchardCoreModules . PageViewStats . Models ;
8+ using Neolution . OrchardCoreModules . PageViewStats . Services ;
9+ using Neolution . OrchardCoreModules . PageViewStats . Settings ;
10+ using OrchardCore . Data ;
11+ using OrchardCore . Entities ;
12+ using OrchardCore . Settings ;
13+ using YesSql ;
14+
15+ public class CountPageViewController : Controller
216{
3- using System ;
4- using System . Text ;
5- using System . Threading . Tasks ;
6- using Dapper ;
7- using Microsoft . AspNetCore . Mvc ;
8- using Neolution . OrchardCoreModules . PageViewStats . Models ;
9- using Neolution . OrchardCoreModules . PageViewStats . Services ;
10- using OrchardCore . Data ;
11- using OrchardCore . Entities ;
12- using OrchardCore . Settings ;
13- using YesSql ;
17+ /// <summary>
18+ /// The table for storing the page views
19+ /// </summary>
20+ private string table ;
21+
22+ /// <summary>
23+ /// The SQL dialect
24+ /// </summary>
25+ private ISqlDialect dialect ;
1426
15- public class CountPageViewController : Controller
27+ private readonly ISession session ;
28+ private readonly IDbConnectionAccessor dbConnectionAccessor ;
29+ private readonly ISiteService siteService ;
30+ private readonly IBotDetector botDetector ;
31+
32+ public CountPageViewController ( ISession session , IDbConnectionAccessor dbConnectionAccessor , ISiteService siteService , IBotDetector botDetector )
1633 {
17- /// <summary>
18- /// The table for storing the page views
19- /// </summary>
20- private string table ;
34+ this . session = session ;
35+ this . dbConnectionAccessor = dbConnectionAccessor ;
36+ this . siteService = siteService ;
37+ this . botDetector = botDetector ;
2138
22- /// <summary>
23- /// The SQL dialect
24- /// </summary>
25- private ISqlDialect dialect ;
39+ SetupDatabaseFields ( ) ;
40+ }
2641
27- private readonly ISession session ;
28- private readonly IDbConnectionAccessor dbConnectionAccessor ;
29- private readonly ISiteService siteService ;
30- private readonly IBotDetector botDetector ;
42+ [ HttpPost ]
43+ [ ActionName ( nameof ( Index ) ) ]
44+ public async Task < ActionResult > IndexPost ( string contentItemId )
45+ {
46+ var settings = ( await siteService . GetSiteSettingsAsync ( ) ) . As < PageViewStatsSettings > ( ) ;
3147
32- public CountPageViewController ( ISession session , IDbConnectionAccessor dbConnectionAccessor , ISiteService siteService , IBotDetector botDetector )
48+ if ( ! settings . IsEnabled )
3349 {
34- this . session = session ;
35- this . dbConnectionAccessor = dbConnectionAccessor ;
36- this . siteService = siteService ;
37- this . botDetector = botDetector ;
38-
39- SetupDatabaseFields ( ) ;
50+ // Ignore requests when page view statistics are disabled
51+ return Ok ( ) ;
4052 }
4153
42- [ HttpPost ]
43- [ ActionName ( nameof ( Index ) ) ]
44- public async Task < ActionResult > IndexPost ( string contentItemId )
54+ var pageView = new PageView
4555 {
46- var settings = ( await siteService . GetSiteSettingsAsync ( ) ) . As < PageViewStatsSettings > ( ) ;
47-
48- if ( ! settings . IsEnabled )
49- {
50- // Ignore requests when page view statistics are disabled
51- return Ok ( ) ;
52- }
53-
54- var pageView = new PageView
55- {
56- ContentItemId = contentItemId ,
57- CreatedUtc = DateTimeOffset . UtcNow ,
58- } ;
56+ ContentItemId = contentItemId ,
57+ CreatedUtc = DateTimeOffset . UtcNow ,
58+ } ;
5959
60- if ( settings . CollectUserIp )
61- {
62- pageView . RequestIpAddress = this . HttpContext . Connection . RemoteIpAddress ? . ToString ( ) ;
63- }
60+ if ( settings . CollectUserIp )
61+ {
62+ pageView . RequestIpAddress = this . HttpContext . Connection . RemoteIpAddress ? . ToString ( ) ;
63+ }
6464
65- if ( settings . CollectUserAgentString )
66- {
67- pageView . RequestUserAgentString = this . Request . Headers . UserAgent . ToString ( ) ;
68- pageView . RequestUserAgentIsRobot = this . botDetector . CheckUserAgentString ( pageView . RequestUserAgentString ) ;
69- }
65+ if ( settings . CollectUserAgentString )
66+ {
67+ pageView . RequestUserAgentString = this . Request . Headers . UserAgent . ToString ( ) ;
68+ pageView . RequestUserAgentIsRobot = this . botDetector . CheckUserAgentString ( pageView . RequestUserAgentString ) ;
69+ }
7070
71- await using var connection = this . dbConnectionAccessor . CreateConnection ( ) ;
71+ await using var connection = this . dbConnectionAccessor . CreateConnection ( ) ;
7272
73- var insertCmd =
74- $ "INSERT INTO { dialect . QuoteForTableName ( table ) } (" +
75- $ " { dialect . QuoteForColumnName ( nameof ( PageView . Id ) ) } , " +
76- $ " { dialect . QuoteForColumnName ( nameof ( PageView . CreatedUtc ) ) } , " +
77- $ " { dialect . QuoteForColumnName ( nameof ( PageView . ContentItemId ) ) } , " +
78- $ " { dialect . QuoteForColumnName ( nameof ( PageView . RequestIpAddress ) ) } , " +
79- $ " { dialect . QuoteForColumnName ( nameof ( PageView . RequestUserAgentString ) ) } , " +
80- $ " { dialect . QuoteForColumnName ( nameof ( PageView . RequestUserAgentIsRobot ) ) } ) " +
81- $ "VALUES (" +
82- $ " @{ nameof ( PageView . Id ) } , " +
83- $ " @{ nameof ( PageView . CreatedUtc ) } , " +
84- $ " @{ nameof ( PageView . ContentItemId ) } , " +
85- $ " @{ nameof ( PageView . RequestIpAddress ) } , " +
86- $ " @{ nameof ( PageView . RequestUserAgentString ) } , " +
87- $ " @{ nameof ( PageView . RequestUserAgentIsRobot ) } );";
73+ var insertCmd =
74+ $ "INSERT INTO { dialect . QuoteForTableName ( table ) } (" +
75+ $ " { dialect . QuoteForColumnName ( nameof ( PageView . Id ) ) } , " +
76+ $ " { dialect . QuoteForColumnName ( nameof ( PageView . CreatedUtc ) ) } , " +
77+ $ " { dialect . QuoteForColumnName ( nameof ( PageView . ContentItemId ) ) } , " +
78+ $ " { dialect . QuoteForColumnName ( nameof ( PageView . RequestIpAddress ) ) } , " +
79+ $ " { dialect . QuoteForColumnName ( nameof ( PageView . RequestUserAgentString ) ) } , " +
80+ $ " { dialect . QuoteForColumnName ( nameof ( PageView . RequestUserAgentIsRobot ) ) } ) " +
81+ $ "VALUES (" +
82+ $ " @{ nameof ( PageView . Id ) } , " +
83+ $ " @{ nameof ( PageView . CreatedUtc ) } , " +
84+ $ " @{ nameof ( PageView . ContentItemId ) } , " +
85+ $ " @{ nameof ( PageView . RequestIpAddress ) } , " +
86+ $ " @{ nameof ( PageView . RequestUserAgentString ) } , " +
87+ $ " @{ nameof ( PageView . RequestUserAgentIsRobot ) } );";
8888
89- await connection . ExecuteAsync ( insertCmd , pageView ) ;
90-
91- return Ok ( ) ;
92- }
89+ await connection . ExecuteAsync ( insertCmd , pageView ) ;
9390
94- private void SetupDatabaseFields ( )
95- {
96- dialect = session . Store . Configuration . SqlDialect ;
91+ return Ok ( ) ;
92+ }
9793
98- var tablePrefix = session . Store . Configuration . TablePrefix ;
99- if ( ! string . IsNullOrEmpty ( tablePrefix ) )
100- {
101- tablePrefix += '_' ;
102- }
94+ private void SetupDatabaseFields ( )
95+ {
96+ dialect = session . Store . Configuration . SqlDialect ;
10397
104- table = $ "{ tablePrefix } { PageView . TableName } ";
98+ var tablePrefix = session . Store . Configuration . TablePrefix ;
99+ if ( ! string . IsNullOrEmpty ( tablePrefix ) )
100+ {
101+ tablePrefix += '_' ;
105102 }
103+
104+ table = $ "{ tablePrefix } { PageView . TableName } ";
106105 }
107- }
106+ }
0 commit comments