Browse Source

Added error handling; added tests; basic read from DB working

develop
Robin Thoni 6 years ago
parent
commit
934546e075
31 changed files with 681 additions and 185 deletions
  1. 1
    1
      Luticate2.Auth.Tests/Business/ExpressionConverter/ExpressionConverterTests.cs
  2. 1
    1
      Luticate2.Auth.Tests/Business/ExpressionConverter/Objects.cs
  3. 1
    1
      Luticate2.Auth.Tests/Business/ObjectConverter/Objects.cs
  4. 239
    0
      Luticate2.Auth.Tests/DataAccess/Crud/LuEfCrudTests.cs
  5. 45
    0
      Luticate2.Auth.Tests/DataAccess/Crud/Models/luticate2_unit_testsContext.cs
  6. 15
    0
      Luticate2.Auth.Tests/DataAccess/Crud/Models/table_simple_1.cs
  7. 5
    0
      Luticate2.Auth.Tests/Luticate2.Auth.Tests.csproj
  8. 34
    38
      Luticate2.Auth.Tests/RandomTests.cs
  9. 2
    2
      Luticate2.Auth/Auth/Business/LuGroupsBusiness.cs
  10. 2
    1
      Luticate2.Auth/Luticate2.Auth.csproj
  11. 46
    27
      Luticate2.Auth/Utils/Business/Converters/ExpressionConverter/LuExpressionConverterVisitor.cs
  12. 1
    1
      Luticate2.Auth/Utils/Business/Converters/ExpressionConverter/LuExpressionParamReplaceVisitor.cs
  13. 29
    5
      Luticate2.Auth/Utils/Business/Converters/LuConvertersAllocator.cs
  14. 55
    11
      Luticate2.Auth/Utils/Business/Converters/LuConvertersExtensions.cs
  15. 1
    1
      Luticate2.Auth/Utils/Business/Converters/LuConvertersOptions.cs
  16. 4
    3
      Luticate2.Auth/Utils/Business/Converters/LuConvertersTypeConverter.cs
  17. 1
    1
      Luticate2.Auth/Utils/Business/Converters/ObjectConverter/LuObjectConverterIdentity.cs
  18. 32
    8
      Luticate2.Auth/Utils/Business/Converters/ObjectConverter/LuObjectConverterLists.cs
  19. 31
    10
      Luticate2.Auth/Utils/Business/Converters/ObjectConverter/LuObjectConverterPoco.cs
  20. 11
    3
      Luticate2.Auth/Utils/Business/Converters/ObjectConverter/LuObjectConverterUtils.cs
  21. 14
    9
      Luticate2.Auth/Utils/Business/Converters/ObjectConverterDescriptor/LuObjectConverterDescriptor.cs
  22. 1
    1
      Luticate2.Auth/Utils/Business/Converters/ObjectConverterDescriptor/LuObjectConverterDescriptorEnumerable.cs
  23. 6
    5
      Luticate2.Auth/Utils/Business/Converters/ObjectConverterDescriptor/LuObjectConverterDescriptorIdentity.cs
  24. 3
    3
      Luticate2.Auth/Utils/Business/Converters/ObjectConverterDescriptor/LuObjectConverterDescriptorQueryable.cs
  25. 5
    2
      Luticate2.Auth/Utils/Business/Pagination/LuFilterParser.cs
  26. 22
    22
      Luticate2.Auth/Utils/Business/Pagination/LuOrderByParser.cs
  27. 65
    17
      Luticate2.Auth/Utils/DataAccess/Crud/LuEfCrudDataAccess.cs
  28. 2
    8
      Luticate2.Auth/Utils/Dbo/Pagination/LuOrderByFieldDbo.cs
  29. 2
    1
      Luticate2.Auth/Utils/Interfaces/ILuConvertersAllocator.cs
  30. 2
    1
      Luticate2.Auth/Utils/Interfaces/ILuConvertersTypeConverter.cs
  31. 3
    2
      Luticate2.Auth/Utils/Interfaces/ILuObjectConverterDescriptor.cs

+ 1
- 1
Luticate2.Auth.Tests/Business/ExpressionConverter/ExpressionConverterTests.cs View File

3
 using System.Linq;
3
 using System.Linq;
4
 using System.Linq.Expressions;
4
 using System.Linq.Expressions;
5
 using Luticate2.Auth.Utils.Business.Converters;
5
 using Luticate2.Auth.Utils.Business.Converters;
6
-using Luticate2.Auth.Utils.Business.ExpressionConverter;
6
+using Luticate2.Auth.Utils.Business.Converters.ExpressionConverter;
7
 using Luticate2.Auth.Utils.Interfaces;
7
 using Luticate2.Auth.Utils.Interfaces;
8
 using Microsoft.Extensions.DependencyInjection;
8
 using Microsoft.Extensions.DependencyInjection;
9
 using Xunit;
9
 using Xunit;

+ 1
- 1
Luticate2.Auth.Tests/Business/ExpressionConverter/Objects.cs View File

1
 using System;
1
 using System;
2
 using System.Collections.Generic;
2
 using System.Collections.Generic;
3
 using System.Linq.Expressions;
3
 using System.Linq.Expressions;
4
-using Luticate2.Auth.Utils.Business.ObjectConverterDescriptor;
4
+using Luticate2.Auth.Utils.Business.Converters.ObjectConverterDescriptor;
5
 
5
 
6
 namespace Luticate2.Auth.Tests.Business.ExpressionConverter
6
 namespace Luticate2.Auth.Tests.Business.ExpressionConverter
