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

Commit

Permalink
#8 init
Browse files Browse the repository at this point in the history
  • Loading branch information
thangchung committed Jan 4, 2018
1 parent 2739f4a commit 22dec97
Show file tree
Hide file tree
Showing 15 changed files with 437 additions and 5 deletions.
11 changes: 9 additions & 2 deletions crmcore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,18 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CRMCore.Module.Data", "src\
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "base", "base", "{E71DEB4F-1656-46AA-979F-0211977DE983}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CRMCore.Module.Entities", "src\modules\base\CRMCore.Module.Entities\CRMCore.Module.Entities.csproj", "{A3971BCB-B342-4267-9783-D4C6A56A8391}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CRMCore.Module.Entities", "src\modules\base\CRMCore.Module.Entities\CRMCore.Module.Entities.csproj", "{A3971BCB-B342-4267-9783-D4C6A56A8391}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CRMCore.Module.MvcCore", "src\modules\base\CRMCore.Module.MvcCore\CRMCore.Module.MvcCore.csproj", "{AFA2A443-4642-4C86-A3EC-106122E22D5B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CRMCore.Module.MvcCore", "src\modules\base\CRMCore.Module.MvcCore\CRMCore.Module.MvcCore.csproj", "{AFA2A443-4642-4C86-A3EC-106122E22D5B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CRMCore.Module.Data.SqlServer", "src\modules\support\CRMCore.Module.Data.SqlServer\CRMCore.Module.Data.SqlServer.csproj", "{C1668722-949E-495A-B6A9-12D49B810041}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CRMCore.Module.ReDoc", "src\modules\support\CRMCore.Module.ReDoc\CRMCore.Module.ReDoc.csproj", "{A9068043-B2DD-4E36-AF0D-AA5A918CFBEC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CRMCore.Module.Swagger", "src\modules\support\CRMCore.Module.Swagger\CRMCore.Module.Swagger.csproj", "{72596C0C-FDB0-40FB-90AC-01752A080118}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CRMCore.Module.GraphQL", "src\modules\crm\CRMCore.Module.GraphQL\CRMCore.Module.GraphQL.csproj", "{7ECB9B94-009E-4331-A924-5663C2111788}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -180,6 +182,10 @@ Global
{72596C0C-FDB0-40FB-90AC-01752A080118}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72596C0C-FDB0-40FB-90AC-01752A080118}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72596C0C-FDB0-40FB-90AC-01752A080118}.Release|Any CPU.Build.0 = Release|Any CPU
{7ECB9B94-009E-4331-A924-5663C2111788}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7ECB9B94-009E-4331-A924-5663C2111788}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7ECB9B94-009E-4331-A924-5663C2111788}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7ECB9B94-009E-4331-A924-5663C2111788}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -212,6 +218,7 @@ Global
{C1668722-949E-495A-B6A9-12D49B810041} = {18C4BB63-4369-4DF3-87D2-B203F0917F1E}
{A9068043-B2DD-4E36-AF0D-AA5A918CFBEC} = {18C4BB63-4369-4DF3-87D2-B203F0917F1E}
{72596C0C-FDB0-40FB-90AC-01752A080118} = {18C4BB63-4369-4DF3-87D2-B203F0917F1E}
{7ECB9B94-009E-4331-A924-5663C2111788} = {7C9ACFC1-00F0-4547-9AF4-1A0E43AE1935}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3171C67F-EFB1-46FF-A566-6DC6C1E80FC5}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export default {
},
{
name: 'Swagger',
url: `${globalConfig.apiServer}/swagger`,
url: `${globalConfig.apiServer}/my-swagger`,
icon: 'icon-star'
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ApplicationIcon />
<OutputType>Library</OutputType>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ApplicationIcon />
<OutputType>Library</OutputType>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<OutputType>Library</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GraphQL" Version="0.17.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\targets\CRMCore.Module.Targets\CRMCore.Module.Targets.csproj" />
<ProjectReference Include="..\..\core\CRMCore.Module.Data\CRMCore.Module.Data.csproj" />
</ItemGroup>

</Project>
35 changes: 35 additions & 0 deletions src/modules/crm/CRMCore.Module.GraphQL/GraphQLController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using GraphQL;
using GraphQL.Http;
using GraphQL.Types;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace CRMCore.Module.GraphQL
{
[Area("CRMCore.Module.GraphQL")]
[Route("graphql/api/query")]
public class GraphQLController : Controller
{
private readonly Schema graphQLSchema;

public GraphQLController(Schema schema)
{
graphQLSchema = schema;
}

[HttpPost("")]
public async Task<string> Get([FromQuery] string query = "{ crm_Tasks_list { id } }")
{
var result = await new DocumentExecuter().ExecuteAsync(
new ExecutionOptions()
{
Schema = graphQLSchema,
Query = query
}
).ConfigureAwait(false);

var json = new DocumentWriter(indent: true).Write(result.Data);
return json;
}
}
}
102 changes: 102 additions & 0 deletions src/modules/crm/CRMCore.Module.GraphQL/GraphQLQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using CRMCore.Module.Data;
using CRMCore.Module.GraphQL.Models;
using GraphQL.Resolvers;
using GraphQL.Types;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;

namespace CRMCore.Module.GraphQL
{
public class GraphQLQuery : ObjectGraphType<object>
{
private IDatabaseMetadata _dbMetadata;
private ApplicationDbContext _dbContext;

public GraphQLQuery(ApplicationDbContext dbContext, IDatabaseMetadata dbMetadata)
{
_dbMetadata = dbMetadata;
_dbContext = dbContext;

Name = "Query";

foreach (var metaTable in _dbMetadata.GetMetadataTables())
{
var tableType = new TableType(metaTable);
/*AddField(new FieldType()
{
Name = metaTable.TableName,
Type = tableType.GetType(),
ResolvedType = tableType,
Resolver = new MyFieldResolver(metaTable, _dbContext),
Arguments = new QueryArguments(
tableType.TableArgs
)
});*/

//lets add key to get list of current table
var listType = new ListGraphType(tableType);
AddField(new FieldType
{
Name = $"{metaTable.TableName}_list",
Type = listType.GetType(),
ResolvedType = listType,
Resolver = new MyFieldResolver(metaTable, _dbContext),
/*Arguments = new QueryArguments(
tableType.TableArgs
) */
});
}
}
}

public class MyFieldResolver : IFieldResolver
{
private TableMetadata _tableMetadata;
private ApplicationDbContext _dbContext;

public MyFieldResolver(TableMetadata tableMetadata, ApplicationDbContext dbContext)
{
_tableMetadata = tableMetadata;
_dbContext = dbContext;
}

public object Resolve(ResolveFieldContext context)
{
var source = context.Source;

List<Dictionary<string, object>> finalResult = new List<Dictionary<string, object>>();
if (context.FieldName.Contains("_list"))
{

using (var command = _dbContext.Database.GetDbConnection().CreateCommand())
{
command.CommandText = "SELECT * FROM " + _tableMetadata.TableName;
_dbContext.Database.OpenConnection();
using (var result = command.ExecuteReader())
{
while (result.Read())
{
var temp = new Dictionary<string, object>();

for (var index = 0; index < result.FieldCount; index++)
{
var lowerCase = Char.ToLowerInvariant(result.GetName(index)[0]) + result.GetName(index).Substring(1);
temp.Add(lowerCase, result[result.GetName(index)]);
}

finalResult.Add(temp);
}
}

if (_dbContext.Database.GetDbConnection().State == System.Data.ConnectionState.Open)
{
_dbContext.Database.CloseConnection();
}
}
}

return finalResult;
}
}
}
25 changes: 25 additions & 0 deletions src/modules/crm/CRMCore.Module.GraphQL/Models/ColumnMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace CRMCore.Module.GraphQL.Models
{
public class ColumnMetadata
{
[Column("name")]
public string ColumnName
{
get; set;
}

[Column("type")]
public string DataType
{
get; set;
}

[Column("notnull")]
public string IsNullable
{
get; set;
}
}
}
97 changes: 97 additions & 0 deletions src/modules/crm/CRMCore.Module.GraphQL/Models/DatabaseMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using CRMCore.Module.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using System.Collections.Generic;
using System.Linq;

namespace CRMCore.Module.GraphQL.Models
{
public class DatabaseMetadata : IDatabaseMetadata
{
protected ApplicationDbContext _dbContext;

public DatabaseMetadata(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
DatabaseName = _dbContext.Database.GetDbConnection().Database;
if (Tables == null)
LoadMetaData();
}

public string DatabaseName { get; set; }

public List<TableMetadata> Tables { get; set; }

public void ReloadMetadata()
{
LoadMetaData();
}

public IEnumerable<TableMetadata> GetMetadataTables()
{
if (Tables == null)
return new List<TableMetadata>();

return Tables;
}

private void LoadMetaData()
{
// var res = new List<TableMetadata>();
/*res.Add(
FetchTableMetaData("Customers")
);*/

Tables = FetchTableMetaData();
}

/*public List<TableMetadata> GetMetadataTables()
{
if (Tables == null)
return new List<TableMetadata>();
return Tables;
} */

private List<TableMetadata> FetchTableMetaData()
{
var metaTables = new List<TableMetadata>();
foreach (var entityType in _dbContext.Model.GetEntityTypes())
{
var metaTable = new TableMetadata();
var relational = entityType.Relational();
var tableName = relational.TableName;

metaTable.TableName = tableName;
metaTable.Columns = GetColumnsMetadata(entityType).ToList();

metaTables.Add(metaTable);
}

return metaTables;
}

private IEnumerable<ColumnMetadata> GetColumnsMetadata(IEntityType entityType)
{
var tableColumns = new List<ColumnMetadata>();

foreach (var propertyType in entityType.GetProperties())
{
var relational = propertyType.Relational();
tableColumns.Add(new ColumnMetadata
{
ColumnName = relational.ColumnName,
DataType = relational.ColumnType
});
}

return tableColumns;
}
}

public interface IDatabaseMetadata
{
void ReloadMetadata();
IEnumerable<TableMetadata> GetMetadataTables();
}
}
14 changes: 14 additions & 0 deletions src/modules/crm/CRMCore.Module.GraphQL/Models/TableMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace CRMCore.Module.GraphQL.Models
{
public class TableMetadata
{
[Column("table_name")]
public string TableName { get; set; }

public List<ColumnMetadata> Columns { get; set; }
}
}
1 change: 1 addition & 0 deletions src/modules/crm/CRMCore.Module.GraphQL/Module.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CRMCore.Module.Data;
using CRMCore.Module.GraphQL.Models;
using GraphQL.Types;
using Microsoft.Extensions.DependencyInjection;

namespace CRMCore.Module.GraphQL
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddMyGraphQL(this IServiceCollection services)
{
services.AddScoped<IDatabaseMetadata, DatabaseMetadata>();
services.AddScoped((resolver) =>
{
var dbContext = resolver.GetRequiredService<ApplicationDbContext>();
var metaDatabase = resolver.GetRequiredService<IDatabaseMetadata>();
var schema = new Schema { Query = new GraphQLQuery(dbContext, metaDatabase) };
schema.Initialize();
return schema;
});

return services;
}
}
}
Loading

0 comments on commit 22dec97

Please sign in to comment.