Explorar el Código

added fs files crud; simplified ILuCrudInterface; tests; added LuPaginatedDbo helpers

tags/v0.5.0
Robin Thoni hace 7 años
padre
commit
004e9e48fc
Se han modificado 32 ficheros con 965 adiciones y 169 borrados
  1. 1
    1
      Luticate2.Auth/project.json
  2. 8
    79
      Luticate2.Utils/Business/LuCrudBusiness.cs
  3. 2
    1
      Luticate2.Utils/Business/LuNotificationsBusiness.cs
  4. 2
    3
      Luticate2.Utils/Controllers/LuCrudController.cs
  5. 59
    0
      Luticate2.Utils/Controllers/LuFsFilesCrudController.cs
  6. 18
    1
      Luticate2.Utils/Controllers/LuUtilsExtensions.cs
  7. 8
    39
      Luticate2.Utils/DataAccess/LuEfCrudDataAccess.cs
  8. 180
    0
      Luticate2.Utils/DataAccess/LuFsFilesCrudDataAccess.cs
  9. 21
    1
      Luticate2.Utils/Dbo/Basic/LuPaginatedDbo.cs
  10. 11
    0
      Luticate2.Utils/Dbo/FsFiles/LuFsFilesAddDbo.cs
  11. 17
    0
      Luticate2.Utils/Dbo/FsFiles/LuFsFilesDbo.cs
  12. 2
    0
      Luticate2.Utils/Dbo/Result/LuStatus.cs
  13. 3
    35
      Luticate2.Utils/Interfaces/ILuCrudInterface.cs
  14. 17
    0
      Luticate2.Utils/Interfaces/ILuNotificationsBusiness.cs
  15. 82
    0
      Luticate2.Utils/Utils/LuCrudInterfaceExtensions.cs
  16. 61
    0
      Luticate2.Utils/Utils/LuExtensions.cs
  17. 19
    0
      TestUtils/DataAccess/LuTmpFsFilesCrudDataAccess.cs
  18. 4
    0
      TestUtils/DataAccess/LuUtilsPkBigSerialDataAccess.cs
  19. 4
    0
      TestUtils/DataAccess/LuUtilsPkGuidDataAccess.cs
  20. 1
    0
      TestUtils/EfCrudDataAccess/LuEfCreateDataAccessTest.cs
  21. 1
    0
      TestUtils/EfCrudDataAccess/LuEfDeleteDataAccessTest.cs
  22. 1
    0
      TestUtils/EfCrudDataAccess/LuEfReadDataAccessTest.cs
  23. 2
    1
      TestUtils/EfCrudDataAccess/LuEfTransactionScopeTest.cs
  24. 1
    0
      TestUtils/EfCrudDataAccess/LuEfUpdateDataAccessTest.cs
  25. 361
    0
      TestUtils/EfCrudDataAccess/LuFsFilesCrudDataAccessTest.cs
  26. 22
    4
      TestUtils/Tests.cs
  27. 1
    1
      TestUtils/project.json
  28. 14
    0
      WebApiUtils/Business/UploadBusiness.cs
  29. 14
    0
      WebApiUtils/Controllers/UploadController.cs
  30. 12
    0
      WebApiUtils/DataAccess/UploadDataAccess.cs
  31. 15
    3
      WebApiUtils/Startup.cs
  32. 1
    0
      WebApiUtils/project.json

+ 1
- 1
Luticate2.Auth/project.json Ver fichero

@@ -4,7 +4,7 @@
4 4
         "debugType": "portable"
5 5
     },
6 6
     "dependencies": {
7
-        "Luticate2.Utils": "0.3.*"
7
+        "Luticate2.Utils": "0.4.*"
8 8
     },
9 9
     "frameworks": {
10 10
         "netcoreapp1.0": {

+ 8
- 79
Luticate2.Utils/Business/LuCrudBusiness.cs Ver fichero

@@ -1,11 +1,9 @@
1 1
 using System;
2
-using System.Collections.Generic;
3
-using System.Linq;
4 2
 using Luticate2.Utils.Dbo.Basic;
5
-using Luticate2.Utils.Dbo.Filter;
6
-using Luticate2.Utils.Dbo.OrderBy;
3
+using Luticate2.Utils.Dbo.PaginatedRequest;
7 4
 using Luticate2.Utils.Dbo.Result;
8 5
 using Luticate2.Utils.Interfaces;
6
+using Luticate2.Utils.Utils;
9 7
 
10 8
 namespace Luticate2.Utils.Business
11 9
 {
@@ -19,7 +17,7 @@ namespace Luticate2.Utils.Business
19 17
     {
20 18
         protected readonly TDataAccess DataAccess;
21 19
 
22
-        private readonly LuNotificationsBusiness _notificationsBusiness;
20
+        private readonly ILuNotificationsBusiness _notificationsBusiness;
23 21
 
24 22
         protected virtual string EntityType { get; set; }
25 23
 
@@ -29,7 +27,7 @@ namespace Luticate2.Utils.Business
29 27
 
30 28
         protected virtual Func<string, TDboRead, bool> NotifyDeleteFilter { get; set; }
31 29
 
32
-        protected LuCrudBusiness(TDataAccess dataAccess, LuNotificationsBusiness notificationsBusiness)
30
+        protected LuCrudBusiness(TDataAccess dataAccess, ILuNotificationsBusiness notificationsBusiness)
33 31
         {
34 32
             DataAccess = dataAccess;
35 33
             _notificationsBusiness = notificationsBusiness;
@@ -40,21 +38,6 @@ namespace Luticate2.Utils.Business
40 38
             return LuResult<TDboCreate>.Ok(obj);
41 39
         }
42 40
 
43
-        protected virtual LuResult<IEnumerable<TDboCreate>> CheckAdd(IEnumerable<TDboCreate> objs)
44
-        {
45
-            var list = new List<TDboCreate>();
46
-            foreach (var obj in objs)
47
-            {
48
-                var res = CheckAdd(obj);
49
-                if (!res)
50
-                {
51
-                    return res.To<IEnumerable<TDboCreate>>();
52
-                }
53
-                list.Add(res.Data);
54
-            }
55
-            return LuResult<IEnumerable<TDboCreate>>.Ok(list);
56
-        }
57
-
58 41
         protected virtual LuResult<TDboUpdate> CheckEdit(TDboRead dbo, TDboUpdate update)
59 42
         {
60 43
             return LuResult<TDboUpdate>.Ok(update);
@@ -71,16 +54,9 @@ namespace Luticate2.Utils.Business
71 54
         }
72 55
 
73 56
 
74
-
75
-
76
-        public virtual LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
57
+        public TId GetDboId(TDboRead obj)
77 58
         {
78
-            var res = CheckAdd(objs);
79
-            if (!res)
80
-            {
81
-                return res.To<T>();
82
-            }
83
-            return DataAccess.Add(res.Data, returnFunc);
59
+            return DataAccess.GetDboId(obj);
84 60
         }
85 61
 
86 62
         public virtual LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
@@ -100,26 +76,6 @@ namespace Luticate2.Utils.Business
100 76
             return newEntity.To<T>();
101 77
         }
102 78
 
103
-        public virtual LuResult<IEnumerable<TId>> AddId(IEnumerable<TDboCreate> objs)
104
-        {
105
-            return Add(objs, reads => reads.Select(read => (TId)((dynamic)read).Id));
106
-        }
107
-
108
-        public virtual LuResult<TId> AddId(TDboCreate obj)
109
-        {
110
-            return Add(obj, read => (TId)((dynamic)read).Id);
111
-        }
112
-
113
-        public virtual LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> objs)
114
-        {
115
-            return Add(objs, reads => reads);
116
-        }
117
-
118
-        public virtual LuResult<TDboRead> AddDbo(TDboCreate obj)
119
-        {
120
-            return Add(obj, read => read);
121
-        }
122
-
123 79
 
124 80
 
125 81
 
@@ -129,16 +85,9 @@ namespace Luticate2.Utils.Business
129 85
         }
130 86
 
131 87
 
132
-        public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, LuFilterDbo filter,
133
-            int page = 0, int perPage = int.MaxValue)
134
-        {
135
-            return DataAccess.GetMultiple(orderBy, filter, page, perPage);
136
-        }
137
-
138
-        public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0,
139
-            int perPage = int.MaxValue)
88
+        public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuPaginatedRequestDbo request)
140 89
         {
141
-            return GetMultiple(orderBy, new LuFilterDbo { Query = ""}, page, perPage);
90
+            return DataAccess.GetMultiple(request);
142 91
         }
