Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 8 files changed
  • 0 comments
  • 1 contributor
23  Drivers/UserViewPartDriver.cs
... ...
@@ -1,27 +1,20 @@
1  
-using NGM.ContentViewCounter.Models;
2  
-using NGM.ContentViewCounter.Services;
  1
+using Contrib.Voting.Services;
  2
+using NGM.ContentViewCounter.Models;
3 3
 using Orchard.ContentManagement.Drivers;
4 4
 
5 5
 namespace NGM.ContentViewCounter.Drivers {
6 6
     public class UserViewPartDriver : ContentPartDriver<UserViewPart> {
7  
-        private readonly IViewCounterService _viewCounterService;
  7
+        private readonly IVotingService _votingService;
8 8
 
9  
-        public UserViewPartDriver(IViewCounterService viewCounterService) {
10  
-            _viewCounterService = viewCounterService;
  9
+        public UserViewPartDriver(IVotingService votingService) {
  10
+            _votingService = votingService;
11 11
         }
12 12
 
13 13
         protected override DriverResult Display(UserViewPart part, string displayType, dynamic shapeHelper) {
14  
-            return Combined(
15  
-                ContentShape(
16  
-                    "Parts_UserView_SummaryAdmin",
17  
-                        () => shapeHelper.Parts_UserView_SummaryAdmin(BuildUserView(part)))
18  
-                );
19  
-        }
20  
-
21  
-        private UserViewPart BuildUserView(UserViewPart part) {
22  
-            part.TotalViews = _viewCounterService.TotalViewsFor(part.ContentItem);
  14
+            var resultRecord = _votingService.GetResult(part.ContentItem.Id, "count", "ContentViews");
  15
+            part.TotalViews = resultRecord == null ? 0 : (int)resultRecord.Value;
23 16
 
24  
-            return part;
  17
+            return ContentShape("Parts_UserView_SummaryAdmin", () => shapeHelper.Parts_UserView_SummaryAdmin(ContentPart: part));
25 18
         }
26 19
     }
27 20
 }
45  Handlers/UserViewPartHandler.cs
... ...
@@ -1,18 +1,19 @@
1 1
 using System;
  2
+using System.Linq;
  3
+using Contrib.Voting.Services;
2 4
 using NGM.ContentViewCounter.Models;
3  
-using NGM.ContentViewCounter.Services;
4 5
 using NGM.ContentViewCounter.Settings;
5 6
 using Orchard;
6 7
 using Orchard.ContentManagement.Handlers;
7 8
 
8 9
 namespace NGM.ContentViewCounter.Handlers {
9 10
     public class UserViewPartHandler : ContentHandler {
10  
-        private readonly IViewCounterService _viewCounterServices;
  11
+        private readonly IVotingService _votingService;
11 12
         private readonly IOrchardServices _orchardServices;
12 13
 
13  
-        public UserViewPartHandler(IViewCounterService viewCounterServices,
  14
+        public UserViewPartHandler(IVotingService votingService,
14 15
             IOrchardServices orchardServices) {
15  
-            _viewCounterServices = viewCounterServices;
  16
+            _votingService = votingService;
16 17
             _orchardServices = orchardServices;
17 18
 
18 19
             OnGetDisplayShape<UserViewPart>((context, part) => {
@@ -20,22 +21,32 @@ public class UserViewPartHandler : ContentHandler {
20 21
                 if (!context.DisplayType.Equals(settings.DisplayType, StringComparison.InvariantCultureIgnoreCase))
21 22
                     return;
22 23
 
23  
-                var currentUser = _orchardServices.WorkContext.CurrentUser;
  24
+                RecordView(part, settings);
  25
+            });
  26
+
  27
+        }
24 28
 
25  
-                if (currentUser != null) {
26  
-                    if (!_viewCounterServices.HasViewed(part.ContentItem, currentUser.UserName) || settings.AllowMultipleViewsFromSameUserToCount)
27  
-                        _viewCounterServices.AddView(part.ContentItem, currentUser.UserName, _orchardServices.WorkContext.HttpContext.Request.UserHostAddress);
28  
-                } else if (settings.AllowAnonymousViews) {
29  
-                    var anonHostname = _orchardServices.WorkContext.HttpContext.Request.UserHostAddress;
30  
-                    if (!string.IsNullOrWhiteSpace(_orchardServices.WorkContext.HttpContext.Request.Headers["X-Forwarded-For"]))
31  
-                        anonHostname += "-" + _orchardServices.WorkContext.HttpContext.Request.Headers["X-Forwarded-For"];
  29
+        private void RecordView(UserViewPart part, UserViewTypePartSettings settings) {
  30
+            var currentUser = _orchardServices.WorkContext.CurrentUser;
32 31
 
33  
-                    if (!_viewCounterServices.HasViewed(part.ContentItem, "Anonymous" + anonHostname) || settings.AllowMultipleViewsFromSameUserToCount) {
34  
-                        _viewCounterServices.AddView(part.ContentItem, "Anonymous" + anonHostname, anonHostname);
35  
-                    }
36  
-                }
37  
-            });
  32
+            if (currentUser != null) {
  33
+                Vote(currentUser.UserName, part, settings);
  34
+            } else if (settings.AllowAnonymousViews) {
  35
+                var anonHostname = _orchardServices.WorkContext.HttpContext.Request.UserHostAddress;
  36
+                if (!string.IsNullOrWhiteSpace(_orchardServices.WorkContext.HttpContext.Request.Headers["X-Forwarded-For"]))
  37
+                    anonHostname += "-" + _orchardServices.WorkContext.HttpContext.Request.Headers["X-Forwarded-For"];
  38
+
  39
+                Vote("Anonymous" + anonHostname, part, settings);
  40
+            }
  41
+        }
  42
+
  43
+        private void Vote(string userName, UserViewPart part, UserViewTypePartSettings settings) {
  44
+            var currentVote = _votingService.Get(v => v.ContentItemRecord == part.ContentItem.Record && v.Username == userName).FirstOrDefault();
38 45
 
  46
+            if (currentVote != null && settings.AllowMultipleViewsFromSameUserToCount)
  47
+                _votingService.ChangeVote(currentVote, 1);
  48
+            else if (currentVote == null)
  49
+                _votingService.Vote(part.ContentItem, userName, _orchardServices.WorkContext.HttpContext.Request.UserHostAddress, 1, "ContentViews");
39 50
         }
40 51
     }
41 52
 }
6  Migrations.cs
@@ -20,5 +20,11 @@ public class Migrations : DataMigrationImpl {
20 20
 
21 21
             return 1;
22 22
         }
  23
+
  24
+        public int UpdateFrom1() {
  25
+            SchemaBuilder.DropTable("UserViewCounterRecord");
  26
+
  27
+            return 2;
  28
+        }
23 29
     }