7
 {
7
 {

+ 1
- 1
Luticate2.Auth.Tests/Business/ObjectConverter/Objects.cs View File

1
 using System;
1
 using System;
2
 using System.Collections.Generic;
2
 using System.Collections.Generic;
3
 using System.Linq.Expressions;
3
 using System.Linq.Expressions;
4
-using Luticate2.Auth.Utils.Business.ObjectConverterDescriptor;
4
+using Luticate2.Auth.Utils.Business.Converters.ObjectConverterDescriptor;
5
 
5
 
6
 namespace Luticate2.Auth.Tests.Business.ObjectConverter
6
 namespace Luticate2.Auth.Tests.Business.ObjectConverter
7
 {
7
 {

+ 239
- 0
Luticate2.Auth.Tests/DataAccess/Crud/LuEfCrudTests.cs View File

1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq.Expressions;
4
+using Luticate2.Auth.Tests.DataAccess.Crud.Models;
5
+using Luticate2.Auth.Utils.Business.Converters;
6
+using Luticate2.Auth.Utils.Business.Converters.ObjectConverterDescriptor;
7
+using Luticate2.Auth.Utils.Business.Fields;
8
+using Luticate2.Auth.Utils.DataAccess.Crud;
9
+using Luticate2.Auth.Utils.Dbo;
10
+using Luticate2.Auth.Utils.Dbo.Fields;
11
+using Luticate2.Auth.Utils.Dbo.Pagination;
12
+using Luticate2.Auth.Utils.Dbo.Result;
13
+using Luticate2.Auth.Utils.Interfaces;
14
+using Microsoft.EntityFrameworkCore;
15
+using Microsoft.Extensions.DependencyInjection;
16
+using Xunit;
17
+
18
+namespace Luticate2.Auth.Tests.DataAccess.Crud
19
+{
20
+    public class LuEfCrudTests
21
+    {
22
+        public class TableSimple1Dbo
23
+        {
24
+            public Guid Id { get; set; }
25
+
26
+            public string Name { get; set; }
27
+
28
+            public int Priority { get; set; }
29
+
30
+            public string Comment { get; set; }
31
+
32
+            public int? OptionalInt { get; set; }
33
+
34
+            public DateTime CreatedAt { get; set; }
35
+
36
+            public DateTime? UpdatedAt { get; set; }
37
+        }
38
+
39
+        public class TableSimple1DboDescriptor : LuObjectConverterDescriptor<TableSimple1Dbo, table_simple_1>
40
+        {
41
+            public TableSimple1DboDescriptor()
42
+            {
43
+                AddStaticMemberConverter(dbo => dbo.Id, model => model.id);
44
+                AddStaticMemberConverter(dbo => dbo.Name, model => model.name);
45
+                AddStaticMemberConverter(dbo => dbo.Priority, model => model.priority);
46
+                AddStaticMemberConverter(dbo => dbo.Comment, model => model.comment);
47
+                AddStaticMemberConverter(dbo => dbo.OptionalInt, model => model.optional_int);
48
+                AddStaticMemberConverter(dbo => dbo.CreatedAt, model => model.created_at);
49
+                AddStaticMemberConverter(dbo => dbo.UpdatedAt, model => model.updated_at);
50
+            }
51
+        }
52
+
53
+        public class TableSimple1DataAccess : LuEfCrudDataAccess<TableSimple1Dbo, table_simple_1, luticate2_unit_testsContext>
54
+        {
55
+            public TableSimple1DataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
56
+            {
57
+            }
58
+        }
59
+
60
+        protected IServiceProvider GetServiceProvider()
61
+        {
62
+            var services = new ServiceCollection();
63
+            services.AddLuObjectConverterDescriptors();
64
+            services.AddSingleton<ILuObjectConverterDescriptor<TableSimple1Dbo, table_simple_1>, TableSimple1DboDescriptor>();
65
+
66
+            services.AddLuObjectConverters();
67
+            services.AddLuObjectConverterPoco<table_simple_1, TableSimple1Dbo>(); // DB read
68
+
69
+            services.AddDbContext<luticate2_unit_testsContext>(options =>
70
+            {
71
+                options.UseNpgsql(@"Host=localhost;Database=luticate2_unit_tests;Username=dev;Password=dev");
72
+                options.UseInternalServiceProvider(new ServiceCollection()
73
+                    .AddEntityFrameworkNpgsql()
74
+                    .BuildServiceProvider());
75
+            }, ServiceLifetime.Transient);
76
+
77
+            services.AddSingleton<TableSimple1DataAccess>();
78
+            services.AddSingleton<ILuConvertersTypeConverter>(new LuConvertersTypeConverter(new Dictionary<Type, Type>
79
+            {
80
+                {typeof(TableSimple1Dbo), typeof(table_simple_1)} // DB read
81
+            }));
82
+
83
+            var serviceProvider = services.BuildServiceProvider();
84
+            return serviceProvider;
85
+        }
86
+
87
+        [Fact]
88
+        public void TestSimpleReadFilter1()
89
+        {
90
+            var serviceProvider = GetServiceProvider();
91
+
92
+            var crudDataAccess = serviceProvider.GetRequiredService<TableSimple1DataAccess>();
93
+
94
+            var result = crudDataAccess.Read(LuPartialFieldsParser.Parse("*").Data, new LuPaginatedParamsDbo
95
+            {
96
+                Filter = new LuFilterDbo
97
+                {
98
+                    Expression = (Expression<Func<TableSimple1Dbo, bool>>) (x => x.Name == "test1")
99
+                },
100
+                Page = 0,
101
+                OrderBy = new LuOrderByDbo
102
+                {
103
+                    OrderByFields = new List<LuOrderByFieldDbo>()
104
+                },
105
+                PerPage = 10
106
+            });
107
+            Assert.NotNull(result);
108
+            Assert.Equal(LuStatus.Success.ToInt(), result.Status);
109
+            var items = result.Data;
110
+            Assert.NotNull(items);
111
+            Assert.Equal(1, items.Count);
112
+            Assert.NotNull(items.Data);
113
+            Assert.Equal(1, items.Data.Count);
114
+
115
+            var item = items.Data[0];
116
+            Assert.NotNull(item);
117
+            Assert.Equal("test1", item.Name);
118
+            Assert.Equal(42, item.Priority);
119
+            Assert.Null(item.Comment);
120
+            Assert.Null(item.OptionalInt);
121
+        }
122
+
123
+        [Fact]
124
+        public void TestSimpleReadOrderBy1()
125
+        {
126
+            var serviceProvider = GetServiceProvider();
127
+
128
+            var crudDataAccess = serviceProvider.GetRequiredService<TableSimple1DataAccess>();
129
+
130
+            var result = crudDataAccess.Read(LuPartialFieldsParser.Parse("*").Data, new LuPaginatedParamsDbo
131
+            {
132
+                Filter = new LuFilterDbo
133
+                {
134
+                    Expression = (Expression<Func<TableSimple1Dbo, bool>>) (x => true)
135
+                },
136
+                Page = 0,
137
+                OrderBy = new LuOrderByDbo
138
+                {
139
+                    OrderByFields = new List<LuOrderByFieldDbo>
140
+                    {
141
+                        new LuOrderByFieldDbo
142
+                        {
143
+                            Asc = true,
144
+                            Expression = (Expression<Func<TableSimple1Dbo, object>>) (x => x.Priority)
145
+                        }
146
+                    }
147
+                },
148
+                PerPage = 10
149
+            });
150
+            Assert.NotNull(result);
151
+            Assert.Equal(LuStatus.Success.ToInt(), result.Status);
152
+            var items = result.Data;
153
+            Assert.NotNull(items);
154
+            Assert.Equal(2, items.Count);
155
+            Assert.NotNull(items.Data);
156
+            Assert.Equal(2, items.Data.Count);
157
+
158
+            var item = items.Data[0];
159
+            Assert.NotNull(item);
160
+            Assert.Equal("test2", item.Name);
161
+            Assert.Equal(24, item.Priority);
162
+            Assert.Null(item.Comment);
163
+            Assert.Equal(1, item.OptionalInt);
164
+
165
+            item = items.Data[1];
166
+            Assert.NotNull(item);
167
+            Assert.Equal("test1", item.Name);
168
+            Assert.Equal(42, item.Priority);
169
+            Assert.Null(item.Comment);
170
+            Assert.Null(item.OptionalInt);
171
+        }
172
+
173
+        [Fact]
174
+        public void TestSimpleReadOrderBy2()
175
+        {
176
+            var serviceProvider = GetServiceProvider();
177
+
178
+            var crudDataAccess = serviceProvider.GetRequiredService<TableSimple1DataAccess>();
179
+
180
+            var partialRespone = new LuPartialFieldsDbo
181
+            {
182
+                Fields = new List<LuFieldDbo>
183
+                {
184
+                    new LuFieldDbo
185
+                    {
186
+                        Parts = new List<string>
187
+                        {
188
+                            "*"
189
+                        }
190
+                    }
191
+                }
192
+            };
193
+
194
+            var paginationParams = new LuPaginatedParamsDbo
195
+            {
196
+                Filter = new LuFilterDbo
197
+                {
198
+                    Expression = (Expression<Func<TableSimple1Dbo, bool>>) (x => true)
199
+                },
200
+                Page = 0,
201
+                OrderBy = new LuOrderByDbo
202
+                {
203
+                    OrderByFields = new List<LuOrderByFieldDbo>
204
+                    {
205
+                        new LuOrderByFieldDbo
206
+                        {
207
+                            Asc = false,
208
+                            Expression = (Expression<Func<TableSimple1Dbo, object>>) (x => x.Priority)
209
+                        }
210
+                    }
211
+                },
212
+                PerPage = 10
213
+            };
214
+
215
+            var result = crudDataAccess.Read(partialRespone, paginationParams);
216
+            Assert.NotNull(result);
217
+            Assert.Equal(LuStatus.Success.ToInt(), result.Status);
218
+            var items = result.Data;
219
+            Assert.NotNull(items);
220
+            Assert.Equal(2, items.Count);
221
+            Assert.NotNull(items.Data);
222
+            Assert.Equal(2, items.Data.Count);
223
+
224
+            var item = items.Data[0];
225
+            Assert.NotNull(item);
226
+            Assert.Equal("test1", item.Name);
227
+            Assert.Equal(42, item.Priority);
228
+            Assert.Null(item.Comment);
229
+            Assert.Null(item.OptionalInt);
230
+
231
+            item = items.Data[1];
232
+            Assert.NotNull(item);
233
+            Assert.Equal("test2", item.Name);
234
+            Assert.Equal(24, item.Priority);
235
+            Assert.Null(item.Comment);
236
+            Assert.Equal(1, item.OptionalInt);
237
+        }
238
+    }
239
+}

+ 45
- 0
Luticate2.Auth.Tests/DataAccess/Crud/Models/luticate2_unit_testsContext.cs View File

1
+using Microsoft.EntityFrameworkCore;
2
+
3
+namespace Luticate2.Auth.Tests.DataAccess.Crud.Models
4
+{
5
+    public partial class luticate2_unit_testsContext : DbContext
6
+    {
7
+        public luticate2_unit_testsContext()
8
+        {
9
+        }
10
+
11
+        public luticate2_unit_testsContext(DbContextOptions<luticate2_unit_testsContext> options)
12
+            : base(options)
13
+        {
14
+        }
15
+
16
+        public virtual DbSet<table_simple_1> table_simple_1 { get; set; }
17
+
18
+        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
19
+        {
20
+            if (!optionsBuilder.IsConfigured)
21
+            {
22
+#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
23
+                optionsBuilder.UseNpgsql("Host=localhost;Database=luticate2_unit_tests;Username=dev;Password=dev");
24
+            }
25
+        }
26
+
27
+        protected override void OnModelCreating(ModelBuilder modelBuilder)
28
+        {
29
+            modelBuilder.HasPostgresExtension("uuid-ossp");
30
+
31
+            modelBuilder.Entity<table_simple_1>(entity =>
32
+            {
33
+                entity.Property(e => e.id).HasDefaultValueSql("uuid_generate_v1()");
34
+
35
+                entity.Property(e => e.comment).IsRequired();
36
+
37
+                entity.Property(e => e.created_at).HasColumnType("timestamp with time zone");
38
+
39
+                entity.Property(e => e.name).IsRequired();
40
+
41
+                entity.Property(e => e.updated_at).HasColumnType("timestamp with time zone");
42
+            });
43
+        }
44
+    }
45
+}

+ 15
- 0
Luticate2.Auth.Tests/DataAccess/Crud/Models/table_simple_1.cs View File

1
+using System;
2
+
3
+namespace Luticate2.Auth.Tests.DataAccess.Crud.Models
4
+{
5
+    public partial class table_simple_1
6
+    {
7
+        public Guid id { get; set; }
8
+        public string name { get; set; }
9
+        public int priority { get; set; }
10
+        public string comment { get; set; }
11
+        public int? optional_int { get; set; }
12
+        public DateTime created_at { get; set; }
13
+        public DateTime? updated_at { get; set; }
14
+    }
15
+}

+ 5
- 0
Luticate2.Auth.Tests/Luticate2.Auth.Tests.csproj View File

4
     <IsPackable>false</IsPackable>
4
     <IsPackable>false</IsPackable>
5
   </PropertyGroup>
5
   </PropertyGroup>
6
   <ItemGroup>
6
   <ItemGroup>
7
+    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.4" />
7
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
8
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
9
+    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.1.2" />
8
     <PackageReference Include="xunit" Version="2.3.1" />
10
     <PackageReference Include="xunit" Version="2.3.1" />
9
     <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
11
     <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
10
     <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
12
     <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
12
   <ItemGroup>
14
   <ItemGroup>
13
     <ProjectReference Include="..\Luticate2.Auth\Luticate2.Auth.csproj" />
15
     <ProjectReference Include="..\Luticate2.Auth\Luticate2.Auth.csproj" />
14
   </ItemGroup>
16
   </ItemGroup>
17
+  <ItemGroup>
18
+    <Folder Include="DataAccess\Crud\Models" />
19
+  </ItemGroup>
15
 </Project>
20
 </Project>

+ 34
- 38
Luticate2.Auth.Tests/RandomTests.cs View File

1
-using Xunit;
1
+using System;
2
+using System.Linq.Expressions;
3
+using Luticate2.Auth.Tests.Business.ObjectConverter;
4
+using Luticate2.Auth.Utils.Business.Pagination;
5
+using Luticate2.Auth.Utils.Dbo;
6
+using Luticate2.Auth.Utils.Dbo.Pagination;
7
+using Luticate2.Auth.Utils.Dbo.Result;
8
+using Microsoft.CodeAnalysis.CSharp.Scripting;
9
+using Microsoft.CodeAnalysis.Scripting;
10
+using Xunit;
2
 
11
 
3
 namespace Luticate2.Auth.Tests
12
 namespace Luticate2.Auth.Tests
4
 {
13
 {
5
     public class RandomTests
14
     public class RandomTests
6
     {
15
     {
7
-
8
-//        [Fact]
9
-//        public void Test1()
10
-//        {
11
-//            var dbContext = new luticate2Context();
12
-//            var items = dbContext.LuGroups.Where(groups => groups.LuGroupsObjects.Any(x => x.Priority == 0));
13
-//            var list = items.ToList();
14
-//            Assert.NotNull(list);
15
-//            Assert.Equal(1, list.Count);
16
-//        }
17
-//
18
-//        [Fact]
19
-//        public void Test2()
20
-//        {
21
-//            var dbContext = new luticate2Context();
22
-//            var items = dbContext.LuGroups.OrderBy(x => x.Id).Where(groups => groups.LuGroupsObjects.Any(x => x.Priority == 0));
23
-//            var list = items.ToList();
24
-//            Assert.NotNull(list);
25
-//            Assert.Equal(1, list.Count);
26
-//        }
27
-//
28
-//        [Fact]
29
-//        public void Test3()
30
-//        {
31
-//            var dbContext = new luticate2Context();
32
-//            var items = dbContext.LuGroups.OrderBy(x => x.Id);
33
-//            var list = items.ToList();
34
-//            Assert.NotNull(list);
35
-//            Assert.Equal(2, list.Count);
36
-//        }
37
-
38
-//        protected void AddILuExpressionConverter<TTypeFrom, TTypeTo, TTypeImpl>(IServiceCollection services)
39
-//            where TTypeImpl : class, ILuExpressionConverter<TTypeFrom, TTypeTo>
40
-//        {
41
-//            services.AddSingleton<ILuExpressionConverter<TTypeFrom, TTypeTo>, TTypeImpl>();
42
-//            services.TryAddSingleton<ILuExpressionConverter<TTypeFrom>, TTypeImpl>();
43
-//        }
16
+        public static LuResult<LuFilterDbo> Parse<T>(string data)
17
+        {
18
+            if (string.IsNullOrWhiteSpace(data))
19
+            {
20
+                data = "true";
21
+            }
22
+            var completeData = $"(x) => ({data})";
23
+            try
24
+            {
25
+                var options = ScriptOptions.Default.AddReferences(typeof(RandomTests).Assembly,
26
+                    typeof(LuFilterParser).Assembly).AddImports("System.Linq.Enumerable");
27
+                var expr = CSharpScript.EvaluateAsync<Expression<Func<T, bool>>>(completeData, options).Result;
28
+                return LuResult<LuFilterDbo>.Ok(new LuFilterDbo
29
+                {
30
+                    Expression = expr
31
+                });
32
+            }
33
+            catch (Exception e)
34
+            {
35
+                return LuResult<LuFilterDbo>.Error(LuStatus.InputError.ToInt(), e, "Failed to parse filter expression");
36
+            }
37
+        }
44
 
38
 
45
         [Fact]
39
         [Fact]
46
         public void Test()
40
         public void Test()
49
 //            Expression<Func<TestDbo2, Guid>> expDbo = (x => parentScopeVar.Id);
43
 //            Expression<Func<TestDbo2, Guid>> expDbo = (x => parentScopeVar.Id);
50
 //            var type = typeof(ILuObjectConverterDescriptor<,>);
44
 //            var type = typeof(ILuObjectConverterDescriptor<,>);
51
 //            var gtype = type.MakeGenericType(typeof(int), typeof(Enumerable));
45
 //            var gtype = type.MakeGenericType(typeof(int), typeof(Enumerable));
46
+            var result = Parse<TestDbo1>("x.TestDbo2s.Any(y => y.Name == null)");
47
+//            var result = Parse<TestDbo1>("System.Linq.Enumerable.Any(x.TestDbo2s, y => y.Name == null)");
52
         }
48
         }
53
 
49
 
54
     }
50
     }

+ 2
- 2
Luticate2.Auth/Auth/Business/LuGroupsBusiness.cs View File

4
 using Luticate2.Auth.Auth.DataAccess;
4
 using Luticate2.Auth.Auth.DataAccess;
5
 using Luticate2.Auth.Auth.DataAccess.Models;
5
 using Luticate2.Auth.Auth.DataAccess.Models;
6
 using Luticate2.Auth.Auth.Dbo;
6
 using Luticate2.Auth.Auth.Dbo;
7
-using Luticate2.Auth.Utils.Business.Crud;
8
 using Luticate2.Auth.Utils.Business.Fields;
7
 using Luticate2.Auth.Utils.Business.Fields;
8
+using Luticate2.Auth.Utils.DataAccess.Crud;
9
 using Luticate2.Auth.Utils.Dbo.Fields;
9
 using Luticate2.Auth.Utils.Dbo.Fields;
10
 using Luticate2.Auth.Utils.Dbo.Result;
10
 using Luticate2.Auth.Utils.Dbo.Result;
11
 using Microsoft.EntityFrameworkCore;
11
 using Microsoft.EntityFrameworkCore;
12
 
12
 
13
 namespace Luticate2.Auth.Auth.Business
13
 namespace Luticate2.Auth.Auth.Business
14
 {
14
 {
15
-    public class LuGroupsBusiness : LuEfCrudBusiness<LuGroupDbo, LuGroups, Luticate2DbContext>
15
+    public class LuGroupsBusiness : LuEfCrudDataAccess<LuGroupDbo, LuGroups, Luticate2DbContext>
16
     {
16
     {
17
         public LuGroupsBusiness(IServiceProvider serviceProvider) : base(serviceProvider)
17
         public LuGroupsBusiness(IServiceProvider serviceProvider) : base(serviceProvider)
18
         {
18
         {

+ 2
- 1
Luticate2.Auth/Luticate2.Auth.csproj View File

3
     <TargetFramework>netcoreapp2.1</TargetFramework>
3
     <TargetFramework>netcoreapp2.1</TargetFramework>
4
   </PropertyGroup>
4
   </PropertyGroup>
5
   <ItemGroup>
5
   <ItemGroup>
6
-    <Folder Include="Utils" />
6
+    <Folder Include="Auth\DataAccess\Converters" />
7
+    <Folder Include="Utils\DataAccess" />
7
   </ItemGroup>
8
   </ItemGroup>
8
   <ItemGroup>
9
   <ItemGroup>
9
     <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="2.8.2" />
10
     <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="2.8.2" />

Luticate2.Auth/Utils/Business/ExpressionConverter/LuExpressionConverterVisitor.cs → Luticate2.Auth/Utils/Business/Converters/ExpressionConverter/LuExpressionConverterVisitor.cs View File

7
 using Luticate2.Auth.Utils.Exceptions;
7
 using Luticate2.Auth.Utils.Exceptions;
8
 using Luticate2.Auth.Utils.Interfaces;
8
 using Luticate2.Auth.Utils.Interfaces;
9
 
9
 
10
-namespace Luticate2.Auth.Utils.Business.ExpressionConverter
10
+namespace Luticate2.Auth.Utils.Business.Converters.ExpressionConverter
11
 {
11
 {
12
     public interface ILuExpressionConverterVisitorOptions
12
     public interface ILuExpressionConverterVisitorOptions
13
     {
13
     {
59
             }
59
             }
60
         }
60
         }
61
 
61
 
62
-        protected ILuObjectConverterDescriptor GetConverterDescriptor(Type typeFrom, Type typeTo)
62
+        protected LuResult<ILuObjectConverterDescriptor> GetConverterDescriptor(Type typeFrom, Type typeTo)
63
         {
63
         {
64
             var type = typeof(ILuObjectConverterDescriptor<,>);
64
             var type = typeof(ILuObjectConverterDescriptor<,>);
65
             var gtype = type.MakeGenericType(typeFrom, typeTo);
65
             var gtype = type.MakeGenericType(typeFrom, typeTo);
68
             {
68
             {
69
                 descriptor = (ILuObjectConverterDescriptor) ServiceProvider.GetService(typeof(ILuObjectConverterDescriptorIdentity));
69
                 descriptor = (ILuObjectConverterDescriptor) ServiceProvider.GetService(typeof(ILuObjectConverterDescriptorIdentity));
70
             }
70
             }
71
-            return descriptor;
71
+
72
+            if (descriptor != null)
73
+            {
74
+                return LuResult<ILuObjectConverterDescriptor>.Ok(descriptor);
75
+            }
76
+
77
+            return LuResult<ILuObjectConverterDescriptor>.Error(LuStatus.InternalError.ToInt(),
78
+                $"Could not find converter descriptor for {typeFrom} => {typeTo}");
72
         }
79
         }
73
 
80
 
74
         protected LuResult<Expression> ConvertMemberExpression(MemberExpression memberExpression, Expression newExpression, Type typeTo)
81
         protected LuResult<Expression> ConvertMemberExpression(MemberExpression memberExpression, Expression newExpression, Type typeTo)
75
         {
82
         {
76
             var typeFrom = memberExpression.Expression == null ? memberExpression.Member.DeclaringType : memberExpression.Expression.Type;
83
             var typeFrom = memberExpression.Expression == null ? memberExpression.Member.DeclaringType : memberExpression.Expression.Type;
77
-            var descriptor = GetConverterDescriptor(typeFrom, typeTo);
78
-            if (descriptor == null)
84
+            var descriptorResult = GetConverterDescriptor(typeFrom, typeTo);
85
+            if (!descriptorResult)
79
             {
86
             {
80
-                return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
81
-                    $"Could not find converter descriptor for {typeFrom} => {typeTo}");
87
+                return descriptorResult.To<Expression>();
82
             }
88
             }
89
+            var descriptor = descriptorResult.Data;
83
 
90
 
84
-            var valueExpression = descriptor.GetMemberValueExpression(memberExpression.Member, Options.DescriptorOptions);
85
-            if (valueExpression == null)
91
+            var valueExpressionResult = descriptor.GetMemberValueExpression(memberExpression.Member, Options.DescriptorOptions);
92
+            if (!valueExpressionResult)
86
             {
93
             {
87
-                return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
88
-                    $"Could not find converter descriptor lambda for {typeFrom} => {typeTo} => {memberExpression.Member}");
94
+                return valueExpressionResult.To<Expression>();
89
             }
95
             }
96
+            var valueExpression = valueExpressionResult.Data;
97
+
90
             if (valueExpression.Parameters[0].Type != typeTo)
98
             if (valueExpression.Parameters[0].Type != typeTo)
91
             {
99
             {
92
                 return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
100
                 return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
101
             var visitor = new LuExpressionParamReplaceVisitor(Options.VisitorOptions);
109
             var visitor = new LuExpressionParamReplaceVisitor(Options.VisitorOptions);
102
             newExpression = visitor.Visit(valueExpression.Body);
110
             newExpression = visitor.Visit(valueExpression.Body);
103
 
111
 
104
-            if (newExpression != null)
105
-            {
106
-                Options.Parameters.Remove(valueExpression.Parameters[0]);
107
-            }
112
+            Options.Parameters.Remove(valueExpression.Parameters[0]);
108
 
113
 
109
             return LuResult<Expression>.Ok(newExpression);
114
             return LuResult<Expression>.Ok(newExpression);
110
         }
115
         }
112
         protected LuResult<Expression> ConvertMethodCallExpression(MethodCallExpression methodCallExpression, Expression newExpression, Type typeTo)
117
         protected LuResult<Expression> ConvertMethodCallExpression(MethodCallExpression methodCallExpression, Expression newExpression, Type typeTo)
113
         {
118
         {
114
             var typeFrom = methodCallExpression.Object == null ? methodCallExpression.Method.DeclaringType : methodCallExpression.Object.Type;
119
             var typeFrom = methodCallExpression.Object == null ? methodCallExpression.Method.DeclaringType : methodCallExpression.Object.Type;
115
-            var descriptor = GetConverterDescriptor(typeFrom, typeTo);
116
-            if (descriptor == null)
120
+            var descriptorResult = GetConverterDescriptor(typeFrom, typeTo);
121
+            if (!descriptorResult)
117
             {
122
             {
118
-                return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
119
-                    $"Could not find converter descriptor for {typeFrom} => {typeTo}");
123
+                return descriptorResult.To<Expression>();
120
             }
124
             }
125
+            var descriptor = descriptorResult.Data;
121
 
126
 
122
-            var valueExpression = descriptor.GetMethodValueExpression(methodCallExpression.Method, Options.DescriptorOptions);
123
-            if (valueExpression == null)
127
+            var valueExpressionResult = descriptor.GetMethodValueExpression(methodCallExpression.Method, Options.DescriptorOptions);
128
+            if (!valueExpressionResult)
124
             {
129
             {
125
-                return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
126
-                    $"Could not find converter descriptor lambda for {typeFrom} => {typeTo} => {methodCallExpression.Method}");
130
+                return valueExpressionResult.To<Expression>();
127
             }
131
             }
132
+            var valueExpression = valueExpressionResult.Data;
133
+
128
             if (valueExpression.Parameters.Count != methodCallExpression.Arguments.Count + 1)
134
             if (valueExpression.Parameters.Count != methodCallExpression.Arguments.Count + 1)
129
             {
135
             {
130
                 return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
136
                 return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
173
             else if (currentExp is MemberExpression memberExpression)
179
             else if (currentExp is MemberExpression memberExpression)
174
             {
180
             {
175
                 var typeFrom = memberExpression.Member.DeclaringType;
181
                 var typeFrom = memberExpression.Member.DeclaringType;
176
-                var typeTo = Options.TypeConverter.ConvertType(typeFrom);
182
+                var typeToResult = Options.TypeConverter.ConvertType(typeFrom);
183
+                if (!typeToResult)
184
+                {
185
+                    return typeToResult.To<Expression>();
186
+                }
187
+                var typeTo = typeToResult.Data;
177
 
188
 
178
                 var convertResult = ConvertMemberExpression(memberExpression, null, typeTo);
189
                 var convertResult = ConvertMemberExpression(memberExpression, null, typeTo);
179
                 if (!convertResult)
190
                 if (!convertResult)
186
             else if (currentExp is MethodCallExpression methodCallExpression)
197
             else if (currentExp is MethodCallExpression methodCallExpression)
187
             {
198
             {
188
                 var typeFrom = methodCallExpression.Method.DeclaringType;
199
                 var typeFrom = methodCallExpression.Method.DeclaringType;
189
-                var typeTo = Options.TypeConverter.ConvertType(typeFrom);
200
+
201
+                var typeToResult = Options.TypeConverter.ConvertType(typeFrom);
202
+                if (!typeToResult)
203
+                {
204
+                    return typeToResult.To<Expression>();
205
+                }
206
+                var typeTo = typeToResult.Data;
190
 
207
 
191
                 var convertResult = ConvertMethodCallExpression(methodCallExpression, null, typeTo);
208
                 var convertResult = ConvertMethodCallExpression(methodCallExpression, null, typeTo);
192
                 if (!convertResult)
209
                 if (!convertResult)
193
                 {
210
                 {
194
                     return convertResult;
211
                     return convertResult;
195
                 }
212
                 }
196
-
197
                 newExpression = convertResult.Data;
213
                 newExpression = convertResult.Data;
198
             }
214
             }
199
             else
215
             else
271
             var convertedParams = new List<ParameterExpression>();
287
             var convertedParams = new List<ParameterExpression>();
272
             foreach (var parameter in node.Parameters)
288
             foreach (var parameter in node.Parameters)
273
             {
289
             {
274
-                var convertedParam = Expression.Parameter(Options.TypeConverter.ConvertType(parameter.Type));
290
+                var convertedTypeResult = Options.TypeConverter.ConvertType(parameter.Type).ThrowIfNotSuccess();
291
+                var convertedType = convertedTypeResult.Data;
292
+                var convertedParam = Expression.Parameter(convertedType);
275
                 Options.Parameters.Add(parameter, convertedParam);
293
                 Options.Parameters.Add(parameter, convertedParam);
276
                 convertedParams.Add(convertedParam);
294
                 convertedParams.Add(convertedParam);
277
             }
295
             }
296
+
278
             var convertedBody = Visit(node.Body);
297
             var convertedBody = Visit(node.Body);
279
             foreach (var parameter in node.Parameters)
298
             foreach (var parameter in node.Parameters)
280
             {
299
             {

Luticate2.Auth/Utils/Business/ExpressionConverter/LuExpressionParamReplaceVisitor.cs → Luticate2.Auth/Utils/Business/Converters/ExpressionConverter/LuExpressionParamReplaceVisitor.cs View File

4
 using Luticate2.Auth.Utils.Dbo.Result;
4
 using Luticate2.Auth.Utils.Dbo.Result;
5
 using Luticate2.Auth.Utils.Exceptions;
5
 using Luticate2.Auth.Utils.Exceptions;
6
 
6
 
7
-namespace Luticate2.Auth.Utils.Business.ExpressionConverter
7
+namespace Luticate2.Auth.Utils.Business.Converters.ExpressionConverter
8
 {
8
 {
9
     public interface ILuExpressionParamReplaceVisitorOptions
9
     public interface ILuExpressionParamReplaceVisitorOptions
10
     {
10
     {

+ 29
- 5
Luticate2.Auth/Utils/Business/Converters/LuConvertersAllocator.cs View File

1
 using System;
1
 using System;
2
 using System.Collections.Generic;
2
 using System.Collections.Generic;
3
 using System.Collections.ObjectModel;
3
 using System.Collections.ObjectModel;
4
+using Luticate2.Auth.Utils.Dbo;
5
+using Luticate2.Auth.Utils.Dbo.Result;
4
 using Luticate2.Auth.Utils.Interfaces;
6
 using Luticate2.Auth.Utils.Interfaces;
5
 
7
 
6
 namespace Luticate2.Auth.Utils.Business.Converters
8
 namespace Luticate2.Auth.Utils.Business.Converters
16
             {typeof(List<>), typeof(List<>)}
18
             {typeof(List<>), typeof(List<>)}
17
         };
19
         };
18
 
20
 
19
-        public virtual object GetInstance(Type type)
21
+        public virtual LuResult<object> CreateInstance(Type type)
22
+        {
23
+            try
24
+            {
25
+                var obj = Activator.CreateInstance(type);
26
+                // TODO Nullable<T> (and maybe others) gets null instead of object
27
+//                if (obj != null)
28
+//                {
29
+                    return LuResult<object>.Ok(obj);
30
+//                }
31
+
32
+//                return LuResult<object>.Error(LuStatus.InternalError.ToInt(),
33
+//                    $"Could not instantiate type {type}");
34
+            }
35
+            catch (Exception e)
36
+            {
37
+                return LuResult<object>.Error(LuStatus.InternalError.ToInt(), e);
38
+            }
39
+        }
40
+
41
+        public virtual LuResult<object> GetInstance(Type type)
20
         {
42
         {
21
             if (type.IsGenericType)
43
             if (type.IsGenericType)
22
             {
44
             {
25
                 if (listTypes.ContainsKey(gtype))
47
                 if (listTypes.ContainsKey(gtype))
26
                 {
48
                 {
27
                     var glistType = listTypes[gtype].MakeGenericType(type.GenericTypeArguments[0]);
49
                     var glistType = listTypes[gtype].MakeGenericType(type.GenericTypeArguments[0]);
28
-                    return Activator.CreateInstance(glistType);
50
+                    var listActivateResult = CreateInstance(glistType);
51
+                    return listActivateResult;
29
                 }
52
                 }
30
             }
53
             }
31
 
54
 
32
-            if (type == typeof(string) || type.IsPrimitive)
55
+            if (type == typeof(string))
33
             {
56
             {
34
-                return null;
57
+                return LuResult<object>.Ok("");
35
             }
58
             }
36
 
59
 
37
-            return Activator.CreateInstance(type);
60
+            var activateResult = CreateInstance(type);
61
+            return activateResult;
38
         }
62
         }
39
     }
63
     }
40
 }
64
 }

+ 55
- 11
Luticate2.Auth/Utils/Business/Converters/LuConvertersExtensions.cs View File

1
+using System;
1
 using System.Collections.Generic;
2
 using System.Collections.Generic;
2
 using System.Collections.ObjectModel;
3
 using System.Collections.ObjectModel;
3
 using System.Linq;
4
 using System.Linq;
4
-using Luticate2.Auth.Utils.Business.ObjectConverter;
5
-using Luticate2.Auth.Utils.Business.ObjectConverterDescriptor;
5
+using Luticate2.Auth.Utils.Business.Converters.ObjectConverter;
6
+using Luticate2.Auth.Utils.Business.Converters.ObjectConverterDescriptor;
6
 using Luticate2.Auth.Utils.Interfaces;
7
 using Luticate2.Auth.Utils.Interfaces;
7
 using Microsoft.Extensions.DependencyInjection;
8
 using Microsoft.Extensions.DependencyInjection;
8
 using Microsoft.Extensions.DependencyInjection.Extensions;
9
 using Microsoft.Extensions.DependencyInjection.Extensions;
11
 {
12
 {
12
     public static class LuConvertersExtensions
13
     public static class LuConvertersExtensions
13
     {
14
     {
15
+        public class LuObjectConvertersOptions
16
+        {
17
+            public List<Type> ValueTypes { get; set; }
18
+
19
+            public List<Type> ListTypes { get; set; }
20
+        }
21
+
14
         public static void AddLuObjectConverterDescriptors(this IServiceCollection services)
22
         public static void AddLuObjectConverterDescriptors(this IServiceCollection services)
15
         {
23
         {
16
             var typeILuObjectConverterDescriptor = typeof(ILuObjectConverterDescriptor<,>);
24
             var typeILuObjectConverterDescriptor = typeof(ILuObjectConverterDescriptor<,>);
21
             services.AddSingleton(typeILuObjectConverterDescriptorEnumerable, typeof(LuObjectConverterDescriptorEnumerable));
29
             services.AddSingleton(typeILuObjectConverterDescriptorEnumerable, typeof(LuObjectConverterDescriptorEnumerable));
22
         }
30
         }
23
 
31
 
24
-        public static void AddLuObjectConverters(this IServiceCollection services)
32
+        public static void AddLuObjectConverters(this IServiceCollection services, Action<LuObjectConvertersOptions> optionsAction = null)
25
         {
33
         {
26
             var typeILuObjectConverter = typeof(ILuObjectConverter<,>);
34
             var typeILuObjectConverter = typeof(ILuObjectConverter<,>);
27
-            var listTypes = new []
35
+
36
+            var options = new LuObjectConvertersOptions
28
             {
37
             {
29
-                typeof(IEnumerable<>),
30
-                typeof(ICollection<>),
31
-                typeof(Collection<>),
32
-                typeof(IList<>),
33
-                typeof(List<>)
38
+                ValueTypes = new List<Type>
39
+                {
40
+                    typeof(bool),
41
+
42
+                    typeof(decimal),
43
+                    typeof(double),
44
+                    typeof(float),
45
+
46
+                    typeof(sbyte),
47
+                    typeof(byte),
48
+                    typeof(char),
49
+                    typeof(short),
50
+                    typeof(ushort),
51
+                    typeof(int),
52
+                    typeof(uint),
53
+                    typeof(long),
54
+                    typeof(ulong),
55
+
56
+                    typeof(Guid),
57
+
58
+                    typeof(DateTime),
59
+                    typeof(DateTimeOffset),
60
+                    typeof(TimeSpan)
61
+                },
62
+                ListTypes = new List<Type>
63
+                {
64
+                    typeof(IEnumerable<>),
65
+                    typeof(ICollection<>),
66
+                    typeof(Collection<>),
67
+                    typeof(IList<>),
68
+                    typeof(List<>)
69
+                }
34
             };
70
             };
35
 
71
 
72
+            optionsAction?.Invoke(options);
73
+
36
             services.TryAddSingleton<ILuConvertersAllocator, LuConvertersAllocator>();
74
             services.TryAddSingleton<ILuConvertersAllocator, LuConvertersAllocator>();
37
             services.AddSingleton<ILuObjectConverterIdentity, LuObjectConverterIdentity>();
75
             services.AddSingleton<ILuObjectConverterIdentity, LuObjectConverterIdentity>();
38
-            foreach (var typeFrom in listTypes)
76
+            foreach (var valueType in options.ValueTypes)
77
+            {
78
+                var nullableValueType = typeof(Nullable<>).MakeGenericType(valueType);
79
+                var converterType = typeILuObjectConverter.MakeGenericType(valueType, nullableValueType);
80
+                services.AddSingleton(converterType, typeof(LuObjectConverterIdentity));
81
+            }
82
+            foreach (var typeFrom in options.ListTypes)
39
             {
83
             {
40
-                foreach (var typeTo in listTypes)
84
+                foreach (var typeTo in options.ListTypes)
41
                 {
85
                 {
42
                     services.AddSingleton(typeILuObjectConverter.MakeGenericType(typeFrom, typeTo), typeof(LuObjectConverterLists));
86
                     services.AddSingleton(typeILuObjectConverter.MakeGenericType(typeFrom, typeTo), typeof(LuObjectConverterLists));
43
                 }
87
                 }

+ 1
- 1
Luticate2.Auth/Utils/Business/Converters/LuConvertersOptions.cs View File

1
 using System.Collections.Generic;
1
 using System.Collections.Generic;
2
 using System.Linq.Expressions;
2
 using System.Linq.Expressions;
3
-using Luticate2.Auth.Utils.Business.ExpressionConverter;
3
+using Luticate2.Auth.Utils.Business.Converters.ExpressionConverter;
4
 using Luticate2.Auth.Utils.Interfaces;
4
 using Luticate2.Auth.Utils.Interfaces;
5
 
5
 
6
 namespace Luticate2.Auth.Utils.Business.Converters
6
 namespace Luticate2.Auth.Utils.Business.Converters

+ 4
- 3
Luticate2.Auth/Utils/Business/Converters/LuConvertersTypeConverter.cs View File

1
 using System;
1
 using System;
2
 using System.Collections.Generic;
2
 using System.Collections.Generic;
3
+using Luticate2.Auth.Utils.Dbo.Result;
3
 using Luticate2.Auth.Utils.Interfaces;
4
 using Luticate2.Auth.Utils.Interfaces;
4
 
5
 
5
 namespace Luticate2.Auth.Utils.Business.Converters
6
 namespace Luticate2.Auth.Utils.Business.Converters
13
             Types = types;
14
             Types = types;
14
         }
15
         }
15
 
16
 
16
-        public Type ConvertType(Type typeFrom)
17
+        public LuResult<Type> ConvertType(Type typeFrom)
17
         {
18
         {
18
             if (Types.ContainsKey(typeFrom))
19
             if (Types.ContainsKey(typeFrom))
19
             {
20
             {
20
-                return Types[typeFrom];
21
+                return LuResult<Type>.Ok(Types[typeFrom]);
21
             }
22
             }
22
 
23
 
23
-            return typeFrom;
24
+            return LuResult<Type>.Ok(typeFrom);
24
         }
25
         }
25
     }
26
     }
26
 }
27
 }

Luticate2.Auth/Utils/Business/ObjectConverter/LuObjectConverterIdentity.cs → Luticate2.Auth/Utils/Business/Converters/ObjectConverter/LuObjectConverterIdentity.cs View File

2
 using Luticate2.Auth.Utils.Dbo.Result;
2
 using Luticate2.Auth.Utils.Dbo.Result;
3
 using Luticate2.Auth.Utils.Interfaces;
3
 using Luticate2.Auth.Utils.Interfaces;
4
 
4
 
5
-namespace Luticate2.Auth.Utils.Business.ObjectConverter
5
+namespace Luticate2.Auth.Utils.Business.Converters.ObjectConverter
6
 {
6
 {
7
     public class LuObjectConverterIdentity : ILuObjectConverterIdentity
7
     public class LuObjectConverterIdentity : ILuObjectConverterIdentity
8
     {
8
     {

Luticate2.Auth/Utils/Business/ObjectConverter/LuObjectConverterLists.cs → Luticate2.Auth/Utils/Business/Converters/ObjectConverter/LuObjectConverterLists.cs View File

6
 using Luticate2.Auth.Utils.Dbo.Result;
6
 using Luticate2.Auth.Utils.Dbo.Result;
7
 using Luticate2.Auth.Utils.Interfaces;
7
 using Luticate2.Auth.Utils.Interfaces;
8
 
8
 
9
-namespace Luticate2.Auth.Utils.Business.ObjectConverter
9
+namespace Luticate2.Auth.Utils.Business.Converters.ObjectConverter
10
 {
10
 {
11
     public class LuObjectConverterLists : ILuObjectConverter
11
     public class LuObjectConverterLists : ILuObjectConverter
12
     {
12
     {
19
 
19
 
20
         public LuResult<object> Convert(object srcObj, object dstObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
20
         public LuResult<object> Convert(object srcObj, object dstObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
21
         {
21
         {
22
+            var dstObjType = dstObj.GetType();
23
+            var addMethodInfo = dstObjType.GetMethod(nameof(ICollection<object>.Add));
24
+            if (addMethodInfo == null)
25
+            {
26
+                return LuResult<object>.Error(LuStatus.InternalError.ToInt(),
27
+                    $"Could not find get method info {nameof(ICollection)}.{nameof(ICollection<object>.Add)} for type {dstObjType}");
28
+            }
29
+
22
             if (srcObj is IEnumerable enumerable)
30
             if (srcObj is IEnumerable enumerable)
23
             {
31
             {
24
                 foreach (var subSrcObj in enumerable)
32
                 foreach (var subSrcObj in enumerable)
27
                     if (subSrcObj != null)
35
                     if (subSrcObj != null)
28
                     {
36
                     {
29
                         var typeFrom = subSrcObj.GetType();
37
                         var typeFrom = subSrcObj.GetType();
30
-                        var typeTo = options.TypeConverter.ConvertType(typeFrom);
31
-                        // TODO Handle converter == null
32
-                        var converter = LuObjectConverterUtils.GetObjectConverter(ServiceProvider, typeFrom, typeTo);
33
-                        var subDstObj = options.Allocator.GetInstance(typeTo);
38
+
39
+                        var typeToResult = options.TypeConverter.ConvertType(typeFrom);
40
+                        if (!typeToResult)
41
+                        {
42
+                            return typeToResult.To<object>();
43
+                        }
44
+                        var typeTo = typeToResult.Data;
45
+
46
+                        var converterResult = LuObjectConverterUtils.GetObjectConverter(ServiceProvider, typeFrom, typeTo);
47
+                        if (!converterResult)
48
+                        {
49
+                            return converterResult.To<object>();
50
+                        }
51
+                        var converter = converterResult.Data;
52
+
53
+                        var subDstObjResult = options.Allocator.GetInstance(typeTo);
54
+                        if (!subDstObjResult)
55
+                        {
56
+                            return subDstObjResult.To<object>();
57
+                        }
58
+                        var subDstObj = subDstObjResult.Data;
59
+
34
                         var convertResult = converter.Convert(subSrcObj, subDstObj, path, fields, options);
60
                         var convertResult = converter.Convert(subSrcObj, subDstObj, path, fields, options);
35
                         if (!convertResult)
61
                         if (!convertResult)
36
                         {
62
                         {
37
                             return convertResult;
63
                             return convertResult;
38
                         }
64
                         }
39
-
40
                         convertedObj = convertResult.Data;
65
                         convertedObj = convertResult.Data;
41
                     }
66
                     }
42
 
67
 
43
-                    var methodInfo = dstObj.GetType().GetMethod(nameof(ICollection<object>.Add));
44
-                    methodInfo.Invoke(dstObj, new []{convertedObj});
68
+                    addMethodInfo.Invoke(dstObj, new []{convertedObj});
45
                 }
69
                 }
46
 
70
 
47
                 return LuResult<object>.Ok(dstObj);
71
                 return LuResult<object>.Ok(dstObj);

Luticate2.Auth/Utils/Business/ObjectConverter/LuObjectConverterPoco.cs → Luticate2.Auth/Utils/Business/Converters/ObjectConverter/LuObjectConverterPoco.cs View File

1
 using System;
1
 using System;
2
 using Luticate2.Auth.Utils.Business.Fields;
2
 using Luticate2.Auth.Utils.Business.Fields;
3
 using Luticate2.Auth.Utils.Business.Utils;
3
 using Luticate2.Auth.Utils.Business.Utils;
4
+using Luticate2.Auth.Utils.Dbo;
4
 using Luticate2.Auth.Utils.Dbo.Fields;
5
 using Luticate2.Auth.Utils.Dbo.Fields;
5
 using Luticate2.Auth.Utils.Dbo.Result;
6
 using Luticate2.Auth.Utils.Dbo.Result;
6
 using Luticate2.Auth.Utils.Interfaces;
7
 using Luticate2.Auth.Utils.Interfaces;
7
 using Microsoft.Extensions.DependencyInjection;
8
 using Microsoft.Extensions.DependencyInjection;
8
 
9
 
9
-namespace Luticate2.Auth.Utils.Business.ObjectConverter
10
+namespace Luticate2.Auth.Utils.Business.Converters.ObjectConverter
10
 {
11
 {
11
     public class LuObjectConverterPoco<TTypeFrom, TTypeTo> : ILuObjectConverter<TTypeFrom, TTypeTo>
12
     public class LuObjectConverterPoco<TTypeFrom, TTypeTo> : ILuObjectConverter<TTypeFrom, TTypeTo>
12
     {
13
     {
23
             {
24
             {
24
                 return LuResult<object>.Ok(default(TTypeTo));
25
                 return LuResult<object>.Ok(default(TTypeTo));
25
             }
26
             }
26
-            // TODO Handle descriptor == null
27
-            var descriptor = ServiceProvider.GetService<ILuObjectConverterDescriptor<TTypeTo, TTypeFrom>>();
27
+
28
             var typeTo = typeof(TTypeTo);
28
             var typeTo = typeof(TTypeTo);
29
-//            var dstObj = options.Allocator.GetInstance(typeTo);
29
+            var descriptor = ServiceProvider.GetService<ILuObjectConverterDescriptor<TTypeTo, TTypeFrom>>();
30
+            if (descriptor == null)
31
+            {
32
+                return LuResult<object>.Error(LuStatus.InternalError.ToInt(),
33
+                    $"Could not find converter descriptor for {typeTo} => {typeof(TTypeFrom)}");
34
+            }
30
 
35
 
31
             foreach (var memberInfo in typeTo.GetProperties())
36
             foreach (var memberInfo in typeTo.GetProperties())
32
             {
37
             {
35
                 {
40
                 {
36
                     continue;
41
                     continue;
37
                 }
42
                 }
38
-                // TODO Handle srcPropExpression == null
39
-                var subSrcPropExpression = descriptor.GetMemberValueExpression(memberInfo, options.DescriptorOptions);
43
+
44
+                var subSrcPropExpressionResult = descriptor.GetMemberValueExpression(memberInfo, options.DescriptorOptions);
45
+                if (!subSrcPropExpressionResult)
46
+                {
47
+                    return subSrcPropExpressionResult.To<object>();
48
+                }
49
+                var subSrcPropExpression = subSrcPropExpressionResult.Data;
50
+
40
                 var subSrcPropDelegate = subSrcPropExpression.Compile();
51
                 var subSrcPropDelegate = subSrcPropExpression.Compile();
41
                 var subSrcProp = subSrcPropDelegate.DynamicInvoke(srcObj);
52
                 var subSrcProp = subSrcPropDelegate.DynamicInvoke(srcObj);
42
                 var subSrcType = subSrcProp != null ? subSrcProp.GetType() : subSrcPropExpression.ReturnType;
53
                 var subSrcType = subSrcProp != null ? subSrcProp.GetType() : subSrcPropExpression.ReturnType;
43
                 var subDstType = memberInfo.GetUnderlyingType();
54
                 var subDstType = memberInfo.GetUnderlyingType();
44
 
55
 
45
-                // TODO Handle objectConverter == null
46
-                var objectConverter = LuObjectConverterUtils.GetObjectConverter(ServiceProvider, subSrcType, subDstType);
47
-                var subDstObj = options.Allocator.GetInstance(subDstType);
56
+                var objectConverterResult = LuObjectConverterUtils.GetObjectConverter(ServiceProvider, subSrcType, subDstType);
57
+                if (!objectConverterResult)
58
+                {
59
+                    return objectConverterResult.To<object>();
60
+                }
61
+                var objectConverter = objectConverterResult.Data;
62
+
63
+                var subDstObjResult = options.Allocator.GetInstance(subDstType);
64
+                if (!subDstObjResult)
65
+                {
66
+                    return subDstObjResult.To<object>();
67
+                }
68
+                var subDstObj = subDstObjResult.Data;
69
+
48
                 var dstPropResult = objectConverter.Convert(subSrcProp, subDstObj, memberPath, fields, options);
70
                 var dstPropResult = objectConverter.Convert(subSrcProp, subDstObj, memberPath, fields, options);
49
                 if (!dstPropResult)
71
                 if (!dstPropResult)
50
                 {
72
                 {
51
                     return dstPropResult;
73
                     return dstPropResult;
52
                 }
74
                 }
53
-
54
                 var dstProp = dstPropResult.Data;
75
                 var dstProp = dstPropResult.Data;
55
 
76
 
56
                 var result = LuExpressionUtils.SetValueFromMember(memberInfo, dstObj, dstProp);
77
                 var result = LuExpressionUtils.SetValueFromMember(memberInfo, dstObj, dstProp);

Luticate2.Auth/Utils/Business/ObjectConverter/LuObjectConverterUtils.cs → Luticate2.Auth/Utils/Business/Converters/ObjectConverter/LuObjectConverterUtils.cs View File

1
 using System;
1
 using System;
2
+using Luticate2.Auth.Utils.Dbo;
3
+using Luticate2.Auth.Utils.Dbo.Result;
2
 using Luticate2.Auth.Utils.Interfaces;
4
 using Luticate2.Auth.Utils.Interfaces;
3
 
5
 
4
-namespace Luticate2.Auth.Utils.Business.ObjectConverter
6
+namespace Luticate2.Auth.Utils.Business.Converters.ObjectConverter
5
 {
7
 {
6
     public static class LuObjectConverterUtils
8
     public static class LuObjectConverterUtils
7
     {
9
     {
8
-        public static ILuObjectConverter GetObjectConverter(IServiceProvider serviceProvider, Type typeFrom, Type typeTo)
10
+        public static LuResult<ILuObjectConverter> GetObjectConverter(IServiceProvider serviceProvider, Type typeFrom, Type typeTo)
9
         {
11
         {
10
             var type = typeof(ILuObjectConverter<,>);
12
             var type = typeof(ILuObjectConverter<,>);
11
             var gtype = type.MakeGenericType(typeFrom, typeTo);
13
             var gtype = type.MakeGenericType(typeFrom, typeTo);
32
                 converter = (ILuObjectConverter) serviceProvider.GetService(typeof(ILuObjectConverterIdentity));
34
                 converter = (ILuObjectConverter) serviceProvider.GetService(typeof(ILuObjectConverterIdentity));
33
             }
35
             }
34
 
36
 
35
-            return converter;
37
+            if (converter != null)
38
+            {
39
+                return LuResult<ILuObjectConverter>.Ok(converter);
40
+            }
41
+
42
+            return LuResult<ILuObjectConverter>.Error(LuStatus.InternalError.ToInt(),
43
+                $"Could not find converter for {typeFrom} => {typeTo}");
36
         }
44
         }
37
     }
45
     }
38
 }
46
 }

Luticate2.Auth/Utils/Business/ObjectConverterDescriptor/LuObjectConverterDescriptor.cs → Luticate2.Auth/Utils/Business/Converters/ObjectConverterDescriptor/LuObjectConverterDescriptor.cs View File

4
 using System.Linq.Expressions;
4
 using System.Linq.Expressions;
5
 using System.Reflection;
5
 using System.Reflection;
6
 using Luticate2.Auth.Utils.Business.Utils;
6
 using Luticate2.Auth.Utils.Business.Utils;
7
+using Luticate2.Auth.Utils.Dbo;
8
+using Luticate2.Auth.Utils.Dbo.Result;
7
 using Luticate2.Auth.Utils.Interfaces;
9
 using Luticate2.Auth.Utils.Interfaces;
8
 
10
 
9
-namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
11
+namespace Luticate2.Auth.Utils.Business.Converters.ObjectConverterDescriptor
10
 {
12
 {
11
     public class LuObjectConverterDescriptor : ILuObjectConverterDescriptor
13
     public class LuObjectConverterDescriptor : ILuObjectConverterDescriptor
12
     {
14
     {
62
                 (info, converter) =>
64
                 (info, converter) =>
63
                 {
65
                 {
64
                     var genericMethodInfo = info.GetGenericMethodDefinition();
66
                     var genericMethodInfo = info.GetGenericMethodDefinition();
65
-                    var templateMethodInfo = genericMethodInfo.MakeGenericMethod(info.GetGenericArguments().Select(converter.ConvertType).ToArray());
67
+                    // TODO Handle converter.ConvertType(type) errors
68
+                    var templateMethodInfo = genericMethodInfo.MakeGenericMethod(info.GetGenericArguments().Select(type => converter.ConvertType(type).Data).ToArray());
66
 
69
 
67
                     var lambdaParams = new List<ParameterExpression>
70
                     var lambdaParams = new List<ParameterExpression>
68
                     {
71
                     {
82
             );
85
             );
83
         }
86
         }
84
 
87
 
85
-        public LambdaExpression GetMemberValueExpression(MemberInfo memberInfo, ILuObjectConverterDescriptorOptions options)
88
+        public LuResult<LambdaExpression> GetMemberValueExpression(MemberInfo memberInfo, ILuObjectConverterDescriptorOptions options)
86
         {
89
         {
87
             if (StaticMemberConverters.ContainsKey(memberInfo))
90
             if (StaticMemberConverters.ContainsKey(memberInfo))
88
             {
91
             {
89
-                return StaticMemberConverters[memberInfo].ValueExpression;
92
+                return LuResult<LambdaExpression>.Ok(StaticMemberConverters[memberInfo].ValueExpression);
90
             }
93
             }
91
 
94
 
92
-            return null;
95
+            return LuResult<LambdaExpression>.Error(LuStatus.InternalError.ToInt(),
96
+                $"Could not get member value expression from member info {memberInfo}");
93
         }
97
         }
94
 
98
 
95
-        public LambdaExpression GetMethodValueExpression(MethodInfo methodInfo, ILuObjectConverterDescriptorOptions options)
99
+        public LuResult<LambdaExpression> GetMethodValueExpression(MethodInfo methodInfo, ILuObjectConverterDescriptorOptions options)
96
         {
100
         {
97
             if (StaticMethodConverters.ContainsKey(methodInfo))
101
             if (StaticMethodConverters.ContainsKey(methodInfo))
98
             {
102
             {
99
-                return StaticMethodConverters[methodInfo].ValueExpression;
103
+                return LuResult<LambdaExpression>.Ok(StaticMethodConverters[methodInfo].ValueExpression);
100
             }
104
             }
101
 
105
 
102
             var genericMethodInfo = methodInfo.GetGenericMethodDefinition();
106
             var genericMethodInfo = methodInfo.GetGenericMethodDefinition();
105
                 var result = DynamicMethodConverters[genericMethodInfo](methodInfo, options.TypeConverter);
109
                 var result = DynamicMethodConverters[genericMethodInfo](methodInfo, options.TypeConverter);
106
                 if (result != null)
110
                 if (result != null)
107
                 {
111
                 {
108
-                    return result.ValueExpression;
112
+                    return LuResult<LambdaExpression>.Ok(result.ValueExpression);
109
                 }
113
                 }
110
             }
114
             }
111
 
115
 
112
-            return null;
116
+            return LuResult<LambdaExpression>.Error(LuStatus.InternalError.ToInt(),
117
+                $"Could not get method value expression from method info {methodInfo}");
113
         }
118
         }
114
     }
119
     }
115
 
120
 

Luticate2.Auth/Utils/Business/ObjectConverterDescriptor/LuObjectConverterDescriptorEnumerable.cs → Luticate2.Auth/Utils/Business/Converters/ObjectConverterDescriptor/LuObjectConverterDescriptorEnumerable.cs View File

3
 using System.Linq;
3
 using System.Linq;
4
 using System.Linq.Expressions;
4
 using System.Linq.Expressions;
5
 
5
 
6
-namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
6
+namespace Luticate2.Auth.Utils.Business.Converters.ObjectConverterDescriptor
7
 {
7
 {
8
     public class LuObjectConverterDescriptorEnumerable : LuObjectConverterDescriptor
8
     public class LuObjectConverterDescriptorEnumerable : LuObjectConverterDescriptor
9
     {
9
     {

Luticate2.Auth/Utils/Business/ObjectConverterDescriptor/LuObjectConverterDescriptorIdentity.cs → Luticate2.Auth/Utils/Business/Converters/ObjectConverterDescriptor/LuObjectConverterDescriptorIdentity.cs View File

2
 using System.Linq;
2
 using System.Linq;
3
 using System.Linq.Expressions;
3
 using System.Linq.Expressions;
4
 using System.Reflection;
4
 using System.Reflection;
5
+using Luticate2.Auth.Utils.Dbo.Result;
5
 using Luticate2.Auth.Utils.Interfaces;
6
 using Luticate2.Auth.Utils.Interfaces;
6
 
7
 
7
-namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
8
+namespace Luticate2.Auth.Utils.Business.Converters.ObjectConverterDescriptor
8
 {
9
 {
9
     public class LuObjectConverterDescriptorIdentity : ILuObjectConverterDescriptorIdentity
10
     public class LuObjectConverterDescriptorIdentity : ILuObjectConverterDescriptorIdentity
10
     {
11
     {
11
-        public LambdaExpression GetMemberValueExpression(MemberInfo memberInfo, ILuObjectConverterDescriptorOptions options)
12
+        public LuResult<LambdaExpression> GetMemberValueExpression(MemberInfo memberInfo, ILuObjectConverterDescriptorOptions options)
12
         {
13
         {
13
             var isStatic = false;
14
             var isStatic = false;
14
             if (memberInfo is FieldInfo fieldInfo)
15
             if (memberInfo is FieldInfo fieldInfo)
22
             var param = Expression.Parameter(memberInfo.DeclaringType);
23
             var param = Expression.Parameter(memberInfo.DeclaringType);
23
             var memberExpression = Expression.MakeMemberAccess(isStatic ? null : param, memberInfo);
24
             var memberExpression = Expression.MakeMemberAccess(isStatic ? null : param, memberInfo);
24
             var lambda = Expression.Lambda(memberExpression, param);
25
             var lambda = Expression.Lambda(memberExpression, param);
25
-            return lambda;
26
+            return LuResult<LambdaExpression>.Ok(lambda);
26
         }
27
         }
27
 
28
 
28
-        public LambdaExpression GetMethodValueExpression(MethodInfo methodInfo, ILuObjectConverterDescriptorOptions options)
29
+        public LuResult<LambdaExpression> GetMethodValueExpression(MethodInfo methodInfo, ILuObjectConverterDescriptorOptions options)
29
         {
30
         {
30
             var lambdaParams = new List<ParameterExpression>
31
             var lambdaParams = new List<ParameterExpression>
31
             {
32
             {
35
             lambdaParams.AddRange(methodParams);
36
             lambdaParams.AddRange(methodParams);
36
             var methodCallExpression = Expression.Call(methodInfo.IsStatic ? null : lambdaParams[0], methodInfo, methodParams);
37
             var methodCallExpression = Expression.Call(methodInfo.IsStatic ? null : lambdaParams[0], methodInfo, methodParams);
37
             var lambda = Expression.Lambda(methodCallExpression, lambdaParams);
38
             var lambda = Expression.Lambda(methodCallExpression, lambdaParams);
38
-            return lambda;
39
+            return LuResult<LambdaExpression>.Ok(lambda);
39
         }
40
         }
40
     }
41
     }
41
 }
42
 }

Luticate2.Auth/Utils/Business/ObjectConverterDescriptor/LuObjectConverterDescriptorDbSet.cs → Luticate2.Auth/Utils/Business/Converters/ObjectConverterDescriptor/LuObjectConverterDescriptorQueryable.cs View File

3
 using System.Linq.Expressions;
3
 using System.Linq.Expressions;
4
 using Microsoft.EntityFrameworkCore;
4
 using Microsoft.EntityFrameworkCore;
5
 
5
 
6
-namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
6
+namespace Luticate2.Auth.Utils.Business.Converters.ObjectConverterDescriptor
7
 {
7
 {
8
-    public class LuObjectConverterDescriptorDbSet : LuObjectConverterDescriptor
8
+    public class LuObjectConverterDescriptorQueryable : LuObjectConverterDescriptor
9
     {
9
     {
10
-        public LuObjectConverterDescriptorDbSet()
10
+        public LuObjectConverterDescriptorQueryable()
11
         {
11
         {
12
             AddDynamicMethodConverterTemplate(
12
             AddDynamicMethodConverterTemplate(
13
                 (Expression<Func<DbSet<object>, Expression<Func<object, bool>>, bool>>)((source, filter) => source.Any(filter)));
13
                 (Expression<Func<DbSet<object>, Expression<Func<object, bool>>, bool>>)((source, filter) => source.Any(filter)));

+ 5
- 2
Luticate2.Auth/Utils/Business/Pagination/LuFilterParser.cs View File

14
         {
14
         {
15
             if (string.IsNullOrWhiteSpace(data))
15
             if (string.IsNullOrWhiteSpace(data))
16
             {
16
             {
17
-                data = "true";
17
+                return LuResult<LuFilterDbo>.Ok(new LuFilterDbo
18
+                {
19
+                    Expression = null
20
+                });
18
             }
21
             }
19
             var completeData = $"(x) => ({data})";
22
             var completeData = $"(x) => ({data})";
20
             try
23
             try
21
             {
24
             {
22
-                var options = ScriptOptions.Default.AddReferences(typeof(LuFilterParser).Assembly);
25
+                var options = ScriptOptions.Default.AddReferences(typeof(LuFilterParser).Assembly, typeof(Expression<>).Assembly);
23
                 var expr = CSharpScript.EvaluateAsync<Expression<Func<T, bool>>>(completeData, options).Result;
26
                 var expr = CSharpScript.EvaluateAsync<Expression<Func<T, bool>>>(completeData, options).Result;
24
                 return LuResult<LuFilterDbo>.Ok(new LuFilterDbo
27
                 return LuResult<LuFilterDbo>.Ok(new LuFilterDbo
25
                 {
28
                 {

+ 22
- 22
Luticate2.Auth/Utils/Business/Pagination/LuOrderByParser.cs View File

1
 using System.Collections.Generic;
1
 using System.Collections.Generic;
2
 using Luticate2.Auth.Utils.Dbo;
2
 using Luticate2.Auth.Utils.Dbo;
3
-using Luticate2.Auth.Utils.Dbo.Fields;
4
 using Luticate2.Auth.Utils.Dbo.Pagination;
3
 using Luticate2.Auth.Utils.Dbo.Pagination;
5
 using Luticate2.Auth.Utils.Dbo.Result;
4
 using Luticate2.Auth.Utils.Dbo.Result;
6
 
5
 
27
                             $"LuOrderByParser: {data}", "Empty order by field");
26
                             $"LuOrderByParser: {data}", "Empty order by field");
28
                     }
27
                     }
29
                     var orderByField = new LuOrderByFieldDbo();
28
                     var orderByField = new LuOrderByFieldDbo();
30
-                    var split = field.Split(':');
31
-                    if (split.Length > 2 || split.Length == 0)
32
-                    {
33
-                        return LuResult<LuOrderByDbo>.Error(LuStatus.InputError.ToInt(),
34
-                            $"LuOrderByParser: {data}", "Invalid order by field syntax");
35
-                    }
36
-                    orderByField.Field = LuFieldDbo.Make(split[0]);
37
-                    var order = split.Length == 2 ? split[1].ToLower() : "asc";
38
-                    if (order == "asc")
39
-                    {
40
-                        orderByField.Asc = true;
41
-                    }
42
-                    else if (order == "desc")
43
-                    {
44
-                        orderByField.Asc = false;
45
-                    }
46
-                    else
47
-                    {
48
-                        return LuResult<LuOrderByDbo>.Error(LuStatus.InputError.ToInt(),
49
-                            $"LuOrderByParser: {data}", "Empty order by field order");
50
-                    }
29
+                    // TODO parse
30
+//                    var split = field.Split(':');
31
+//                    if (split.Length > 2 || split.Length == 0)
32
+//                    {
33
+//                        return LuResult<LuOrderByDbo>.Error(LuStatus.InputError.ToInt(),
34
+//                            $"LuOrderByParser: {data}", "Invalid order by field syntax");
35
+//                    }
36
+//                    orderByField.Field = LuFieldDbo.Make(split[0]);
37
+//                    var order = split.Length == 2 ? split[1].ToLower() : "asc";
38
+//                    if (order == "asc")
39
+//                    {
40
+//                        orderByField.Asc = true;
41
+//                    }
42
+//                    else if (order == "desc")
43
+//                    {
44
+//                        orderByField.Asc = false;
45
+//                    }
46
+//                    else
47
+//                    {
48
+//                        return LuResult<LuOrderByDbo>.Error(LuStatus.InputError.ToInt(),
49
+//                            $"LuOrderByParser: {data}", "Empty order by field order");
50
+//                    }
51
                     dbo.OrderByFields.Add(orderByField);
51
                     dbo.OrderByFields.Add(orderByField);
52
                 }
52
                 }
53
                 return LuResult<LuOrderByDbo>.Ok(dbo);
53
                 return LuResult<LuOrderByDbo>.Ok(dbo);

Luticate2.Auth/Utils/Business/Crud/LuEfCrudBusiness.cs → Luticate2.Auth/Utils/DataAccess/Crud/LuEfCrudDataAccess.cs View File

3
 using System.Linq;
3
 using System.Linq;
4
 using System.Linq.Expressions;
4
 using System.Linq.Expressions;
5
 using Luticate2.Auth.Utils.Business.Converters;
5
 using Luticate2.Auth.Utils.Business.Converters;
6
-using Luticate2.Auth.Utils.Business.ExpressionConverter;
6
+using Luticate2.Auth.Utils.Business.Converters.ExpressionConverter;
7
 using Luticate2.Auth.Utils.Dbo;
7
 using Luticate2.Auth.Utils.Dbo;
8
 using Luticate2.Auth.Utils.Dbo.Fields;
8
 using Luticate2.Auth.Utils.Dbo.Fields;
9
 using Luticate2.Auth.Utils.Dbo.Pagination;
9
 using Luticate2.Auth.Utils.Dbo.Pagination;
12
 using Microsoft.EntityFrameworkCore;
12
 using Microsoft.EntityFrameworkCore;
13
 using Microsoft.Extensions.DependencyInjection;
13
 using Microsoft.Extensions.DependencyInjection;
14
 
14
 
15
-namespace Luticate2.Auth.Utils.Business.Crud
15
+namespace Luticate2.Auth.Utils.DataAccess.Crud
16
 {
16
 {
17
-    public class LuEfCrudBusiness<TDbo, TModel, TDbContext> : ILuCrud<TDbo>
17
+    public class LuEfCrudDataAccess<TDbo, TModel, TDbContext> : ILuCrud<TDbo>
18
         where TModel : class
18
         where TModel : class
19
         where TDbContext : DbContext
19
         where TDbContext : DbContext
20
-        where TDbo : class, new()
20
+        where TDbo : class
21
     {
21
     {
22
         protected IServiceProvider ServiceProvider { get; }
22
         protected IServiceProvider ServiceProvider { get; }
23
         protected ILuConvertersAllocator ConvertersAllocator { get; }
23
         protected ILuConvertersAllocator ConvertersAllocator { get; }
24
+        protected ILuConvertersTypeConverter ConvertersTypeConverter { get; }
24
 
25
 
25
-        public LuEfCrudBusiness(IServiceProvider serviceProvider)
26
+        public LuEfCrudDataAccess(IServiceProvider serviceProvider)
26
         {
27
         {
27
             ServiceProvider = serviceProvider;
28
             ServiceProvider = serviceProvider;
28
             ConvertersAllocator = ServiceProvider.GetRequiredService<ILuConvertersAllocator>();
29
             ConvertersAllocator = ServiceProvider.GetRequiredService<ILuConvertersAllocator>();
30
+            ConvertersTypeConverter = ServiceProvider.GetRequiredService<ILuConvertersTypeConverter>();
29
         }
31
         }
30
 
32
 
31
-        protected virtual ILuObjectConverterOptions GetOptions()
33
+        protected virtual LuConvertersOptions GetOptions()
32
         {
34
         {
33
             return new LuConvertersOptions
35
             return new LuConvertersOptions
34
             {
36
             {
35
-                Allocator = ConvertersAllocator
37
+                Allocator = ConvertersAllocator,
38
+                Parameters = new Dictionary<ParameterExpression, Expression>(),
39
+                TypeConverter = ConvertersTypeConverter
36
             };// TODO
40
             };// TODO
37
         }
41
         }
38
 
42
 
46
             }
50
             }
47
 
51
 
48
             var options = GetOptions();
52
             var options = GetOptions();
49
-            var dstObj = options.Allocator.GetInstance(typeof(TModel));
53
+            var dstObjResult = options.Allocator.GetInstance(typeof(TModel));
54
+            if (!dstObjResult)
55
+            {
56
+                return dstObjResult.To<TModel>();
57
+            }
58
+            var dstObj = dstObjResult.Data;
50
             var result = copier.Convert(dbo, dstObj, new LuFieldDbo(), partialInput, GetOptions());
59
             var result = copier.Convert(dbo, dstObj, new LuFieldDbo(), partialInput, GetOptions());
51
             return result.Select(o => o as TModel);
60
             return result.Select(o => o as TModel);
52
         }
61
         }
61
             }
70
             }
62
 
71
 
63
             var options = GetOptions();
72
             var options = GetOptions();
64
-            var dstObj = options.Allocator.GetInstance(typeof(TDbo));
73
+            var dstObjResult = options.Allocator.GetInstance(typeof(TDbo));
74
+            if (!dstObjResult)
75
+            {
76
+                return dstObjResult.To<TDbo>();
77
+            }
78
+            var dstObj = dstObjResult.Data;
79
+
65
             var result = copier.Convert(model, dstObj, new LuFieldDbo(), partialResponse, GetOptions());
80
             var result = copier.Convert(model, dstObj, new LuFieldDbo(), partialResponse, GetOptions());
66
             return result.Select(o => o as TDbo);
81
             return result.Select(o => o as TDbo);
67
         }
82
         }
94
 
109
 
95
         protected virtual LuResult<IQueryable<TModel>> Filter(LuFilterDbo filter, IQueryable<TModel> queryable)
110
         protected virtual LuResult<IQueryable<TModel>> Filter(LuFilterDbo filter, IQueryable<TModel> queryable)
96
         {
111
         {
112
+            if (filter.Expression == null)
113
+            {
114
+                return LuResult<IQueryable<TModel>>.Ok(queryable);
115
+            }
116
+
97
             var lamdbaDbo = (Expression<Func<TDbo, bool>>) filter.Expression;
117
             var lamdbaDbo = (Expression<Func<TDbo, bool>>) filter.Expression;
98
 
118
 
99
-            var options = new LuConvertersOptions
100
-            {
101
-                Parameters = new Dictionary<ParameterExpression, Expression>(), // TODO
102
-                TypeConverter = null, // TODO
103
-                Allocator = ConvertersAllocator
104
-            };
119
+            var options = GetOptions();
105
             var converter = new LuExpressionConverterVisitor(options, ServiceProvider);
120
             var converter = new LuExpressionConverterVisitor(options, ServiceProvider);
106
             var lamdbaModel = converter.Visit(lamdbaDbo) as Expression<Func<TModel, bool>>;// TODO Handle errors
121
             var lamdbaModel = converter.Visit(lamdbaDbo) as Expression<Func<TModel, bool>>;// TODO Handle errors
107
             return LuResult<IQueryable<TModel>>.Ok(queryable.Where(lamdbaModel));
122
             return LuResult<IQueryable<TModel>>.Ok(queryable.Where(lamdbaModel));
108
         }
123
         }
109
 
124
 
110
-        protected virtual LuResult<IQueryable<TModel>> OrderByField(LuOrderByFieldDbo orderByfield, IQueryable<TModel> queryable)
125
+        protected virtual LuResult<IQueryable<TModel>> OrderByField(LuOrderByFieldDbo orderByField, IQueryable<TModel> queryable)
111
         {
126
         {
127
+            var lamdbaDbo = (Expression<Func<TDbo, object>>) orderByField.Expression;
128
+
129
+            var options = GetOptions();
130
+            var converter = new LuExpressionConverterVisitor(options, ServiceProvider);
131
+            var lamdbaModel = converter.Visit(lamdbaDbo) as Expression<Func<TModel, object>>;
132
+
133
+            IOrderedQueryable<TModel> ordered = null;
134
+
135
+            if (queryable is IOrderedQueryable<TModel> orderedQueryable)
136
+            {
137
+                if (orderByField.Asc)
138
+                {
139
+                    ordered = orderedQueryable.ThenBy(lamdbaModel);
140
+                }
141
+                else
142
+                {
143
+                    ordered = orderedQueryable.ThenByDescending(lamdbaModel);
144
+                }
145
+            }
146
+            else
147
+            {
148
+                if (orderByField.Asc)
149
+                {
150
+                    ordered = queryable.OrderBy(lamdbaModel);
151
+                }
152
+                else
153
+                {
154
+                    ordered = queryable.OrderByDescending(lamdbaModel);
155
+                }
156
+            }
157
+
158
+            return LuResult<IQueryable<TModel>>.Ok(ordered);
159
+
112
 //            var expressionBuilder = ServiceProvider.GetService<ILuDboModelExpressionConverter<TDbo, TModel>>();
160
 //            var expressionBuilder = ServiceProvider.GetService<ILuDboModelExpressionConverter<TDbo, TModel>>();
113
 //            var expressionResult = expressionBuilder.ConvertLamdba<TModel>(orderByfield.);
161
 //            var expressionResult = expressionBuilder.ConvertLamdba<TModel>(orderByfield.);
114
 //            if (!expressionResult)
162
 //            if (!expressionResult)
122
 //            var ordered = queryable.OrderBy(expressionNoConvert);
170
 //            var ordered = queryable.OrderBy(expressionNoConvert);
123
 //
171
 //
124
 //            return LuResult<IQueryable<TModel>>.Ok(ordered);
172
 //            return LuResult<IQueryable<TModel>>.Ok(ordered);
125
-            return LuResult<IQueryable<TModel>>.Ok(queryable);
173
+//            return LuResult<IQueryable<TModel>>.Ok(queryable);
126
         }
174
         }
127
 
175
 
128
         protected virtual LuResult<IQueryable<TModel>> OrderBy(LuOrderByDbo orderBy, IQueryable<TModel> queryable)
176
         protected virtual LuResult<IQueryable<TModel>> OrderBy(LuOrderByDbo orderBy, IQueryable<TModel> queryable)

+ 2
- 8
Luticate2.Auth/Utils/Dbo/Pagination/LuOrderByFieldDbo.cs View File

1
-using Luticate2.Auth.Utils.Dbo.Fields;
1
+using System.Linq.Expressions;
2
 
2
 
3
 namespace Luticate2.Auth.Utils.Dbo.Pagination
3
 namespace Luticate2.Auth.Utils.Dbo.Pagination
4
 {
4
 {
5
     public class LuOrderByFieldDbo
5
     public class LuOrderByFieldDbo
6
     {
6
     {
7
-        public LuFieldDbo Field { get; set; }
7
+        public Expression Expression { get; set; }
8
 
8
 
9
         public bool Asc { get; set; } = true;
9
         public bool Asc { get; set; } = true;
10
-
11
-        public override string ToString()
12
-        {
13
-            var asc = Asc ? "ASC" : "DESC";
14
-            return $"{string.Join(".", Field)}:{asc}";
15
-        }
16
     }
10
     }
17
 }
11
 }

+ 2
- 1
Luticate2.Auth/Utils/Interfaces/ILuConvertersAllocator.cs View File

1
 using System;
1
 using System;
2
+using Luticate2.Auth.Utils.Dbo.Result;
2
 
3
 
3
 namespace Luticate2.Auth.Utils.Interfaces
4
 namespace Luticate2.Auth.Utils.Interfaces
4
 {
5
 {
5
     public interface ILuConvertersAllocator
6
     public interface ILuConvertersAllocator
6
     {
7
     {
7
-        object GetInstance(Type type);
8
+        LuResult<object> GetInstance(Type type);
8
     }
9
     }
9
 }
10
 }

+ 2
- 1
Luticate2.Auth/Utils/Interfaces/ILuConvertersTypeConverter.cs View File

1
 using System;
1
 using System;
2
+using Luticate2.Auth.Utils.Dbo.Result;
2
 
3
 
3
 namespace Luticate2.Auth.Utils.Interfaces
4
 namespace Luticate2.Auth.Utils.Interfaces
4
 {
5
 {
5
     public interface ILuConvertersTypeConverter
6
     public interface ILuConvertersTypeConverter
6
     {
7
     {
7
-        Type ConvertType(Type typeFrom);
8
+        LuResult<Type> ConvertType(Type typeFrom);
8
     }
9
     }
9
 }
10
 }

+ 3
- 2
Luticate2.Auth/Utils/Interfaces/ILuObjectConverterDescriptor.cs View File

1
 using System.Linq.Expressions;
1
 using System.Linq.Expressions;
2
 using System.Reflection;
2
 using System.Reflection;
3
+using Luticate2.Auth.Utils.Dbo.Result;
3
 
4
 
4
 namespace Luticate2.Auth.Utils.Interfaces
5
 namespace Luticate2.Auth.Utils.Interfaces
5
 {
6
 {
10
 
11
 
11
     public interface ILuObjectConverterDescriptor
12
     public interface ILuObjectConverterDescriptor
12
     {
13
     {
13
-        LambdaExpression GetMemberValueExpression(MemberInfo memberInfo, ILuObjectConverterDescriptorOptions options);
14
+        LuResult<LambdaExpression> GetMemberValueExpression(MemberInfo memberInfo, ILuObjectConverterDescriptorOptions options);
14
 
15
 
15
-        LambdaExpression GetMethodValueExpression(MethodInfo methodInfo, ILuObjectConverterDescriptorOptions options);
16
+        LuResult<LambdaExpression> GetMethodValueExpression(MethodInfo methodInfo, ILuObjectConverterDescriptorOptions options);
16
     }
17
     }
17
 
18
 
18
     public interface ILuObjectConverterDescriptor<TTypeFrom, TTypeTo> : ILuObjectConverterDescriptor
19
     public interface ILuObjectConverterDescriptor<TTypeFrom, TTypeTo> : ILuObjectConverterDescriptor

Loading…
Cancel
Save