143 92
 
144 93
 
@@ -167,16 +116,6 @@ namespace Luticate2.Utils.Business
167 116
             return editedEntity.To<T>();
168 117
         }
169 118
 
170
-        public virtual LuResult<TId> EditSingleByIdId(TId id, TDboUpdate update)
171
-        {
172
-            return EditSingleById(id, update, read => (TId)((dynamic)read).Id);
173
-        }
174
-
175
-        public virtual LuResult<TDboRead> EditSingleByIdDbo(TId id, TDboUpdate update)
176
-        {
177
-            return EditSingleById(id, update, read => read);
178
-        }
179
-
180 119
 
181 120
 
182 121
 
@@ -195,15 +134,5 @@ namespace Luticate2.Utils.Business
195 134
             }
196 135
             return res;
197 136
         }
198
-
199
-        public virtual LuResult<TId> DeleteSingleByIdId(TId id)
200
-        {
201
-            return DeleteSingleById(id, read => (TId)((dynamic)read).Id);
202
-        }
203
-
204
-        public virtual LuResult<TDboRead> DeleteSingleByIdDbo(TId id)
205
-        {
206
-            return DeleteSingleById(id, read => read);
207
-        }
208 137
     }
209 138
 }

+ 2
- 1
Luticate2.Utils/Business/LuNotificationsBusiness.cs Ver fichero

@@ -1,12 +1,13 @@
1 1
 using System;
2 2
 using System.Linq;
3 3
 using Luticate2.Utils.Hubs;
4
+using Luticate2.Utils.Interfaces;
4 5
 using Microsoft.AspNetCore.SignalR;
5 6
 using Microsoft.AspNetCore.SignalR.Infrastructure;
6 7
 
7 8
 namespace Luticate2.Utils.Business
8 9
 {
9
-    public class LuNotificationsBusiness
10
+    public class LuNotificationsBusiness : ILuNotificationsBusiness
10 11
     {
11 12
         private readonly LuHubConnectionTracker _connectionTracker;
12 13
         private readonly IHubContext _hubContext;

+ 2
- 3
Luticate2.Utils/Controllers/LuCrudController.cs Ver fichero

@@ -1,9 +1,8 @@
1 1
 using System.ComponentModel.DataAnnotations;
2 2
 using Luticate2.Utils.Dbo.Basic;
3
-using Luticate2.Utils.Dbo.Filter;
4
-using Luticate2.Utils.Dbo.OrderBy;
5 3
 using Luticate2.Utils.Dbo.PaginatedRequest;
6 4
 using Luticate2.Utils.Interfaces;
5
+using Luticate2.Utils.Utils;
7 6
 using Microsoft.AspNetCore.Mvc;
8 7
 using Microsoft.Extensions.Options;
9 8
 
@@ -33,7 +32,7 @@ namespace Luticate2.Utils.Controllers
33 32
         [Route("[controller]")]
34 33
         public virtual LuApiWrapperDbo<LuPaginatedDbo<TDboRead>> GetMultiple([Required]LuPaginatedRequestDbo request)
35 34
         {
36
-            return Handle(Busines.GetMultiple(request.OrderBy, request.Filter, request.Page, request.PerPage));
35
+            return Handle(Busines.GetMultiple(request));
37 36
         }
38 37
 
39 38
         [HttpPost]

+ 59
- 0
Luticate2.Utils/Controllers/LuFsFilesCrudController.cs Ver fichero

@@ -0,0 +1,59 @@
1
+using System.ComponentModel.DataAnnotations;
2
+using Luticate2.Utils.Dbo.Basic;
3
+using Luticate2.Utils.Dbo.FsFiles;
4
+using Luticate2.Utils.Dbo.PaginatedRequest;
5
+using Luticate2.Utils.Interfaces;
6
+using Luticate2.Utils.Utils;
7
+using Microsoft.AspNetCore.Http;
8
+using Microsoft.AspNetCore.Mvc;
9
+using Microsoft.Extensions.Options;
10
+
11
+namespace Luticate2.Utils.Controllers
12
+{
13
+    public class LuFsFilesCrudController<TBusiness> : LuController
14
+        where TBusiness : ILuCrudInterface<LuFsFilesAddDbo, LuFsFilesDbo, LuFsFilesAddDbo, string>
15
+    {
16
+        private readonly TBusiness _business;
17
+
18
+        public LuFsFilesCrudController(IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo, TBusiness business) : base(luUtilsOptionsDbo)
19
+        {
20
+            _business = business;
21
+        }
22
+
23
+        [HttpPost]
24
+        [Route("[controller]")]
25
+        public LuApiWrapperDbo<LuFsFilesDbo> AddDbo([Required]IFormFile file, string path)
26
+        {
27
+            return Handle(_business.AddDbo(new LuFsFilesAddDbo
28
+            {
29
+                File = file.OpenReadStream(),
30
+                Path = string.IsNullOrEmpty(path) ? file.FileName : path
31
+            }));
32
+        }
33
+
34
+        [HttpGet]
35
+        [Route("[controller]")]
36
+        public virtual LuApiWrapperDbo<LuPaginatedDbo<LuFsFilesDbo>> GetMultiple([Required]LuPaginatedRequestDbo request)
37
+        {
38
+            return Handle(_business.GetMultiple(request));
39
+        }
40
+
41
+        [HttpPost]
42
+        [Route("[controller]/{id}")]
43
+        public virtual LuApiWrapperDbo<LuFsFilesDbo> EditSingleByIdDbo([Required]string id, IFormFile file, string path)
44
+        {
45
+            return Handle(_business.EditSingleByIdDbo(id, new LuFsFilesAddDbo
46
+            {
47
+                File = file?.OpenReadStream(),
48
+                Path = path
49
+            }));
50
+        }
51
+
52
+        [HttpDelete]
53
+        [Route("[controller]/{id}")]
54
+        public virtual LuApiWrapperDbo<LuFsFilesDbo> Delete([Required]string id)
55
+        {
56
+            return Handle(_business.DeleteSingleByIdDbo(id));
57
+        }
58
+    }
59
+}

+ 18
- 1
Luticate2.Utils/Controllers/LuUtilsExtensions.cs Ver fichero

@@ -9,6 +9,7 @@ using Luticate2.Utils.Dbo.OrderBy;
9 9
 using Luticate2.Utils.Dbo.PaginatedRequest;
10 10
 using Luticate2.Utils.Dbo.Result;
11 11
 using Luticate2.Utils.Hubs;
12
+using Luticate2.Utils.Interfaces;
12 13
 using Luticate2.Utils.Middlewares;
13 14
 using Microsoft.AspNetCore.Builder;
14 15
 using Microsoft.AspNetCore.Http;
@@ -39,7 +40,7 @@ namespace Luticate2.Utils.Controllers
39 40
 
40 41
             services.AddScoped<LuEfTransactionScope>();
41 42
             services.AddSingleton<LuHubConnectionTracker>();
42
-            services.AddSingleton<LuNotificationsBusiness>();
43
+            services.AddSingleton<ILuNotificationsBusiness, LuNotificationsBusiness>();
43 44
             services.Configure(optionsDelegate);
44 45
             return services;
45 46
         }
@@ -100,6 +101,14 @@ namespace Luticate2.Utils.Controllers
100 101
             {
101 102
                 return 500;
102 103
             }
104
+            if (result.Status == LuStatus.FsError)
105
+            {
106
+                return 500;
107
+            }
108
+            if (result.Status == LuStatus.BackendError)
109
+            {
110
+                return 500;
111
+            }
103 112
             if (result.Status == LuStatus.InternalError)
104 113
             {
105 114
                 return 500;
@@ -133,6 +142,14 @@ namespace Luticate2.Utils.Controllers
133 142
             {
134 143
                 return "Internal Error";
135 144
             }
145
+            if (result.Status == LuStatus.FsError)
146
+            {
147
+                return "Internal Error";
148
+            }
149
+            if (result.Status == LuStatus.BackendError)
150
+            {
151
+                return "Internal Error";
152
+            }
136 153
             if (result.Status == LuStatus.InternalError)
137 154
             {
138 155
                 return "Internal Error";

+ 8
- 39
Luticate2.Utils/DataAccess/LuEfCrudDataAccess.cs Ver fichero

@@ -5,6 +5,7 @@ using System.Linq.Expressions;
5 5
 using Luticate2.Utils.Dbo.Basic;
6 6
 using Luticate2.Utils.Dbo.Filter;
7 7
 using Luticate2.Utils.Dbo.OrderBy;
8
+using Luticate2.Utils.Dbo.PaginatedRequest;
8 9
 using Luticate2.Utils.Dbo.Result;
9 10
 using Luticate2.Utils.Interfaces;
10 11
 using Luticate2.Utils.Utils;
@@ -91,6 +92,11 @@ namespace Luticate2.Utils.DataAccess
91 92
             return table;
92 93
         }
93 94
 
95
+        public TId GetDboId(TDboRead obj)
96
+        {
97
+            return GetIdFunc<TId>()(obj);
98
+        }
99
+
94 100
         protected virtual Func<TDboRead, T> GetIdFunc<T>()
95 101
         {
96 102
             return x => ((dynamic) x).Id;
@@ -160,22 +166,12 @@ namespace Luticate2.Utils.DataAccess
160 166
             return Add(objs, list => list.Select(func));
161 167
         }
162 168
 
163
-        public virtual LuResult<TId> AddId(TDboCreate obj)
164
-        {
165
-            return AddId(new List<TDboCreate> {obj}).To(list => list.First());
166
-        }
167
-
168 169
 
169 170
         public virtual LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
170 171
         {
171 172
             return Add(obj, read => read);
172 173
         }
173 174
 
174
-        public virtual LuResult<TDboRead> AddDbo(TDboCreate obj)
175
-        {
176
-            return AddDbo(new List<TDboCreate> {obj}).To(list => list.First());
177
-        }
178
-
179 175
 
180 176
 
181 177
 
@@ -304,15 +300,9 @@ namespace Luticate2.Utils.DataAccess
304 300
             });