24 30
 }
13  Models/UserViewCounterRecord.cs
... ...
@@ -1,13 +0,0 @@
1  
-using System;
2  
-using Orchard.ContentManagement.Records;
3  
-
4  
-namespace NGM.ContentViewCounter.Models {
5  
-    public class UserViewCounterRecord {
6  
-        public virtual int Id { get; set; }
7  
-        public virtual DateTime? CreatedUtc { get; set; }
8  
-        public virtual ContentItemRecord ContentItemRecord { get; set; }
9  
-        public virtual string ContentType { get; set; }
10  
-        public virtual string Username { get; set; }
11  
-        public virtual string Hostname { get; set; }
12  
-    }
13  
-}
3  Module.txt
@@ -9,4 +9,5 @@ Features:
9 9
     NGM.ContentViewCounter:
10 10
         Name: Content View Counter
11 11
         Description: A module to count unique views of a particular content item.
12  
-        Category: Content
  12
+        Category: Content
  13
+		Dependencies: Contrib.Voting
6  NGM.ContentViewCounter.csproj
@@ -53,10 +53,8 @@
53 53
     <Compile Include="Handlers\UserViewPartHandler.cs" />
54 54
     <Compile Include="Migrations.cs" />
55 55
     <Compile Include="Models\UserViewPart.cs" />
