瀏覽代碼

[WebApiUtils] Added global predicate

feature/authentication-tests
Robin Thoni 9 年之前
父節點
當前提交
410b439c44
共有 1 個文件被更改,包括 23 次插入2 次删除
  1. 23
    2
      WebApiUtils/BusinessManager/SqlServerManager.cs

+ 23
- 2
WebApiUtils/BusinessManager/SqlServerManager.cs 查看文件

@@ -3,6 +3,7 @@ using System.Collections.Generic;
3 3
 using System.Data.Entity;
4 4
 using System.Linq;
5 5
 using System.Linq.Expressions;
6
+using System.Runtime.Serialization;
6 7
 using iiie.Logs.DBO;
7 8
 using iiie.WebApiUtils.DBO;
8 9
 
@@ -32,6 +33,15 @@ namespace iiie.WebApiUtils.BusinessManager
32 33
         /// <returns>The DB object</returns>
33 34
         public abstract OpResult<TDbObject> DboAddToDb(TDboAdd obj);
34 35
 
36
+        /// <summary>
37
+        /// Return a global predicate that will be applied to all selects
38
+        /// </summary>
39
+        /// <returns>The expression</returns>
40
+        public virtual Expression<Func<TDbObject, bool>> GetGlobalSelectPredicate()
41
+        {
42
+            return x => true;
43
+        }
44
+
35 45
         /// <summary>
36 46
         /// Convert a DBO to DB object
37 47
         /// </summary>
@@ -40,6 +50,16 @@ namespace iiie.WebApiUtils.BusinessManager
40 50
         /// <returns>The DB object</returns>
41 51
         public abstract OpResult<TDbObject> DboEditToDb(TDboEdit obj, TDbObject edit);
42 52
 
53
+        /// <summary>
54
+        /// Helper to get real select predicate
55
+        /// </summary>
56
+        /// <returns>The global predicate</returns>
57
+        public static Expression<Func<TDbObject, bool>> GetGlobalSelectPredicateStatic()
58
+        {
59
+            var obj = new TThis();
60
+            return obj.GetGlobalSelectPredicate();
61
+        }
62
+
43 63
         /// <summary>
44 64
         /// Helper to convert DB object to DBO
45 65
         /// </summary>
@@ -82,7 +102,7 @@ namespace iiie.WebApiUtils.BusinessManager
82 102
         /// <returns>The object that match</returns>
83 103
         public static OpResult<TDbObject> GetSingleDbObject(DbSet<TDbObject> table, Expression<Func<TDbObject, bool>> predicate)
84 104
         {
85
-            var o = table.FirstOrDefault(predicate);
105
+            var o = table.Where(GetGlobalSelectPredicateStatic()).FirstOrDefault(predicate);
86 106
             if (o == null)
87 107
             {
88 108
                 return OpResult<TDbObject>.Error(ResultStatus.NotFound, typeof(TThis).Name + ": Value not found", "");
@@ -140,7 +160,8 @@ namespace iiie.WebApiUtils.BusinessManager
140 160
             return Execute((db, table) =>
141 161
             {
142 162
                 var count = table.Count(predicate);
143
-                var results = table.OrderBy(orderBy).Where(predicate).Skip(page * perPage).Take(perPage).Select(DbToDboGetStatic).ToList();
163
+                var results = table.OrderBy(orderBy).Where(GetGlobalSelectPredicateStatic())
164
+                    .Where(predicate).Skip(page * perPage).Take(perPage).Select(DbToDboGetStatic).ToList();
144 165
                 var result = new DboGetMultiple<TDboGet>
145 166
                 {
146 167
                     Count = count,

Loading…
取消
儲存