|
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
3
|
3
|
using System.Data.Entity;
|
4
|
4
|
using System.Linq;
|
5
|
5
|
using System.Linq.Expressions;
|
6
|
|
-using iiie.Logs.DataAccess;
|
7
|
6
|
using iiie.Logs.DBO;
|
8
|
7
|
using iiie.WebApiUtils.DBO;
|
9
|
8
|
|
|
@@ -31,7 +30,7 @@ namespace iiie.WebApiUtils.BusinessManager
|
31
|
30
|
/// </summary>
|
32
|
31
|
/// <param name="obj">The object to be converted</param>
|
33
|
32
|
/// <returns>The DB object</returns>
|
34
|
|
- public abstract TDbObject DboAddToDb(TDboAdd obj);
|
|
33
|
+ public abstract OpResult<TDbObject> DboAddToDb(TDboAdd obj);
|
35
|
34
|
|
36
|
35
|
/// <summary>
|
37
|
36
|
/// Convert a DBO to DB object
|
|
@@ -39,7 +38,7 @@ namespace iiie.WebApiUtils.BusinessManager
|
39
|
38
|
/// <param name="obj">The object to be converted</param>
|
40
|
39
|
/// <param name="edit">The object beeing edited</param>
|
41
|
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
|
43
|
/// <summary>
|
45
|
44
|
/// Helper to convert DB object to DBO
|
|
@@ -57,7 +56,7 @@ namespace iiie.WebApiUtils.BusinessManager
|
57
|
56
|
/// </summary>
|
58
|
57
|
/// <param name="res">The result to convert</param>
|
59
|
58
|
/// <returns>The DB object</returns>
|
60
|
|
- public static TDbObject DboAddToDbStatic(TDboAdd res)
|
|
59
|
+ public static OpResult<TDbObject> DboAddToDbStatic(TDboAdd res)
|
61
|
60
|
{
|
62
|
61
|
var obj = new TThis();
|
63
|
62
|
return obj.DboAddToDb(res);
|
|
@@ -69,7 +68,7 @@ namespace iiie.WebApiUtils.BusinessManager
|
69
|
68
|
/// <param name="res">The result to convert</param>
|
70
|
69
|
/// <param name="edit">The object beeing edited</param>
|
71
|
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
|
73
|
var obj = new TThis();
|
75
|
74
|
return obj.DboEditToDb(res, edit);
|
|
@@ -169,17 +168,55 @@ namespace iiie.WebApiUtils.BusinessManager
|
169
|
168
|
/// Add an entry in the database
|
170
|
169
|
/// </summary>
|
171
|
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
|
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
|
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
|
220
|
/// <summary>
|
184
|
221
|
/// Edit an entry in the database
|
185
|
222
|
/// </summary>
|
|
@@ -194,7 +231,9 @@ namespace iiie.WebApiUtils.BusinessManager
|
194
|
231
|
if (!edit)
|
195
|
232
|
return edit.To<bool>();
|
196
|
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
|
237
|
db.SaveChanges();
|
199
|
238
|
return OpResult<bool>.Ok(true);
|
200
|
239
|
});
|