浏览代码

[WebApiUtils] Get multiple return total element count

feature/authentication-tests
Robin Thoni 9 年前
父节点
当前提交
08d05e0cf3

WebApiUtils/BusinessManager/CheckModelForNullAttribute.cs → WebApiUtils/BusinessManager/Attributes/CheckModelForNullAttribute.cs 查看文件

@@ -5,7 +5,7 @@ using System.Net.Http;
5 5
 using System.Web.Http.Controllers;
6 6
 using System.Web.Http.Filters;
7 7
 
8
-namespace iiie.WebApiUtils.BusinessManager
8
+namespace iiie.WebApiUtils.BusinessManager.Attributes
9 9
 {
10 10
     [AttributeUsage(AttributeTargets.Method)]
11 11
     public class CheckModelForNullAttribute : ActionFilterAttribute

WebApiUtils/BusinessManager/ExceptionLoggerAttribute.cs → WebApiUtils/BusinessManager/Attributes/ExceptionLoggerAttribute.cs 查看文件

@@ -4,7 +4,7 @@ using System.Web.Http.Filters;
4 4
 using iiie.Logs.DataAccess;
5 5
 using iiie.Logs.DBO;
6 6
 
7
-namespace iiie.WebApiUtils.BusinessManager
7
+namespace iiie.WebApiUtils.BusinessManager.Attributes
8 8
 {
9 9
     [AttributeUsage(AttributeTargets.Method)]
10 10
     public class ExceptionLoggerAttribute : ExceptionFilterAttribute

WebApiUtils/BusinessManager/ValidateModelStateAttribute.cs → WebApiUtils/BusinessManager/Attributes/ValidateModelStateAttribute.cs 查看文件

@@ -3,7 +3,7 @@ using System.Net.Http;
3 3
 using System.Web.Http.Controllers;
4 4
 using System.Web.Http.Filters;
5 5
 
6
-namespace iiie.WebApiUtils.BusinessManager
6
+namespace iiie.WebApiUtils.BusinessManager.Attributes
7 7
 {
8 8
     public class ValidateModelStateAttribute : ActionFilterAttribute
9 9
     {

+ 12
- 5
WebApiUtils/BusinessManager/SqlServerManager.cs 查看文件

@@ -5,6 +5,7 @@ using System.Linq;
5 5
 using System.Linq.Expressions;
6 6
 using iiie.Logs.DataAccess;
7 7
 using iiie.Logs.DBO;
8
+using iiie.WebApiUtils.DBO;
8 9
 
9 10
 namespace iiie.WebApiUtils.BusinessManager
10 11
 {
@@ -134,13 +135,19 @@ namespace iiie.WebApiUtils.BusinessManager
134 135
         /// <param name="page">The page numeber (0 based)</param>
135 136
         /// <param name="perPage">The maximum number of items par page</param>
136 137
         /// <returns>All matching objects</returns>
137
-        public static OpResult<IEnumerable<TDboGet>> GetMultiple<TKey>(Expression<Func<TDbObject, bool>> predicate,
138
-            Expression<Func<TDbObject, TKey>> orderBy, int page = 0,int perPage = Int32.MaxValue)
138
+        public static OpResult<DboGetMultiple<TDboGet>> GetMultiple<TKey>(Expression<Func<TDbObject, bool>> predicate,
139
+            Expression<Func<TDbObject, TKey>> orderBy, int page = 0, int perPage = Int32.MaxValue)
139 140
         {
140 141
             return Execute((db, table) =>
141 142
             {
142
-                var results = table.OrderBy(orderBy).Where(predicate).Skip(page * perPage).Take(perPage).ToList();
143
-                return OpResult<IEnumerable<TDboGet>>.Ok(results.Select(DbToDboGetStatic));
143
+                var count = table.Count(predicate);
144
+                var results = table.OrderBy(orderBy).Where(predicate).Skip(page * perPage).Take(perPage).Select(DbToDboGetStatic).ToList();
145
+                var result = new DboGetMultiple<TDboGet>
146
+                {
147
+                    Count = count,
148
+                    Data = results
149
+                };
150
+                return OpResult<DboGetMultiple<TDboGet>>.Ok(result);
144 151
             });
145 152
         }
146 153
 
@@ -152,7 +159,7 @@ namespace iiie.WebApiUtils.BusinessManager
152 159
         /// <param name="orderBy">The order by function</param>
153 160
         /// <param name="keys">The key names and values</param>
154 161
         /// <returns>All matching objects</returns>
155
-        public static OpResult<IEnumerable<TDboGet>> GetMultipleByKeys<TKey>(Expression<Func<TDbObject, TKey>> orderBy,
162
+        public static OpResult<DboGetMultiple<TDboGet>> GetMultipleByKeys<TKey>(Expression<Func<TDbObject, TKey>> orderBy,
156 163
             int page = 0, int perPage = Int32.MaxValue, params KeyValuePair<string, object>[] keys)
157 164
         {
158 165
             return GetMultiple(GetExpression(keys), orderBy, page, perPage);

+ 1
- 0
WebApiUtils/BusinessManager/WebApiUtils.cs 查看文件

@@ -1,5 +1,6 @@
1 1
 using System.Web.Http;
2 2
 using iiie.Logs.DataAccess;
3
+using iiie.WebApiUtils.BusinessManager.Attributes;
3 4
 
4 5
 namespace iiie.WebApiUtils.BusinessManager
5 6
 {

+ 17
- 0
WebApiUtils/DBO/DboGetMultiple.cs 查看文件

@@ -0,0 +1,17 @@
1
+using System.Collections.Generic;
2
+
3
+namespace iiie.WebApiUtils.DBO
4
+{
5
+    public class DboGetMultiple<TDboGet>
6
+    {
7
+        /// <summary>
8
+        /// The number of data that matched
9
+        /// </summary>
10
+        public int Count { get; set; }
11
+
12
+        /// <summary>
13
+        /// The data that matched
14
+        /// </summary>
15
+        public IEnumerable<TDboGet> Data { get; set; }
16
+    }
17
+}

+ 4
- 3
WebApiUtils/WebApiUtils.csproj 查看文件

@@ -90,12 +90,13 @@
90 90
   </ItemGroup>
91 91
   <ItemGroup>
92 92
     <Compile Include="BusinessManager\BMRHandler.cs" />
93
-    <Compile Include="BusinessManager\CheckModelForNullAttribute.cs" />
94
-    <Compile Include="BusinessManager\ExceptionLoggerAttribute.cs" />
93
+    <Compile Include="BusinessManager\Attributes\CheckModelForNullAttribute.cs" />
94
+    <Compile Include="BusinessManager\Attributes\ExceptionLoggerAttribute.cs" />
95 95
     <Compile Include="BusinessManager\SqlServerBasicManager.cs" />
96 96
     <Compile Include="BusinessManager\SqlServerManager.cs" />
97
-    <Compile Include="BusinessManager\ValidateModelStateAttribute.cs" />
97
+    <Compile Include="BusinessManager\Attributes\ValidateModelStateAttribute.cs" />
98 98
     <Compile Include="BusinessManager\WebApiUtils.cs" />
99
+    <Compile Include="DBO\DboGetMultiple.cs" />
99 100
     <Compile Include="Properties\AssemblyInfo.cs" />
100 101
   </ItemGroup>
101 102
   <ItemGroup>

正在加载...
取消
保存