305 301
         }
306 302
 
307
-        public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, LuFilterDbo filter,
308
-            int page = 0, int perPage = int.MaxValue)
309
-        {
310
-            return GetMultiple(orderBy, (db, table) => GetFilterQuery(filter, db, table), page, perPage);
311
-        }
312
-
313
-        public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
303
+        public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuPaginatedRequestDbo request)
314 304
         {
315
-            return GetMultiple(orderBy, GetGetQueryable, page, perPage);
305
+            return GetMultiple(request.OrderBy, (db, table) => GetFilterQuery(request.Filter, db, table), request.Page, request.PerPage);
316 306
         }
317 307
 
318 308
 
@@ -418,16 +408,6 @@ namespace Luticate2.Utils.DataAccess
418 408
             return GetSingleById(id).To(returnFunc);
419 409
         }
420 410
 
421
-        public virtual LuResult<TId> EditSingleByIdId(TId id, TDboUpdate update)
422
-        {
423
-            return EditSingleById(id, update, GetIdFunc<TId>());
424
-        }
425
-
426
-        public virtual LuResult<TDboRead> EditSingleByIdDbo(TId id, TDboUpdate update)
427
-        {
428
-            return EditSingleById(id, update, read => read);
429
-        }
430
-
431 411
 
432 412
 
433 413
 
@@ -473,16 +453,5 @@ namespace Luticate2.Utils.DataAccess
473 453
             return Delete(GetExpression(new KeyValuePair<string, object>("id", guid)),
474 454
                 reads => returnFunc(reads.FirstOrDefault()));
475 455
         }
476
-
477
-        public virtual LuResult<TId> DeleteSingleByIdId(TId id)
478
-        {
479
-            var func = GetIdFunc<TId>();
480
-            return DeleteSingleById(id, func);
481
-        }
482
-
483
-        public virtual LuResult<TDboRead> DeleteSingleByIdDbo(TId id)
484
-        {
485
-            return DeleteSingleById(id, read => read);
486
-        }
487 456
     }
488 457
 }

+ 180
- 0
Luticate2.Utils/DataAccess/LuFsFilesCrudDataAccess.cs Ver fichero

@@ -0,0 +1,180 @@
1
+using System;
2
+using System.IO;
3
+using System.Linq;
4
+using Luticate2.Utils.Dbo.Basic;
5
+using Luticate2.Utils.Dbo.Filter;
6
+using Luticate2.Utils.Dbo.FsFiles;
7
+using Luticate2.Utils.Dbo.PaginatedRequest;
8
+using Luticate2.Utils.Dbo.Result;
9
+using Luticate2.Utils.Interfaces;
10
+using Luticate2.Utils.Utils;
11
+
12
+namespace Luticate2.Utils.DataAccess
13
+{
14
+    public abstract class LuFsFilesCrudDataAccess : ILuCrudInterface<LuFsFilesAddDbo, LuFsFilesDbo, LuFsFilesAddDbo, string>
15
+    {
16
+        public abstract string GetBasePath();
17
+
18
+        public string GetFullPath(string path)
19
+        {
20
+            return Path.Combine(GetBasePath(), path);
21
+        }
22
+
23
+        public bool CanCreate(string fullpath)
24
+        {
25
+            try
26
+            {
27
+                using (File.Create(fullpath))
28
+                {
29
+                }
30
+                File.Delete(fullpath);
31
+                return true;
32
+            }
33
+            catch (Exception e)
34
+            {
35
+                return false;
36
+            }
37
+        }
38
+
39
+        public static Func<FileInfo, object> GetOrderByFieldExpression(string fieldName)
40
+        {
41
+            if (fieldName == "path")
42
+            {
43
+                return x => x.Name.ToLower();
44
+            }
45
+            if (fieldName == "createdAt")
46
+            {
47
+                return x => x.LastWriteTime;
48
+            }
49
+            if (fieldName == "size")
50
+            {
51
+                return x => x.Length;
52
+            }
53
+            return null;
54
+        }
55
+
56
+        public static Func<FileInfo, bool> GetFilterExpression(LuFilterDbo filter)
57
+        {
58
+            if (filter == null)
59
+            {
60
+                return s => true;
61
+            }
62
+            return s => s.Name.ToLower().Contains(filter.Query.ToLower());
63
+        }
64
+
65
+        public virtual LuResult<T> HandleError<T>(Exception e)
66
+        {
67
+            return LuResult<T>.Error(LuStatus.FsError, e);
68
+        }
69
+
70
+        public string GetDboId(LuFsFilesDbo obj)
71
+        {
72
+            return obj.Path;
73
+        }
74
+
75
+        public LuResult<T> Add<T>(LuFsFilesAddDbo file, Func<LuFsFilesDbo, T> returnFunc)
76
+        {
77
+            var fullpath = GetFullPath(file.Path);
78
+            if (File.Exists(fullpath))
79
+            {
80
+                return LuResult<T>.Error(LuStatus.InputError, $"File: {file.Path}", "File already exist");
81
+            }
82
+            if (!CanCreate(fullpath))
83
+            {
84
+                return LuResult<T>.Error(LuStatus.FsError, $"File: {file.Path}", "File can not be created");
85
+            }
86
+            try
87
+            {
88
+                using (var fileStream = new FileStream(fullpath, FileMode.CreateNew, FileAccess.Write))
89
+                {
90
+                    file.File.CopyTo(fileStream);
91
+                }
92
+                return LuResult<T>.Ok(returnFunc(new FileInfo(fullpath).ToDbo()));
93
+            }
94
+            catch (Exception e)
95
+            {
96
+                return HandleError<T>(e);
97
+            }
98
+        }
99
+
100
+        public LuResult<LuFsFilesDbo> GetSingleById(string id)
101
+        {
102
+            var fullpath = GetFullPath(id);
103
+            if (!File.Exists(fullpath))
104
+            {
105
+                return LuResult<LuFsFilesDbo>.Error(LuStatus.NotFound, $"File {fullpath}", "File not found");
106
+            }
107
+            return LuResult<LuFsFilesDbo>.Ok(new FileInfo(fullpath).ToDbo());
108
+        }
109
+
110
+        public LuResult<LuPaginatedDbo<LuFsFilesDbo>> GetMultiple(LuPaginatedRequestDbo request)
111
+        {
112
+            try
113
+            {
114
+                var fullPath = GetBasePath();
115
+//                var fullPath = GetFullPath(request.Filter.GetFilterString("folder", ""));
116
+//                if (!Directory.Exists(fullPath))
117
+//                {
118
+//                    return LuResult<LuPaginatedDbo<LuFsFilesDbo>>.Error(LuStatus.NotFound, $"Folder: {fullPath}", "Folder not found");
119
+//                }
120
+                return LuResult<LuPaginatedDbo<LuFsFilesDbo>>.Ok(Directory.GetFiles(fullPath)
121
+                    .Select(s => new FileInfo(s))
122
+                    .Where(GetFilterExpression(request.Filter))
123
+                    .Paginate(request, GetOrderByFieldExpression)
124
+                    .Select(info => info.ToDbo()));
125
+            }
126
+            catch (Exception e)
127
+            {
128
+                return HandleError<LuPaginatedDbo<LuFsFilesDbo>>(e);
129
+            }
130
+        }
131
+
132
+        public LuResult<T> EditSingleById<T>(string id, LuFsFilesAddDbo update, Func<LuFsFilesDbo, T> returnFunc)
133
+        {
134
+            try
135
+            {
136
+                var get = GetSingleById(id);
137
+                if (!get)
138
+                {
139
+                    return get.To<T>();
140
+                }
141
+                var fullpath = GetFullPath(get.Data.Path);
142
+                if (update.Path != null)
143
+                {
144
+                    File.Move(fullpath, GetFullPath(update.Path));
145
+                    fullpath = GetFullPath(update.Path);
146
+                }
147
+                if (update.File != null)
148
+                {
149
+                    using (var fileStream = new FileStream(fullpath, FileMode.Open, FileAccess.Write))
150
+                    {
151
+                        update.File.CopyTo(fileStream);
152
+                    }
153
+                }
154
+                return LuResult<T>.Ok(returnFunc(new FileInfo(fullpath).ToDbo()));
155
+            }
156
+            catch (Exception e)
157
+            {
158
+                return HandleError<T>(e);
159
+            }
160
+        }
161
+
162
+        public LuResult<T> DeleteSingleById<T>(string id, Func<LuFsFilesDbo, T> returnFunc)
163
+        {
164
+            try
165
+            {
166
+                var get = GetSingleById(id);
167
+                if (get)
168
+                {
169
+                    var fullpath = GetFullPath(get.Data.Path);
170
+                    File.Delete(fullpath);
171
+                }
172
+                return get.To(returnFunc);
173
+            }
174
+            catch (Exception e)
175
+            {
176
+                return HandleError<T>(e);
177
+            }
178
+        }
179
+    }
180
+}

