Browse Source

[WebApiUtils] Added global predicate

feature/authentication-tests
Robin Thoni 9 years ago
parent
commit
410b439c44
1 changed files with 23 additions and 2 deletions
  1. 23
    2
      WebApiUtils/BusinessManager/SqlServerManager.cs

+ 23
- 2
WebApiUtils/BusinessManager/SqlServerManager.cs View File

3
 using System.Data.Entity;
3
 using System.Data.Entity;
4
 using System.Linq;
4
 using System.Linq;
5
 using System.Linq.Expressions;
5
 using System.Linq.Expressions;
6
+using System.Runtime.Serialization;
6
 using iiie.Logs.DBO;
7
 using iiie.Logs.DBO;
7
 using iiie.WebApiUtils.DBO;
8
 using iiie.WebApiUtils.DBO;
8
 
9
 
32
         /// <returns>The DB object</returns>
33
         /// <returns>The DB object</returns>
33
         public abstract OpResult<TDbObject> DboAddToDb(TDboAdd obj);
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
         /// <summary>
45
         /// <summary>
36
         /// Convert a DBO to DB object
46
         /// Convert a DBO to DB object
37
         /// </summary>
47
         /// </summary>
40
         /// <returns>The DB object</returns>
50
         /// <returns>The DB object</returns>
41
         public abstract OpResult<TDbObject> DboEditToDb(TDboEdit obj, TDbObject edit);
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
         /// <summary>
63
         /// <summary>
44
         /// Helper to convert DB object to DBO
64
         /// Helper to convert DB object to DBO
45
         /// </summary>
65
         /// </summary>
82
         /// <returns>The object that match</returns>
102
         /// <returns>The object that match</returns>
83
         public static OpResult<TDbObject> GetSingleDbObject(DbSet<TDbObject> table, Expression<Func<TDbObject, bool>> predicate)
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
             if (o == null)
106
             if (o == null)
87
             {
107
             {
88
                 return OpResult<TDbObject>.Error(ResultStatus.NotFound, typeof(TThis).Name + ": Value not found", "");
108
                 return OpResult<TDbObject>.Error(ResultStatus.NotFound, typeof(TThis).Name + ": Value not found", "");
140
             return Execute((db, table) =>
160
             return Execute((db, table) =>
141
             {
161
             {
142
                 var count = table.Count(predicate);
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
                 var result = new DboGetMultiple<TDboGet>
165
                 var result = new DboGetMultiple<TDboGet>
145
                 {
166
                 {
146
                     Count = count,
167
                     Count = count,

Loading…
Cancel
Save