Browse Source

[WebApiUtils] Add can return data (eg: id); add / edit can cancel operation (eg: invalid data)

feature/authentication-tests
Robin Thoni 9 years ago
parent
commit
faa65d21b0
1 changed files with 49 additions and 10 deletions
  1. 49
    10
      WebApiUtils/BusinessManager/SqlServerManager.cs

+ 49
- 10
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 iiie.Logs.DataAccess;
7
 using iiie.Logs.DBO;
6
 using iiie.Logs.DBO;
8
 using iiie.WebApiUtils.DBO;
7
 using iiie.WebApiUtils.DBO;
9
 
8
 
31
         /// </summary>
30
         /// </summary>
32
         /// <param name="obj">The object to be converted</param>
31
         /// <param name="obj">The object to be converted</param>
33
         /// <returns>The DB object</returns>
32
         /// <returns>The DB object</returns>
34
-        public abstract TDbObject DboAddToDb(TDboAdd obj);
33
+        public abstract OpResult<TDbObject> DboAddToDb(TDboAdd obj);
35
 
34
 
36
         /// <summary>
35
         /// <summary>
37
         /// Convert a DBO to DB object
36
         /// Convert a DBO to DB object
39
         /// <param name="obj">The object to be converted</param>
38
         /// <param name="obj">The object to be converted</param>
40
         /// <param name="edit">The object beeing edited</param>
39
         /// <param name="edit">The object beeing edited</param>
41
         /// <returns>The DB object</returns>
40
         /// <returns>The DB object</returns>
42
-        public abstract TDbObject DboEditToDb(TDboEdit obj, TDbObject edit);
41
+        public abstract OpResult<TDbObject> DboEditToDb(TDboEdit obj, TDbObject edit);
43
 
42
 
44
         /// <summary>
43
         /// <summary>
45
         /// Helper to convert DB object to DBO
44
         /// Helper to convert DB object to DBO
57
         /// </summary>
56
         /// </summary>
58
         /// <param name="res">The result to convert</param>
57
         /// <param name="res">The result to convert</param>
59
         /// <returns>The DB object</returns>
58
         /// <returns>The DB object</returns>
60
-        public static TDbObject DboAddToDbStatic(TDboAdd res)
59
+        public static OpResult<TDbObject> DboAddToDbStatic(TDboAdd res)
61
         {
60
         {
62
             var obj = new TThis();
61
             var obj = new TThis();
63
             return obj.DboAddToDb(res);
62
             return obj.DboAddToDb(res);
69
         /// <param name="res">The result to convert</param>
68
         /// <param name="res">The result to convert</param>
70
         /// <param name="edit">The object beeing edited</param>
69
         /// <param name="edit">The object beeing edited</param>
71
         /// <returns>The DB object</returns>
70
         /// <returns>The DB object</returns>
72
-        public static TDbObject DboEditToDbStatic(TDboEdit res, TDbObject edit)
71
+        public static OpResult<TDbObject> DboEditToDbStatic(TDboEdit res, TDbObject edit)
73
         {
72
         {
74
             var obj = new TThis();
73
             var obj = new TThis();
75
             return obj.DboEditToDb(res, edit);
74
             return obj.DboEditToDb(res, edit);
169
         /// Add an entry in the database
168
         /// Add an entry in the database
170
         /// </summary>
169
         /// </summary>
171
         /// <param name="obj">The object to be added</param>
170
         /// <param name="obj">The object to be added</param>
172
-        /// <returns>Always true or an OpResult error</returns>
173
-        public static OpResult<bool> Add(TDboAdd obj)
171
+        /// <param name="returnFunc">Used to return some data</param>
172
+        /// <returns>The result defined by returnFunc</returns>
173
+        public static OpResult<T> Add<T>(TDboAdd obj, Expression<Func<TDbObject, T>> returnFunc)
174
         {
174
         {
175
             return Execute((db, table) =>
175
             return Execute((db, table) =>
176
             {
176
             {
177
-                table.Add(DboAddToDbStatic(obj));
177
+                var item = DboAddToDbStatic(obj);
178
+                if (!item)
179
+                    return item.To<T>();
180
+                table.Add(item.Data);
178
                 db.SaveChanges();
181
                 db.SaveChanges();
179
-                return OpResult<bool>.Ok(true);
182
+                var res = returnFunc.Compile().DynamicInvoke(item.Data);
183
+                return OpResult<T>.Ok((T)res);
180
             });
184
             });
181
         }
185
         }
182
 
186
 
187
+        /// <summary>
188
+        /// Add an entry in the database
189
+        /// </summary>
190
+        /// <param name="obj">The object to be added</param>
191
+        /// <returns>Always true or an OpResult error</returns>
192
+        public static OpResult<bool> Add(TDboAdd obj)
193
+        {
194
+            return Add(obj, x => true);
195
+        }
196
+
197
+        /// <summary>
198
+        /// Add an entry in the database
199
+        /// </summary>
200
+        /// <param name="obj">The object to be added</param>
201
+        /// <returns>The DBO or an OpResult error</returns>
202
+        public static OpResult<TDboGet> AddDbo(TDboAdd obj)
203
+        {
204
+            return Add(obj, x => DbToDboGetStatic(x));
205
+        }
206
+
207
+        /// <summary>
208
+        /// Add an entry in the database
209
+        /// </summary>
210
+        /// <param name="obj">The object to be added</param>
211
+        /// <returns>The DBO or an OpResult error</returns>
212
+        public static OpResult<long> AddId(TDboAdd obj)
213
+        {
214
+            var param = Expression.Parameter(typeof(TDbObject), "x");
215
+            var exp = Expression.Property(param, "id");
216
+            var lambda = Expression.Lambda<Func<TDbObject, long>>(exp, param);
217
+            return Add(obj, lambda);
218
+        }
219
+
183
         /// <summary>
220
         /// <summary>
184
         /// Edit an entry in the database
221
         /// Edit an entry in the database
185
         /// </summary>
222
         /// </summary>
194
                 if (!edit)
231
                 if (!edit)
195
                     return edit.To<bool>();
232
                     return edit.To<bool>();
196
                 var res = DboEditToDbStatic(obj, edit.Data);
233
                 var res = DboEditToDbStatic(obj, edit.Data);
197
-                db.Entry(edit.Data).CurrentValues.SetValues(res);
234
+                if (!res)
235
+                    return res.To<bool>();
236
+                db.Entry(edit.Data).CurrentValues.SetValues(res.Data);
198
                 db.SaveChanges();
237
                 db.SaveChanges();
199
                 return OpResult<bool>.Ok(true);
238
                 return OpResult<bool>.Ok(true);
200
             });
239
             });

Loading…
Cancel
Save