+ 21
- 1
Luticate2.Utils/Dbo/Basic/LuPaginatedDbo.cs Ver fichero

@@ -1,4 +1,6 @@
1
-using System.Collections.Generic;
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
2 4
 
3 5
 namespace Luticate2.Utils.Dbo.Basic
4 6
 {
@@ -7,5 +9,23 @@ namespace Luticate2.Utils.Dbo.Basic
7 9
         public long Count { get; set; }
8 10
 
9 11
         public List<TDbo> Data { get; set; }
12
+
13
+        public LuPaginatedDbo<T> To<T>(Func<List<TDbo>, List<T>> convert)
14
+        {
15
+            return new LuPaginatedDbo<T>
16
+            {
17
+                Count = Count,
18
+                Data = convert(Data)
19
+            };
20
+        }
21
+
22
+        public LuPaginatedDbo<T> Select<T>(Func<TDbo, T> convert)
23
+        {
24
+            return new LuPaginatedDbo<T>
25
+            {
26
+                Count = Count,
27
+                Data = Data.Select(convert).ToList()
28
+            };
29
+        }
10 30
     }
11 31
 }

+ 11
- 0
Luticate2.Utils/Dbo/FsFiles/LuFsFilesAddDbo.cs Ver fichero

@@ -0,0 +1,11 @@
1
+using System.IO;
2
+
3
+namespace Luticate2.Utils.Dbo.FsFiles
4
+{
5
+    public class LuFsFilesAddDbo
6
+    {
7
+        public string Path { get; set; }
8
+
9
+        public Stream File { get; set; }
10
+    }
11
+}

+ 17
- 0
Luticate2.Utils/Dbo/FsFiles/LuFsFilesDbo.cs Ver fichero

@@ -0,0 +1,17 @@
1
+using System;
2
+
3
+namespace Luticate2.Utils.Dbo.FsFiles
4
+{
5
+    public class LuFsFilesDbo
6
+    {
7
+        public string Id { get; set; }
8
+
9
+        public string Path { get; set; }
10
+
11
+        public DateTime CreatedAt { get; set; }
12
+
13
+        public DateTime? UpdatedAt { get; set; }
14
+
15
+        public long Size { get; set; }
16
+    }
17
+}

+ 2
- 0
Luticate2.Utils/Dbo/Result/LuStatus.cs Ver fichero

@@ -5,6 +5,8 @@
5 5
         Success,
6 6
         InputError,
7 7
         DbError,
8
+        FsError,
9
+        BackendError,
8 10
         PermissionError,
9 11
         LoginError,
10 12
         NotFound,

+ 3
- 35
Luticate2.Utils/Interfaces/ILuCrudInterface.cs Ver fichero

@@ -1,8 +1,6 @@
1 1
 using System;
2
-using System.Collections.Generic;
3 2
 using Luticate2.Utils.Dbo.Basic;
4
-using Luticate2.Utils.Dbo.Filter;
5
-using Luticate2.Utils.Dbo.OrderBy;
3
+using Luticate2.Utils.Dbo.PaginatedRequest;
6 4
 using Luticate2.Utils.Dbo.Result;
7 5
 
8 6
 namespace Luticate2.Utils.Interfaces
@@ -12,46 +10,16 @@ namespace Luticate2.Utils.Interfaces
12 10
         where TDboRead : class
13 11
         where TDboUpdate : class
14 12
     {
15
-        LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc);
13
+        TId GetDboId(TDboRead obj);
16 14
 
17 15
         LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc);
18 16
 
19
-
20
-        LuResult<IEnumerable<TId>> AddId(IEnumerable<TDboCreate> objs);
21
-
22
-        LuResult<TId> AddId(TDboCreate obj);
23
-
24
-        LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj);
25
-
26
-        LuResult<TDboRead> AddDbo(TDboCreate obj);
27
-
28
-
29
-
30
-
31 17
         LuResult<TDboRead> GetSingleById(TId id);
32 18
 
33
-
34
-        LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, LuFilterDbo filter,
35
-            int page = 0, int perPage = int.MaxValue);
36
-
37
-        LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue);
38
-
39
-
40
-
19
+        LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuPaginatedRequestDbo request);
41 20
 
42 21
         LuResult<T> EditSingleById<T>(TId id, TDboUpdate update, Func<TDboRead, T> returnFunc);
43 22
 
44
-        LuResult<TId> EditSingleByIdId(TId id, TDboUpdate update);
45
-
46
-        LuResult<TDboRead> EditSingleByIdDbo(TId id, TDboUpdate update);
47
-
48
-
49
-
50
-
51 23
         LuResult<T> DeleteSingleById<T>(TId id, Func<TDboRead, T> returnFunc);
52
-
53
-        LuResult<TId> DeleteSingleByIdId(TId id);
54
-
55
-        LuResult<TDboRead> DeleteSingleByIdDbo(TId id);
56 24
     }
57 25
 }

+ 17
- 0
Luticate2.Utils/Interfaces/ILuNotificationsBusiness.cs Ver fichero

@@ -0,0 +1,17 @@
1
+using System;
2
+
3
+namespace Luticate2.Utils.Interfaces
4
+{
5
+    public interface ILuNotificationsBusiness
6
+    {
7
+        void Notify(string eventName, string entityType, object oldEntity, object newEntity,
8
+            Func<string, bool> filter = null);
9
+
10
+        void NotifyCreate(string entityType, object newEntity, Func<string, bool> filter = null);
11
+
12
+        void NotifyUpdate(string entityType, object oldEntity, object newEntity,
13
+            Func<string, bool> filter = null);
14
+
15
+        void NotifyDelete(string entityType, object oldEntity, Func<string, bool> filter = null);
16
+    }
17
+}

