Skip to content
This repository has been archived by the owner on May 25, 2019. It is now read-only.

Commit

Permalink
#8 add includes
Browse files Browse the repository at this point in the history
  • Loading branch information
thangchung committed Jan 13, 2018
1 parent be2f23d commit bbfaa6b
Show file tree
Hide file tree
Showing 22 changed files with 252 additions and 84 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,20 @@ protected override void Up(MigrationBuilder migrationBuilder)
CategoryType = table.Column<int>(nullable: false),
Created = table.Column<DateTime>(nullable: false),
Name = table.Column<string>(nullable: false),
ParentId = table.Column<Guid>(nullable: true),
TaskId = table.Column<Guid>(nullable: true),
TaskStatus = table.Column<int>(nullable: false),
Updated = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_crm_Tasks", x => x.Id);
table.ForeignKey(
name: "FK_crm_Tasks_crm_Tasks_TaskId",
column: x => x.TaskId,
principalTable: "crm_Tasks",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});

migrationBuilder.CreateTable(
Expand Down Expand Up @@ -226,6 +234,11 @@ protected override void Up(MigrationBuilder migrationBuilder)
column: "NormalizedUserName",
unique: true,
filter: "[NormalizedUserName] IS NOT NULL");

migrationBuilder.CreateIndex(
name: "IX_crm_Tasks_TaskId",
table: "crm_Tasks",
column: "TaskId");
}

protected override void Down(MigrationBuilder migrationBuilder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasAnnotation("ProductVersion", "2.0.1-rtm-125")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

modelBuilder.Entity("CRMCore.Module.CustomCollection.Entity.Morphism", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();

b.Property<string>("ContentStr");

b.Property<DateTime>("Created");

b.Property<string>("SchemaStr");

b.Property<DateTime>("Updated");

b.HasKey("Id");

b.ToTable("crm_Morphisms");
});

modelBuilder.Entity("CRMCore.Module.Entities.Identity.ApplicationRole", b =>
{
b.Property<Guid>("Id")
Expand Down Expand Up @@ -100,24 +118,6 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("AspNetUsers");
});

modelBuilder.Entity("CRMCore.Module.CustomCollection.Entity.Morphism", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();

b.Property<string>("ContentStr");

b.Property<DateTime>("Created");

b.Property<string>("SchemaStr");

b.Property<DateTime>("Updated");

b.HasKey("Id");

b.ToTable("crm_Morphisms");
});

modelBuilder.Entity("CRMCore.Module.Task.Domain.Task", b =>
{
b.Property<Guid>("Id")
Expand All @@ -132,12 +132,18 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<string>("Name")
.IsRequired();

b.Property<Guid?>("ParentId");

b.Property<Guid?>("TaskId");

b.Property<int>("TaskStatus");

b.Property<DateTime>("Updated");

b.HasKey("Id");

b.HasIndex("TaskId");

b.ToTable("crm_Tasks");
});

Expand Down Expand Up @@ -222,6 +228,13 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("AspNetUserTokens");
});

