2
2
using Cantarus . Modules . PolyDeploy . Components . Logging ;
3
3
using Cantarus . Modules . PolyDeploy . Components . WebAPI . ActionFilters ;
4
4
using DotNetNuke . Web . Api ;
5
+ using Newtonsoft . Json ;
6
+ using System ;
5
7
using System . Collections . Generic ;
6
8
using System . Linq ;
7
9
using System . Net ;
8
10
using System . Net . Http ;
11
+ using System . Text ;
9
12
using System . Web . Http ;
13
+ using System . Web . Script . Serialization ;
10
14
11
15
namespace Cantarus . Modules . PolyDeploy . Components . WebAPI
12
16
{
@@ -27,9 +31,92 @@ public HttpResponseMessage Browse(int pageIndex = 0, int pageSize = 30, string e
27
31
actualSeverity = ( EventLogSeverity ) severity ;
28
32
}
29
33
34
+ // Get event logs.
30
35
List < EventLog > eventLogs = EventLogManager . Browse ( pageIndex , pageSize , eventType , actualSeverity ) . ToList ( ) ;
31
36
32
- return Request . CreateResponse ( HttpStatusCode . OK , eventLogs ) ;
37
+ // Work out pagination details.
38
+ int rowCount = EventLogManager . EventCount ( ) ;
39
+ int pageCount = ( int ) Math . Ceiling ( ( double ) ( rowCount / pageSize ) ) ;
40
+
41
+ // Start building meta.
42
+ Dictionary < string , dynamic > pagination = new Dictionary < string , dynamic > ( ) ;
43
+
44
+ // Add basics.
45
+ pagination . Add ( "Records" , rowCount ) ;
46
+ pagination . Add ( "Pages" , pageCount ) ;
47
+ pagination . Add ( "CurrentPage" , pageIndex ) ;
48
+
49
+ // Build navigation.
50
+ Dictionary < string , string > navigation = new Dictionary < string , string > ( ) ;
51
+
52
+ // Parameters passed in not changed by pagination.
53
+ string fixedParams = "" ;
54
+
55
+ // Page size.
56
+ if ( pageSize != 30 )
57
+ {
58
+ fixedParams += string . Format ( "pageSize={0}" , pageSize ) ;
59
+ }
60
+
61
+ // Event type.
62
+ if ( eventType != null )
63
+ {
64
+ fixedParams += string . Format ( "eventType={0}" , eventType ) ;
65
+ }
66
+
67
+ // Severity.
68
+ if ( severity != - 1 )
69
+ {
70
+ fixedParams += string . Format ( "eventType={0}" , severity ) ;
71
+ }
72
+
73
+ // Is there a next page?
74
+ if ( pageIndex < pageCount )
75
+ {
76
+ string nextLink = string . Format ( "Browse?pageIndex={0}" , pageIndex + 1 ) ;
77
+
78
+ if ( ! string . IsNullOrEmpty ( fixedParams ) )
79
+ {
80
+ nextLink = string . Format ( "{0}&{1}" , nextLink , fixedParams ) ;
81
+ }
82
+
83
+ navigation . Add ( "Next" , nextLink ) ;
84
+ }
85
+
86
+ // Is there a previous page?
87
+ if ( pageIndex > 0 )
88
+ {
89
+ string prevLink = string . Format ( "Browse?pageIndex={0}" , pageIndex - 1 ) ;
90
+
91
+ if ( ! string . IsNullOrEmpty ( fixedParams ) )
92
+ {
93
+ prevLink = string . Format ( "{0}&{1}" , prevLink , fixedParams ) ;
94
+ }
95
+
96
+ navigation . Add ( "Previous" , prevLink ) ;
97
+ }
98
+
99
+ // Add navigation.
100
+ pagination . Add ( "Navigation" , navigation ) ;
101
+
102
+ Dictionary < string , dynamic > payload = new Dictionary < string , dynamic > ( ) ;
103
+
104
+ payload . Add ( "Data" , eventLogs ) ;
105
+ payload . Add ( "Pagination" , pagination ) ;
106
+
107
+ string json = JsonConvert . SerializeObject ( payload ) ;
108
+
109
+ HttpResponseMessage response = Request . CreateResponse ( HttpStatusCode . OK ) ;
110
+
111
+ response . Content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
112
+
113
+ return response ;
114
+ }
115
+
116
+ [ HttpGet ]
117
+ public HttpResponseMessage Count ( )
118
+ {
119
+ return Request . CreateResponse ( HttpStatusCode . OK , EventLogManager . EventCount ( ) ) ;
33
120
}
34
121
35
122
[ HttpGet ]
0 commit comments