+ 82
- 0
Luticate2.Utils/Utils/LuCrudInterfaceExtensions.cs Ver fichero

@@ -0,0 +1,82 @@
1
+using Luticate2.Utils.Dbo.Basic;
2
+using Luticate2.Utils.Dbo.Filter;
3
+using Luticate2.Utils.Dbo.OrderBy;
4
+using Luticate2.Utils.Dbo.PaginatedRequest;
5
+using Luticate2.Utils.Dbo.Result;
6
+using Luticate2.Utils.Interfaces;
7
+
8
+namespace Luticate2.Utils.Utils
9
+{
10
+    public static class LuCrudInterfaceExtensions
11
+    {
12
+        public static LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TDboCreate, TDboRead, TDboUpdate, TId>(
13
+            this ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId> crud, LuOrderByDbo orderBy, LuFilterDbo filter,
14
+            int page = 0, int perPage = int.MaxValue)
15
+            where TDboCreate : class where TDboRead : class where TDboUpdate : class
16
+        {
17
+            return crud.GetMultiple(new LuPaginatedRequestDbo
18
+            {
19
+                Filter = filter,
20
+                OrderBy = orderBy,
21
+                Page = page,
22
+                PerPage = perPage
23
+            });
24
+        }
25
+
26
+        public static LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TDboCreate, TDboRead, TDboUpdate, TId>(
27
+            this ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId> crud, LuOrderByDbo orderBy,
28
+            int page = 0, int perPage = int.MaxValue)
29
+            where TDboCreate : class where TDboRead : class where TDboUpdate : class
30
+        {
31
+            return crud.GetMultiple(new LuPaginatedRequestDbo
32
+            {
33
+                Filter = null,
34
+                OrderBy = orderBy,
35
+                Page = page,
36
+                PerPage = perPage
37
+            });
38
+        }
39
+
40
+        public static LuResult<TDboRead> AddDbo<TDboCreate, TDboRead, TDboUpdate, TId>(
41
+            this ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId> crud, TDboCreate obj)
42
+            where TDboCreate : class where TDboRead : class where TDboUpdate : class
43
+        {
44
+            return crud.Add(obj, o => o);
45
+        }
46
+
47
+        public static LuResult<TId> AddId<TDboCreate, TDboRead, TDboUpdate, TId>(
48
+            this ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId> crud, TDboCreate obj)
49
+            where TDboCreate : class where TDboRead : class where TDboUpdate : class
50
+        {
51
+            return crud.Add(obj, crud.GetDboId);
52
+        }
53
+
54
+        public static LuResult<TDboRead> EditSingleByIdDbo<TDboCreate, TDboRead, TDboUpdate, TId>(
55
+            this ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId> crud, TId id, TDboUpdate update)
56
+            where TDboCreate : class where TDboRead : class where TDboUpdate : class
57
+        {
58
+            return crud.EditSingleById(id, update, o => o);
59
+        }
60
+
61
+        public static LuResult<TId> EditSingleByIdId<TDboCreate, TDboRead, TDboUpdate, TId>(
62
+            this ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId> crud, TId id, TDboUpdate update)
63
+            where TDboCreate : class where TDboRead : class where TDboUpdate : class
64
+        {
65
+            return crud.EditSingleById(id, update, crud.GetDboId);
66
+        }
67
+
68
+        public static LuResult<TDboRead> DeleteSingleByIdDbo<TDboCreate, TDboRead, TDboUpdate, TId>(
69
+            this ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId> crud, TId id)
70
+            where TDboCreate : class where TDboRead : class where TDboUpdate : class
71
+        {
72
+            return crud.DeleteSingleById(id, o => o);
73
+        }
74
+
75
+        public static LuResult<TId> DeleteSingleByIdId<TDboCreate, TDboRead, TDboUpdate, TId>(
76
+            this ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId> crud, TId id)
77
+            where TDboCreate : class where TDboRead : class where TDboUpdate : class
78
+        {
79
+            return crud.DeleteSingleById(id, crud.GetDboId);
80
+        }
81
+    }
82
+}

+ 61
- 0
Luticate2.Utils/Utils/LuExtensions.cs Ver fichero

@@ -0,0 +1,61 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.IO;
4
+using System.Linq;
5
+using Luticate2.Utils.Dbo.Basic;
6
+using Luticate2.Utils.Dbo.FsFiles;
7
+using Luticate2.Utils.Dbo.OrderBy;
8
+using Luticate2.Utils.Dbo.PaginatedRequest;
9
+
10
+namespace Luticate2.Utils.Utils
11
+{
12
+    public static class LuExtensions
13
+    {
14
+        public static LuFsFilesDbo ToDbo(this FileInfo file)
15
+        {
16
+            if (file == null)
17
+            {
18
+                return null;
19
+            }
20
+            return new LuFsFilesDbo
21
+            {
22
+                Id = file.Name,
23
+                Path = file.Name,
24
+                CreatedAt = file.CreationTime.ToDbo(),
25
+                UpdatedAt = file.LastWriteTime.ToDbo(),
26
+                Size = file.Length
27
+            };
28
+        }
29
+
30
+        public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> list, LuOrderByDbo orderBy,
31
+            Func<string, Func<T, object>> getOrderByFieldExpression)
32
+        {
33
+            IOrderedEnumerable<T> ordered = null;
34
+            foreach (var field in orderBy.Fields)
35
+            {
36
+                var exp = getOrderByFieldExpression(field.Name);
37
+                if (ordered != null)
38
+                {
39
+                    ordered = field.Asc ? ordered.ThenBy(exp) : ordered.ThenByDescending(exp);
40
+                }
41
+                else
42
+                {
43
+                    ordered = field.Asc ? list.OrderBy(exp) : list.OrderByDescending(exp);
44
+                }
45
+            }
46
+            return ordered ?? list;
47
+        }
48
+
49
+        public static LuPaginatedDbo<T> Paginate<T>(this IEnumerable<T> list, LuPaginatedRequestDbo paginatedRequestDbo,
50
+            Func<string, Func<T, object>> getOrderByFieldExpression)
51
+        {
52
+            var ordered = list.OrderBy(paginatedRequestDbo.OrderBy, getOrderByFieldExpression).ToList();
53
+
54
+            return new LuPaginatedDbo<T>
55
+            {
56
+                Count = ordered.Count,
57
+                Data = ordered.Skip(paginatedRequestDbo.Page * paginatedRequestDbo.PerPage).Take(paginatedRequestDbo.PerPage).ToList()
58
+            };
59
+        }
60
+    }
61
+}

+ 19
- 0
TestUtils/DataAccess/LuTmpFsFilesCrudDataAccess.cs Ver fichero

@@ -0,0 +1,19 @@
1
+using Luticate2.Utils.DataAccess;
2
+
3
+namespace TestUtils.DataAccess
4
+{
5
+    public class LuTmpFsFilesCrudDataAccess : LuFsFilesCrudDataAccess
6
+    {
7
+        private readonly string _basePath;
8
+
9
+        public LuTmpFsFilesCrudDataAccess(string basePath)
10
+        {
11
+            _basePath = basePath;
12
+        }
13
+
14
+        public override string GetBasePath()
15
+        {
16
+            return _basePath;
17
+        }
18
+    }
19
+}

+ 4
- 0
TestUtils/DataAccess/LuUtilsPkBigSerialDataAccess.cs Ver fichero

@@ -37,6 +37,10 @@ namespace TestUtils.DataAccess
37 37
 
38 38
         protected override Expression<Func<pk_bigserial, bool>> GetFilterExpression(LuFilterDbo filter)
39 39
         {
40
+            if (filter == null)
41
+            {
42
+                return x => true;
43
+            }
40 44
             var query = filter.Query.ToLower();
41 45
             return model => model.some_text.ToLower().Contains(query) || model.some_int.ToString().Contains(query);
42 46
         }

+ 4
- 0
TestUtils/DataAccess/LuUtilsPkGuidDataAccess.cs Ver fichero

@@ -64,6 +64,10 @@ namespace TestUtils.DataAccess
64 64
 
65 65
         protected override Expression<Func<pk_guid, bool>> GetFilterExpression(LuFilterDbo filter)