modelBuilder.Entity("CRMCore.Module.Task.Domain.Task", b =>
{
b.HasOne("CRMCore.Module.Task.Domain.Task")
.WithMany("SubTasks")
.HasForeignKey("TaskId");
});

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
{
b.HasOne("CRMCore.Module.Entities.Identity.ApplicationRole")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.FileProviders;

namespace CRMCore.Module.MvcCore.Extensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;

namespace CRMCore.Module.MvcCore.Extensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public static IServiceCollection AddMvcModules(this IServiceCollection services)
internal static void AddMvcModuleCoreServices(this IServiceCollection services)
{
services.AddSingleton<IExtensionManager, ExtensionManager>();

services.Replace(ServiceDescriptor.Scoped<IModularTenantRouteBuilder, ModularTenantRouteBuilder>());
services.AddScoped<IViewLocationExpanderProvider, DefaultViewLocationExpanderProvider>();
services.AddScoped<IViewLocationExpanderProvider, ModularViewLocationExpanderProvider>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace CRMCore.Module.MvcCore
public interface IModularTenantRouteBuilder
{
IRouteBuilder Build();

void Configure(IRouteBuilder builder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext conte

public void PopulateValues(ViewLocationExpanderContext context)
{

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public RequestDelegate BuildTenantPipeline(IServiceProvider serviceProvider)
startup.Configure(appBuilder, routeBuilder, serviceProvider);
}


tenantRouteBuilder.Configure(routeBuilder);

var router = routeBuilder.Build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.ApplicationModels;

namespace CRMCore.Module.MvcCore.RazorPages
{
public class DefaultModularPageRouteModelConvention: IPageRouteModelConvention
public class DefaultModularPageRouteModelConvention : IPageRouteModelConvention
{
public void Apply(PageRouteModel model)
{
Expand All @@ -16,7 +15,8 @@ public void Apply(PageRouteModel model)
var pageIndex = template.LastIndexOf("/Pages/");
var moduleFolder = template.Substring(0, pageIndex);
var moduleId = moduleFolder.Substring(moduleFolder.LastIndexOf("/") + 1);
//convert module format CRMCore.module.ModuleName to ModuleName

//convert module format CRMCore.<module>.ModuleName to ModuleName
moduleId = moduleId.Substring(moduleId.LastIndexOf(".") + 1);

template = moduleId + template.Replace("/Pages/", "/").Substring(pageIndex);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

namespace CRMCore.Module.MvcCore.RazorPages
{
Expand All @@ -11,7 +10,7 @@ public static IMvcBuilder AddModularRazorPages(this IMvcBuilder builder)
{
options.RootDirectory = "/";
options.Conventions.Add(new DefaultModularPageRouteModelConvention());
});
});

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static IQueryable<TEntity> OrderByPropertyName<TEntity>(this IQueryable<T
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(TEntity), type)
.Invoke(null, new object[] { source, lambda });

return (IQueryable<TEntity>)result;
}
}
Expand Down
69 changes: 64 additions & 5 deletions src/modules/crm/CRMCore.Module.GraphQL/DbContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,77 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace CRMCore.Module.GraphQL
{
public static class DbContextExtensions
{
public static IQueryable Query(this DbContext context, string entityName) =>
context.Query(context.Model.FindEntityType(entityName).ClrType);
public static IQueryable Query(
this DbContext context,
string entityName) =>
context.Query(context.Model.FindEntityType(entityName).ClrType);

static readonly MethodInfo SetMethod = typeof(DbContext).GetMethod(nameof(DbContext.Set));
public static IQueryable Query(
this DbContext context,
Type entityType) =>
(IQueryable)SetMethod.MakeGenericMethod(entityType).Invoke(context, null);

public static IQueryable Query(
this DbContext context,
string entityName,
string[] includes)
{
return context.DbSetQuery(
context.Model.FindEntityType(entityName).ClrType,
includes);
}

public static IQueryable DbSetQuery(
this DbContext context,
Type entityType,
string[] includes)
{
return context.Set(entityType, includes);
}

public static IQueryable Set(this DbContext context,
Type T,
string[] includes)
{
// Get the generic type definition
MethodInfo method = typeof(DbContext).GetMethod(nameof(DbContext.Set), BindingFlags.Public | BindingFlags.Instance);

// Build a method with the specific type argument you're interested in
method = method.MakeGenericMethod(T);

var dbSet = method.Invoke(context, null);

public static IQueryable Query(this DbContext context, Type entityType) =>
(IQueryable)SetMethod.MakeGenericMethod(entityType).Invoke(context, null);
if (includes != null && includes.Count() > 0)
{
// https://stackoverflow.com/questions/38312437/can-a-string-based-include-alternative-be-created-in-entity-framework-core
MethodInfo includeMethodInfo = typeof(EntityFrameworkQueryableExtensions)
.GetTypeInfo()
.GetDeclaredMethods(
nameof(EntityFrameworkQueryableExtensions.Include))
.FirstOrDefault(mi => mi.GetParameters().Any(pi => pi.Name == "navigationPropertyPath"));

foreach (var include in includes)
{
var parameter = Expression.Parameter(T, "e");
var property = Expression.PropertyOrField(parameter, include);
includeMethodInfo = includeMethodInfo.MakeGenericMethod(T, property.Type);

dbSet = includeMethodInfo.Invoke(
null,
new object[] { dbSet, Expression.Lambda(property, new[] { parameter }) });
}
}

return dbSet as IQueryable;
}

static readonly MethodInfo SetMethod = typeof(DbContext).GetMethod(nameof(DbContext.Set));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<string> Get([FromQuery] string query = "{ crm_Tasks_list(offse
new ExecutionOptions()
{
Schema = graphQLSchema,
Query = query
Query = query
}
).ConfigureAwait(false);

Expand Down
Loading

0 comments on commit bbfaa6b

Please sign in to comment.