56  
-    <Compile Include="Models\UserViewCounterRecord.cs" />
57 56
     <Compile Include="Properties\AssemblyInfo.cs" />
58 57
     <Compile Include="ResourceManifest.cs" />
59  
-    <Compile Include="Services\ViewCounterService.cs" />
60 58
     <Compile Include="Settings\UserViewTypePartSettings.cs" />
61 59
   </ItemGroup>
62 60
   <ItemGroup>
@@ -84,6 +82,10 @@
84 82
       <Project>{9916839C-39FC-4CEB-A5AF-89CA7E87119F}</Project>
85 83
       <Name>Orchard.Core</Name>
86 84
     </ProjectReference>
  85
+    <ProjectReference Include="..\Contrib.Voting\Contrib.Voting.csproj">
  86
+      <Project>{4DA5F35F-E62C-4B49-B5A8-379503257B3A}</Project>
  87
+      <Name>Contrib.Voting</Name>
  88
+    </ProjectReference>
87 89
   </ItemGroup>
88 90
   <ItemGroup>
89 91
     <Content Include="Views\DefinitionTemplates\UserViewTypePartSettings.cshtml" />
47  Services/ViewCounterService.cs
... ...
@@ -1,47 +0,0 @@
1  
-using System.Linq;
2  
-using NGM.ContentViewCounter.Models;
3  
-using Orchard;
4  
-using Orchard.ContentManagement;
5  
-using Orchard.Data;
6  
-using Orchard.Services;
7  
-
8  
-namespace NGM.ContentViewCounter.Services {
9  
-    public interface IViewCounterService : IDependency {
10  
-        void AddView(ContentItem contentItem, string userName, string hostname);
11  
-        bool HasViewed(ContentItem contentItem, string userName);
12  
-        int TotalViewsFor(ContentItem contentItem);
13  
-    }
14  
-
15  
-    public class ViewCounterService : IViewCounterService {
16  
-        private readonly IRepository<UserViewCounterRecord> _viewCounterRepository;
17  
-        private readonly IClock _clock;
18  
-
19  
-        public ViewCounterService(IRepository<UserViewCounterRecord> viewCounterRepository, IClock clock) {
20  
-            _viewCounterRepository = viewCounterRepository;
21  
-            _clock = clock;
22  
-        }
23  
-
24  
-        public void AddView(ContentItem contentItem, string userName, string hostname) {
25  
-            var userViewCounterRecord = new UserViewCounterRecord {
26  
-                ContentItemRecord = contentItem.Record,
27  
-                ContentType = contentItem.ContentType,
28  
-                CreatedUtc = _clock.UtcNow,
29  
-                Hostname = hostname,
30  
-                Username = userName
31  
-            };
32  
-
33  
-            _viewCounterRepository.Create(userViewCounterRecord);
34  
-        }
35  
-
36  
-        public bool HasViewed(ContentItem contentItem, string userName) {
37  
-            if (_viewCounterRepository.Fetch(r => r.Username == userName && r.ContentItemRecord == contentItem.Record).FirstOrDefault() != null)
38  
-                return true;
39  
-            return false;
40  
-        }
41  
-
42  
-
43  
-        public int TotalViewsFor(ContentItem contentItem) {
44  
-            return _viewCounterRepository.Fetch(r => r.ContentItemRecord == contentItem.Record).Count();
45  
-        }
46  
-    }
47  
-}
2  Views/Parts.UserView.SummaryAdmin.cshtml
@@ -4,5 +4,5 @@
4 4
 }
5 5
 <div class="user-view-admin">
6 6
   <h3>@T("User Views:")</h3>
7  
-  <span class="views">@Model.TotalViews</span>
  7
+  <span class="views">@Model.ContentPart.TotalViews</span>
8 8
 </div>

No commit comments for this range

Something went wrong with that request. Please try again.