66 66
         {
67
+            if (filter == null)
68
+            {
69
+                return x => true;
70
+            }
67 71
             var someText = filter.GetFilterString("someText", null);
68 72
             var someInt = filter.GetFilterInt("someInt", null);
69 73
             return model => LuUtilsDbContext.lu_texts_match(filter.Query, model.some_text + " " + model.some_int.ToString())

+ 1
- 0
TestUtils/EfCrudDataAccess/LuEfCreateDataAccessTest.cs Ver fichero

@@ -2,6 +2,7 @@
2 2
 using System.Collections.Generic;
3 3
 using System.Linq;
4 4
 using Luticate2.Utils.Dbo.Result;
5
+using Luticate2.Utils.Utils;
5 6
 using TestUtils.DataAccess;
6 7
 using TestUtils.Dbo.FkPkGuid;
7 8
 using TestUtils.Dbo.PkBigSerial;

+ 1
- 0
TestUtils/EfCrudDataAccess/LuEfDeleteDataAccessTest.cs Ver fichero

@@ -1,6 +1,7 @@
1 1
 using System.Collections.Generic;
2 2
 using System.Linq;
3 3
 using Luticate2.Utils.Dbo.Result;
4
+using Luticate2.Utils.Utils;
4 5
 using TestUtils.DataAccess;
5 6
 using TestUtils.Dbo.PkBigSerial;
6 7
 using TestUtils.Dbo.PkGuid;

+ 1
- 0
TestUtils/EfCrudDataAccess/LuEfReadDataAccessTest.cs Ver fichero

@@ -4,6 +4,7 @@ using System.Linq;
4 4
 using Luticate2.Utils.Dbo;
5 5
 using Luticate2.Utils.Dbo.OrderBy;
6 6
 using Luticate2.Utils.Dbo.Result;
7
+using Luticate2.Utils.Utils;
7 8
 using TestUtils.DataAccess;
8 9
 using TestUtils.Dbo.PkBigSerial;
9 10
 using TestUtils.Dbo.PkGuid;

+ 2
- 1
TestUtils/EfCrudDataAccess/LuEfTransactionScopeTest.cs Ver fichero

@@ -1,5 +1,6 @@
1 1
 using System;
2 2
 using Luticate2.Utils.Dbo.Result;
3
+using Luticate2.Utils.Utils;
3 4
 using Microsoft.Extensions.DependencyInjection;
4 5
 using TestUtils.DataAccess;
5 6
 using TestUtils.Dbo.PkGuid;
@@ -12,7 +13,7 @@ namespace TestUtils.EfCrudDataAccess
12 13
         [Fact]
13 14
         public void TestAddDboSingle1()
14 15
         {
15
-            var serviceProvider = Tests.BuildServiceProvider();
16
+            var serviceProvider = Tests.BuildRealDbServiceProvider();
16 17
             var service = serviceProvider.GetService<LuUtilsPkGuidDataAccess>();
17 18
             var transact = service.BeginTransaction(null);
18 19
             var res = service.AddDbo(new PkGuidAddDbo

+ 1
- 0
TestUtils/EfCrudDataAccess/LuEfUpdateDataAccessTest.cs Ver fichero

@@ -1,6 +1,7 @@
1 1
 using System.Collections.Generic;
2 2
 using System.Linq;
3 3
 using Luticate2.Utils.Dbo.Result;
4
+using Luticate2.Utils.Utils;
4 5
 using TestUtils.DataAccess;
5 6
 using TestUtils.Dbo.PkBigSerial;
6 7
 using TestUtils.Dbo.PkGuid;

+ 361
- 0
TestUtils/EfCrudDataAccess/LuFsFilesCrudDataAccessTest.cs Ver fichero

@@ -0,0 +1,361 @@
1
+using System.Collections.Generic;
2
+using System.IO;
3
+using Luticate2.Utils.DataAccess;
4
+using Luticate2.Utils.Dbo.FsFiles;
5
+using Luticate2.Utils.Dbo.OrderBy;
6
+using Luticate2.Utils.Dbo.PaginatedRequest;
7
+using Luticate2.Utils.Dbo.Result;
8
+using Luticate2.Utils.Utils;
9
+using Xunit;
10
+
11
+namespace TestUtils.EfCrudDataAccess
12
+{
13
+    public class LuFsFilesCrudDataAccessTest
14
+    {
15
+        public static Stream GenerateStreamFromString(string s)
16
+        {
17
+            if (s == null)
18
+            {
19
+                return null;
20
+            }
21
+            var stream = new MemoryStream();
22
+            var writer = new StreamWriter(stream);
23
+            writer.Write(s);
24
+            writer.Flush();
25
+            stream.Position = 0;
26
+            return stream;
27
+        }
28
+
29
+        public static bool CheckFileContent(LuFsFilesCrudDataAccess access, string path, string content)
30
+        {
31
+            return File.ReadAllText(access.GetFullPath(path)) == content;
32
+        }
33
+
34
+        public static LuFsFilesDbo TestAdd(LuFsFilesCrudDataAccess access, string path, string content)
35
+        {
36
+            var res = access.AddDbo(new LuFsFilesAddDbo
37
+            {
38
+                File = GenerateStreamFromString(content),
39
+                Path = path
40
+            });
41
+            Assert.Equal(LuStatus.Success, res.Status);
42
+            Assert.Equal(path, res.Data.Path);
43
+            Assert.Equal(content.Length, res.Data.Size);
44
+            Assert.True(CheckFileContent(access, res.Data.Path, content));
45
+            return res.Data;
46
+        }
47
+
48
+        public static LuFsFilesDbo TestEdit(LuFsFilesCrudDataAccess access, string path, string content, string pathEdit, string contentEdit)
49
+        {
50
+            TestAdd(access, path, content);
51
+            TestGetSingleById(access, path, content);
52
+            var res = access.EditSingleByIdDbo(path, new LuFsFilesAddDbo
53
+            {
54
+                File = GenerateStreamFromString(contentEdit),
55
+                Path = pathEdit
56
+            });
57
+            Assert.Equal(LuStatus.Success, res.Status);
58
+            Assert.Equal(pathEdit ?? path, res.Data.Path);
59
+            Assert.Equal(contentEdit?.Length ?? content.Length, res.Data.Size);
60
+            Assert.True(CheckFileContent(access, res.Data.Path, contentEdit ?? content));
61
+            return res.Data;
62
+        }
63
+
64
+        public static LuFsFilesDbo TestGetSingleById(LuFsFilesCrudDataAccess access, string path, string content)
65
+        {
66
+            var res = access.GetSingleById(path);
67
+            Assert.Equal(LuStatus.Success, res.Status);
68
+            Assert.Equal(path, res.Data.Path);
69
+            Assert.Equal(content.Length, res.Data.Size);
70
+            Assert.True(CheckFileContent(access, res.Data.Path, content));
71
+            return res.Data;
72
+        }
73
+
74
+        public static LuFsFilesDbo TestDeleteSingleById(LuFsFilesCrudDataAccess access, string path)
75
+        {
76
+            var del = access.DeleteSingleByIdDbo(path);
77
+            Assert.Equal(LuStatus.Success, del.Status);
78
+            Assert.Equal(path, del.Data.Path);
79
+
80
+            var get = access.GetSingleById(path);
81
+            Assert.Equal(LuStatus.NotFound, get.Status);
82
+
83
+            return del.Data;
84
+        }
85
+
86
+        public static void TestGetMultiple(LuFsFilesCrudDataAccess access, LuPaginatedRequestDbo request,
87
+            List<string> paths, List<string> contents,
88
+            List<int> gets, int count, bool add = true)
89
+        {
90
+            if (add)
91
+            {
92
+                for (var i = 0; i < paths.Count; ++i)
93
+                {
94
+                    TestAdd(access, paths[i], contents[i]);
95
+                    TestGetSingleById(access, paths[i], contents[i]);
96
+                }
97
+            }
98
+            var get = access.GetMultiple(request);
99
+            Assert.Equal(LuStatus.Success, get.Status);
100
+            Assert.Equal(count, get.Data.Count);
101
+            Assert.Equal(gets.Count, get.Data.Data.Count);
102
+            for (var i = 0; i < gets.Count; ++i)
103
+            {
104
+                Assert.Equal(paths[gets[i]], get.Data.Data[i].Path);
105
+                Assert.Equal(contents[gets[i]].Length, get.Data.Data[i].Size);
106
+                Assert.True(CheckFileContent(access, paths[gets[i]], contents[gets[i]]));
107
+            }
108
+        }
109
+
110
+        [Fact]
111
+        public void TestAdd1()
112
+        {
113
+            Tests.TestRealFs(access =>
114
+            {
115
+                var path = "42.txt";
116
+                var content = "42";
117
+                TestAdd(access, path, content);
118
+                TestGetSingleById(access, path, content);
119
+            });
120
+        }
121
+
122
+        [Fact]
123
+        public void TestAdd2()
124
+        {
125
+            Tests.TestRealFs(access =>
126
+            {
127
+                var path = "42.txt";
128
+                var content = "42";
129
+                TestAdd(access, path, content);
130
+                TestGetSingleById(access, path, content);
131
+
132
+                var res = access.AddDbo(new LuFsFilesAddDbo
133
+                {
134
+                    File = GenerateStreamFromString(content),
135
+                    Path = path
136
+                });
137
+                Assert.Equal(LuStatus.InputError, res.Status);
138
+            });
139
+        }
140
+
141
+        [Fact]
142
+        public void TestGetMutliple1()
143
+        {
144
+            Tests.TestRealFs(access =>
145
+            {
146
+                var paths = new List<string> {"42.txt", "24.txt", "test"};
147
+                var contents = new List<string> {"txt.42", "txt.24", "test.test"};
148
+                var gets = new List<int>{1, 0, 2};
149
+                TestGetMultiple(access, new LuPaginatedRequestDbo
150
+                {
151
+                    Filter = null,
152
+                    OrderBy = new LuOrderByDbo
153
+                    {
154
+                        Fields = new List<LuOrderByFieldDbo>
155
+                        {
156
+                            new LuOrderByFieldDbo
157
+                            {
158
+                                Asc = true,
159
+                                Name = "path"
160
+                            }
161
+                        }
162
+                    },
163
+                    Page = 0,
164
+                    PerPage = int.MaxValue
165
+                }, paths, contents, gets, gets.Count);
166
+            });
167
+        }
168
+
169
+        [Fact]
170
+        public void TestGetMutliple2()
171
+        {
172
+            Tests.TestRealFs(access =>
173
+            {
174
+                var paths = new List<string> {"42.txt", "24.txt", "test"};
175
+                var contents = new List<string> {"txt.42", "txt.24", "test.test"};
176
+                var gets = new List<int>{1, 0};
177
+                TestGetMultiple(access, new LuPaginatedRequestDbo
178
+                {
179
+                    Filter = null,
180
+                    OrderBy = new LuOrderByDbo
181
+                    {
182
+                        Fields = new List<LuOrderByFieldDbo>
183
+                        {
184
+                            new LuOrderByFieldDbo
185
+                            {
186
+                                Asc = true,
187
+                                Name = "path"
188
+                            }
189
+                        }
190
+                    },
191
+                    Page = 0,
192
+                    PerPage = 2
193
+                }, paths, contents, gets, paths.Count);
194
+            });
195
+        }
196
+
197
+        [Fact]
198
+        public void TestGetMutliple3()
199
+        {
200
+            Tests.TestRealFs(access =>
201
+            {
202
+                var paths = new List<string> {"42.txt", "24.txt", "test"};
203
+                var contents = new List<string> {"txt.42", "txt.24", "test.test"};
204
+                var gets = new List<int>{2, 0, 1};
205
+                TestGetMultiple(access, new LuPaginatedRequestDbo
206
+                {
207
+                    Filter = null,
208
+                    OrderBy = new LuOrderByDbo
209
+                    {
210
+                        Fields = new List<LuOrderByFieldDbo>
211
+                        {
212
+                            new LuOrderByFieldDbo
213
+                            {
214
+                                Asc = false,
215
+                                Name = "path"
216
+                            }
217
+                        }
218
+                    },
219
+                    Page = 0,
220
+                    PerPage = int.MaxValue
221
+                }, paths, contents, gets, gets.Count);
222
+            });
223
+        }
224
+
225
+        [Fact]
226
+        public void TestDelete1()
227
+        {
228
+            Tests.TestRealFs(access =>
229
+            {
230
+                var path = "42.txt";
231
+                var content = "42";
232
+                TestAdd(access, path, content);
233
+                TestGetSingleById(access, path, content);
234
+
235
+                TestDeleteSingleById(access, path);
236
+            });
237
+        }
238
+
239
+        [Fact]
240
+        public void TestDelete2()
241
+        {
242
+            Tests.TestRealFs(access =>
243
+            {
244
+                var path = "42.txt";
245
+                var content = "42";
246
+                TestAdd(access, path, content);
247
+                TestGetSingleById(access, path, content);
248
+
249
+                TestDeleteSingleById(access, path);
250
+
251
+                var del = access.DeleteSingleByIdDbo(path);
252
+                Assert.Equal(LuStatus.NotFound, del.Status);
253
+            });
254
+        }
255
+
256
+        [Fact]
257
+        public void TestDelete3()
258
+        {
259
+            Tests.TestRealFs(access =>
260
+            {
261
+                var paths = new List<string> {"42.txt", "24.txt", "test"};
262
+                var contents = new List<string> {"txt.42", "txt.24", "test.test"};
263
+                var gets = new List<int>{2, 0, 1};
264
+                TestGetMultiple(access, new LuPaginatedRequestDbo
265
+                {
266
+                    Filter = null,
267
+                    OrderBy = new LuOrderByDbo
268
+                    {
269
+                        Fields = new List<LuOrderByFieldDbo>
270
+                        {
271
+                            new LuOrderByFieldDbo
272
+                            {
273
+                                Asc = false,
274
+                                Name = "path"
275
+                            }
276
+                        }
277
+                    },
278
+                    Page = 0,
279
+                    PerPage = int.MaxValue
280
+                }, paths, contents, gets, gets.Count);
281
+
282
+                var path = paths[0];
283
+                TestDeleteSingleById(access, path);
284
+
285
+                var del = access.DeleteSingleByIdDbo(path);
286
+                Assert.Equal(LuStatus.NotFound, del.Status);
287
+
288
+                gets = new List<int>{2, 1};
289
+                TestGetMultiple(access, new LuPaginatedRequestDbo
290
+                {
291
+                    Filter = null,
292
+                    OrderBy = new LuOrderByDbo
293
+                    {
294
+                        Fields = new List<LuOrderByFieldDbo>
295
+                        {
296
+                            new LuOrderByFieldDbo
297
+                            {
298
+                                Asc = false,
299
+                                Name = "path"
300
+                            }
301
+                        }
302
+                    },
303
+                    Page = 0,
304
+                    PerPage = int.MaxValue
305
+                }, paths, contents, gets, gets.Count, false);
306
+            });
307
+        }
308
+
309
+        [Fact]
310
+        public void TestEdit1()
311
+        {
312
+            Tests.TestRealFs(access =>
313
+            {
314
+                var path = "42.txt";
315
+                var content = "42";
316
+                var pathEdit = "4224.txt";
317
+                var contentEdit = "4224";
318
+                TestEdit(access, path, content, pathEdit, contentEdit);
319
+                TestGetSingleById(access, pathEdit, contentEdit);
320
+            });
321
+        }
322
+
323
+        [Fact]
324
+        public void TestEdit2()
325
+        {
326
+            Tests.TestRealFs(access =>
327
+            {
328
+                var path = "42.txt";
329
+                var content = "42";
330
+                var contentEdit = "4224";
331
+                TestEdit(access, path, content, null, contentEdit);
332
+                TestGetSingleById(access, path, contentEdit);
333
+            });
334
+        }
335
+
336
+        [Fact]
337
+        public void TestEdit3()
338
+        {
339
+            Tests.TestRealFs(access =>
340
+            {
341
+                var path = "42.txt";
342
+                var content = "42";
343
+                var pathEdit = "4224.txt";
344
+                TestEdit(access, path, content, pathEdit, null);
345
+                TestGetSingleById(access, pathEdit, content);
346
+            });
347
+        }
348
+
349
+        [Fact]
350
+        public void TestEdit4()
351
+        {
352
+            Tests.TestRealFs(access =>
353
+            {
354
+                var path = "42.txt";
355
+                var content = "42";
356
+                TestEdit(access, path, content, null, null);
357
+                TestGetSingleById(access, path, content);
358
+            });
359
+        }
360
+    }
361
+}

+ 22
- 4
TestUtils/Tests.cs Ver fichero

@@ -1,4 +1,5 @@
1 1
 using System;
2
+using System.IO;
2 3
 using Luticate2.Utils.DataAccess;
3 4
 using Microsoft.EntityFrameworkCore;
4 5
 using Microsoft.Extensions.DependencyInjection;
@@ -8,24 +9,27 @@ namespace TestUtils
8 9
 {
9 10
     public class Tests
10 11
     {
11
-        public const string ConnectionString =
12
+        public const string RealDbConnectionString =
12 13
             "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2_utils;Pooling=true;";
13 14
 
14
-        public static IServiceProvider BuildServiceProvider()
15
+        public const string RealFsDir =
16
+            "/tmp/luticate2/";
17
+
18
+        public static IServiceProvider BuildRealDbServiceProvider()
15 19
         {
16 20
             IServiceCollection serviceCollection = new ServiceCollection();
17 21
             serviceCollection.AddScoped<LuEfTransactionScope>();
18 22
             serviceCollection.AddTransient<LuUtilsPkBigSerialDataAccess>();
19 23
             serviceCollection.AddTransient<LuUtilsPkGuidDataAccess>();
20 24
             serviceCollection.AddTransient<LuUtilsFkPkGuidDataAccess>();
21
-            serviceCollection.AddDbContext<LuUtilsDbContext>(builder => builder.UseNpgsql(ConnectionString),
25
+            serviceCollection.AddDbContext<LuUtilsDbContext>(builder => builder.UseNpgsql(RealDbConnectionString),
22 26
                 ServiceLifetime.Transient);
23 27
             return serviceCollection.BuildServiceProvider();
24 28
         }
25 29
 
26 30
         protected static void _TestRealDb(Action<IServiceProvider> func)
27 31
         {
28
-            var serviceProvider = BuildServiceProvider();
32
+            var serviceProvider = BuildRealDbServiceProvider();
29 33
             var transactionScope = serviceProvider.GetService<LuEfTransactionScope>();
30 34
             transactionScope.BeginTransaction<LuUtilsDbContext>(null);
31 35
             try
@@ -53,5 +57,19 @@ namespace TestUtils
53 57
                 func(provider.GetService<TDataAccess>(), provider.GetService<TDataAccess2>());
54 58
             });
55 59
         }
60
+
61
+        public static void TestRealFs(Action<LuFsFilesCrudDataAccess> func)
62
+        {
63
+            var tmp = Path.Combine(RealFsDir, "RealFsDir-" + new Random().Next());
64
+            try
65
+            {
66
+                Directory.CreateDirectory(tmp);
67
+                func(new LuTmpFsFilesCrudDataAccess(tmp));
68
+            }
69
+            finally
70
+            {
71
+                Directory.Delete(tmp, true);
72
+            }
73
+        }
56 74
     }
57 75
 }

+ 1
- 1
TestUtils/project.json Ver fichero

@@ -5,7 +5,7 @@
5 5
     },
6 6
     "dependencies": {
7 7
         "dotnet-test-xunit": "1.0.0-rc2-*",
8
-        "Luticate2.Utils": "0.3.*",
8
+        "Luticate2.Utils": "0.4.*",
9 9
         "Moq": "4.6.38-alpha",
10 10
         "System.Runtime.Serialization.Primitives": "4.1.1",
11 11
         "xunit": "2.1.0"

+ 14
- 0
WebApiUtils/Business/UploadBusiness.cs Ver fichero

@@ -0,0 +1,14 @@
1
+using Luticate2.Utils.Business;
2
+using Luticate2.Utils.Dbo.FsFiles;
3
+using Luticate2.Utils.Interfaces;
4
+using WebApiUtils.DataAccess;
5
+
6
+namespace WebApiUtils.Business
7
+{
8
+    public class UploadBusiness : LuCrudBusiness<UploadDataAccess, LuFsFilesAddDbo, LuFsFilesDbo, LuFsFilesAddDbo, string>
9
+    {
10
+        public UploadBusiness(UploadDataAccess dataAccess, ILuNotificationsBusiness notificationsBusiness) : base(dataAccess, notificationsBusiness)
11
+        {
12
+        }
13
+    }
14
+}

+ 14
- 0
WebApiUtils/Controllers/UploadController.cs Ver fichero

@@ -0,0 +1,14 @@
1
+using Luticate2.Utils.Controllers;
2
+using Luticate2.Utils.Dbo.Basic;
3
+using Microsoft.Extensions.Options;
4
+using WebApiUtils.Business;
5
+
6
+namespace WebApiUtils.Controllers
7
+{
8
+    public class UploadController : LuFsFilesCrudController<UploadBusiness>
9
+    {
10
+        public UploadController(IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo, UploadBusiness business) : base(luUtilsOptionsDbo, business)
11
+        {
12
+        }
13
+    }
14
+}

+ 12
- 0
WebApiUtils/DataAccess/UploadDataAccess.cs Ver fichero

@@ -0,0 +1,12 @@
1
+using Luticate2.Utils.DataAccess;
2
+
3
+namespace WebApiUtils.DataAccess
4
+{
5
+    public class UploadDataAccess : LuFsFilesCrudDataAccess
6
+    {
7
+        public override string GetBasePath()
8
+        {
9
+            return "/tmp/luticate2/";
10
+        }
11
+    }
12
+}

+ 15
- 3
WebApiUtils/Startup.cs Ver fichero

@@ -1,12 +1,16 @@
1
+using System.IO;
1 2
 using Luticate2.Utils.Controllers;
2 3
 using Microsoft.AspNetCore.Builder;
3 4
 using Microsoft.AspNetCore.Hosting;
5
+using Microsoft.AspNetCore.Http;
4 6
 using Microsoft.EntityFrameworkCore;
5 7
 using Microsoft.Extensions.Configuration;
6 8
 using Microsoft.Extensions.DependencyInjection;
9
+using Microsoft.Extensions.FileProviders;
7 10
 using Microsoft.Extensions.Logging;
8 11
 using TestUtils.DataAccess;
9 12
 using WebApiUtils.Business;
13
+using WebApiUtils.DataAccess;
10 14
 
11 15
 namespace WebApiUtils
12 16
 {
@@ -41,6 +45,8 @@ namespace WebApiUtils
41 45
 
42 46
             services.AddScoped<PkGuidBusiness>();
43 47
             services.AddScoped<LuUtilsPkGuidDataAccess>();
48
+            services.AddScoped<UploadBusiness>();
49
+            services.AddScoped<UploadDataAccess>();
44 50
             services.AddDbContext<LuUtilsDbContext>(options =>
45 51
             {
46 52
                 options.UseNpgsql(Configuration.GetConnectionString("default"));
@@ -60,9 +66,15 @@ namespace WebApiUtils
60 66
             loggerFactory.AddConsole(Configuration.GetSection("Logging"));
61 67
             loggerFactory.AddDebug();
62 68
 
63
-            app.UseApplicationInsightsRequestTelemetry();
64
-
65
-            app.UseApplicationInsightsExceptionTelemetry();
69
+//            app.UseApplicationInsightsRequestTelemetry();
70
+//
71
+//            app.UseApplicationInsightsExceptionTelemetry();
72
+            app.UseStaticFiles(new StaticFileOptions
73
+            {
74
+                ServeUnknownFileTypes = true,
75
+                FileProvider = new PhysicalFileProvider("/tmp/luticate2"),
76
+                RequestPath = new PathString("/upload")
77
+            });
66 78
 
67 79
             app.UseLuticateUtils();
68 80
 

+ 1
- 0
WebApiUtils/project.json Ver fichero

@@ -13,6 +13,7 @@
13 13
         "Microsoft.Extensions.Logging.Console": "1.1.0",
14 14
         "Microsoft.Extensions.Logging.Debug": "1.1.0",
15 15
         "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
16
+        "Microsoft.AspNetCore.StaticFiles": "1.1.0",
16 17
         "Microsoft.NETCore.App": {
17 18
             "version": "1.0.1",
18 19
             "type": "platform"

Loading…
Cancelar
Guardar