|
@@ -0,0 +1,262 @@
|
|
1
|
+using System;
|
|
2
|
+using System.Collections.Generic;
|
|
3
|
+using Luticate2.Utils.Dbo;
|
|
4
|
+using Luticate2.Utils.Interfaces;
|
|
5
|
+
|
|
6
|
+namespace Luticate2.Utils.Business
|
|
7
|
+{
|
|
8
|
+ public abstract class LuCrudBusiness<TDataAccess, TDboCreate, TDboRead, TDboUpdate> : LuBusiness, ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
|
|
9
|
+ where TDataAccess : ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
|
|
10
|
+ where TDboCreate : class
|
|
11
|
+ where TDboRead : class
|
|
12
|
+ where TDboUpdate : class
|
|
13
|
+ {
|
|
14
|
+ protected readonly TDataAccess Db;
|
|
15
|
+
|
|
16
|
+ protected LuCrudBusiness(TDataAccess db)
|
|
17
|
+ {
|
|
18
|
+ Db = db;
|
|
19
|
+ }
|
|
20
|
+
|
|
21
|
+ protected virtual LuResult<TDboCreate> CheckAdd(TDboCreate obj)
|
|
22
|
+ {
|
|
23
|
+ return LuResult<TDboCreate>.Ok(obj);
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ protected virtual LuResult<IEnumerable<TDboCreate>> CheckAdd(IEnumerable<TDboCreate> objs)
|
|
27
|
+ {
|
|
28
|
+ var list = new List<TDboCreate>();
|
|
29
|
+ foreach (var obj in objs)
|
|
30
|
+ {
|
|
31
|
+ var res = CheckAdd(obj);
|
|
32
|
+ if (!res)
|
|
33
|
+ {
|
|
34
|
+ return res.To<IEnumerable<TDboCreate>>();
|
|
35
|
+ }
|
|
36
|
+ list.Add(res.Data);
|
|
37
|
+ }
|
|
38
|
+ return LuResult<IEnumerable<TDboCreate>>.Ok(list);
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ protected virtual LuResult<TDboUpdate> CheckEdit(TDboRead dbo, TDboUpdate update)
|
|
42
|
+ {
|
|
43
|
+ return LuResult<TDboUpdate>.Ok(update);
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ protected LuResult<TDboUpdate> GetAndCheckEdit(string id, TDboUpdate update)
|
|
47
|
+ {
|
|
48
|
+ var res = GetSingleById(id);
|
|
49
|
+ if (!res)
|
|
50
|
+ {
|
|
51
|
+ return res.To<TDboUpdate>();
|
|
52
|
+ }
|
|
53
|
+ return CheckEdit(res.Data, update);
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ protected LuResult<TDboUpdate> GetAndCheckEdit(long id, TDboUpdate update)
|
|
57
|
+ {
|
|
58
|
+ var res = GetSingleById(id);
|
|
59
|
+ if (!res)
|
|
60
|
+ {
|
|
61
|
+ return res.To<TDboUpdate>();
|
|
62
|
+ }
|
|
63
|
+ return CheckEdit(res.Data, update);
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+ public LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
|
|
69
|
+ {
|
|
70
|
+ var res = CheckAdd(objs);
|
|
71
|
+ if (!res)
|
|
72
|
+ {
|
|
73
|
+ return res.To<T>();
|
|
74
|
+ }
|
|
75
|
+ return Db.Add(res.Data, returnFunc);
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ public LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
|
|
79
|
+ {
|
|
80
|
+ var res = CheckAdd(obj);
|
|
81
|
+ if (!res)
|
|
82
|
+ {
|
|
83
|
+ return res.To<T>();
|
|
84
|
+ }
|
|
85
|
+ return Db.Add(res.Data, returnFunc);
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ public LuResult<IEnumerable<string>> AddGuid(IEnumerable<TDboCreate> objs)
|
|
89
|
+ {
|
|
90
|
+ var res = CheckAdd(objs);
|
|
91
|
+ if (!res)
|
|
92
|
+ {
|
|
93
|
+ return res.To<IEnumerable<string>>();
|
|
94
|
+ }
|
|
95
|
+ return Db.AddGuid(res.Data);
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ public LuResult<string> AddGuid(TDboCreate obj)
|
|
99
|
+ {
|
|
100
|
+ var res = CheckAdd(obj);
|
|
101
|
+ if (!res)
|
|
102
|
+ {
|
|
103
|
+ return res.To<string>();
|
|
104
|
+ }
|
|
105
|
+ return Db.AddGuid(res.Data);
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ public LuResult<IEnumerable<long>> AddId(IEnumerable<TDboCreate> obj)
|
|
109
|
+ {
|
|
110
|
+ var res = CheckAdd(obj);
|
|
111
|
+ if (!res)
|
|
112
|
+ {
|
|
113
|
+ return res.To<IEnumerable<long>>();
|
|
114
|
+ }
|
|
115
|
+ return Db.AddId(res.Data);
|
|
116
|
+ }
|
|
117
|
+
|
|
118
|
+ public LuResult<long> AddId(TDboCreate obj)
|
|
119
|
+ {
|
|
120
|
+ var res = CheckAdd(obj);
|
|
121
|
+ if (!res)
|
|
122
|
+ {
|
|
123
|
+ return res.To<long>();
|
|
124
|
+ }
|
|
125
|
+ return Db.AddId(res.Data);
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ public LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
|
|
129
|
+ {
|
|
130
|
+ var res = CheckAdd(obj);
|
|
131
|
+ if (!res)
|
|
132
|
+ {
|
|
133
|
+ return res.To<IEnumerable<TDboRead>>();
|
|
134
|
+ }
|
|
135
|
+ return Db.AddDbo(res.Data);
|
|
136
|
+ }
|
|
137
|
+
|
|
138
|
+ public LuResult<TDboRead> AddDbo(TDboCreate obj)
|
|
139
|
+ {
|
|
140
|
+ var res = CheckAdd(obj);
|
|
141
|
+ if (!res)
|
|
142
|
+ {
|
|
143
|
+ return res.To<TDboRead>();
|
|
144
|
+ }
|
|
145
|
+ return Db.AddDbo(res.Data);
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+ public LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
|
|
152
|
+ {
|
|
153
|
+ return Db.GetSingleByKeys(keys);
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ public LuResult<TDboRead> GetSingleById(string id)
|
|
157
|
+ {
|
|
158
|
+ return Db.GetSingleById(id);
|
|
159
|
+ }
|
|
160
|
+
|
|
161
|
+ public LuResult<TDboRead> GetSingleById(long id)
|
|
162
|
+ {
|
|
163
|
+ return Db.GetSingleById(id);
|
|
164
|
+ }
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+ public LuResult<T> EditSingleById<T>(long id, TDboUpdate update, Func<TDboRead, T> returnFunc)
|
|
170
|
+ {
|
|
171
|
+ var obj = GetAndCheckEdit(id, update);
|
|
172
|
+ if (!obj)
|
|
173
|
+ {
|
|
174
|
+ return obj.To<T>();
|
|
175
|
+ }
|
|
176
|
+ return Db.EditSingleById(id, obj.Data, returnFunc);
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+ public LuResult<long> EditSingleByIdId(long id, TDboUpdate update)
|
|
180
|
+ {
|
|
181
|
+ var obj = GetAndCheckEdit(id, update);
|
|
182
|
+ if (!obj)
|
|
183
|
+ {
|
|
184
|
+ return obj.To<long>();
|
|
185
|
+ }
|
|
186
|
+ return Db.EditSingleByIdId(id, obj.Data);
|
|
187
|
+ }
|
|
188
|
+
|
|
189
|
+ public LuResult<TDboRead> EditSingleByIdDbo(long id, TDboUpdate update)
|
|
190
|
+ {
|
|
191
|
+ var obj = GetAndCheckEdit(id, update);
|
|
192
|
+ if (!obj)
|
|
193
|
+ {
|
|
194
|
+ return obj.To<TDboRead>();
|
|
195
|
+ }
|
|
196
|
+ return Db.EditSingleByIdDbo(id, obj.Data);
|
|
197
|
+ }
|
|
198
|
+
|
|
199
|
+ public LuResult<T> EditSingleById<T>(string id, TDboUpdate update, Func<TDboRead, T> returnFunc)
|
|
200
|
+ {
|
|
201
|
+ var obj = GetAndCheckEdit(id, update);
|
|
202
|
+ if (!obj)
|
|
203
|
+ {
|
|
204
|
+ return obj.To<T>();
|
|
205
|
+ }
|
|
206
|
+ return Db.EditSingleById(id, obj.Data, returnFunc);
|
|
207
|
+ }
|
|
208
|
+
|
|
209
|
+ public LuResult<string> EditSingleByIdGuid(string id, TDboUpdate update)
|
|
210
|
+ {
|
|
211
|
+ var obj = GetAndCheckEdit(id, update);
|
|
212
|
+ if (!obj)
|
|
213
|
+ {
|
|
214
|
+ return obj.To<string>();
|
|
215
|
+ }
|
|
216
|
+ return Db.EditSingleByIdGuid(id, obj.Data);
|
|
217
|
+ }
|
|
218
|
+
|
|
219
|
+ public LuResult<TDboRead> EditSingleByIdDbo(string id, TDboUpdate update)
|
|
220
|
+ {
|
|
221
|
+ var obj = GetAndCheckEdit(id, update);
|
|
222
|
+ if (!obj)
|
|
223
|
+ {
|
|
224
|
+ return obj.To<TDboRead>();
|
|
225
|
+ }
|
|
226
|
+ return Db.EditSingleByIdDbo(id, obj.Data);
|
|
227
|
+ }
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+ public LuResult<T> DeleteSingleById<T>(string id, Func<TDboRead, T> returnFunc)
|
|
233
|
+ {
|
|
234
|
+ return Db.DeleteSingleById(id, returnFunc);
|
|
235
|
+ }
|
|
236
|
+
|
|
237
|
+ public LuResult<string> DeleteSingleByIdGuid(string id)
|
|
238
|
+ {
|
|
239
|
+ return Db.DeleteSingleByIdGuid(id);
|
|
240
|
+ }
|
|
241
|
+
|
|
242
|
+ public LuResult<TDboRead> DeleteSingleByIdDbo(string id)
|
|
243
|
+ {
|
|
244
|
+ return Db.DeleteSingleByIdDbo(id);
|
|
245
|
+ }
|
|
246
|
+
|
|
247
|
+ public LuResult<T> DeleteSingleById<T>(long id, Func<TDboRead, T> returnFunc)
|
|
248
|
+ {
|
|
249
|
+ return Db.DeleteSingleById(id, returnFunc);
|
|
250
|
+ }
|
|
251
|
+
|
|
252
|
+ public LuResult<long> DeleteSingleByIdId(long id)
|
|
253
|
+ {
|
|
254
|
+ return Db.DeleteSingleByIdId(id);
|
|
255
|
+ }
|
|
256
|
+
|
|
257
|
+ public LuResult<TDboRead> DeleteSingleByIdDbo(long id)
|
|
258
|
+ {
|
|
259
|
+ return Db.DeleteSingleByIdDbo(id);
|
|
260
|
+ }
|
|
261
|
+ }
|
|
262
|
+}
|