Skip to content

Commit

Permalink
Cleaned solution and upgraded to last .netcore with Visual Studio 2017
Browse files Browse the repository at this point in the history
  • Loading branch information
robson-paproski committed May 24, 2017
1 parent aa06ca4 commit 8d81030
Show file tree
Hide file tree
Showing 265 changed files with 772 additions and 28,973 deletions.
33 changes: 33 additions & 0 deletions AspNetCore.Identity.PostgreSQL.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.4
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCore.Identity.PostgreSQL", "AspNetCore.Identity.PostgreSQL\AspNetCore.Identity.PostgreSQL.csproj", "{9CE6763F-FE14-4FFB-AEF5-F69AA712F55E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebExample", "WebExample\WebExample.csproj", "{1F910597-E02B-4F95-9F1B-67E27368891C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F204EC76-7505-47B7-810A-3E87AE87CDFA}"
ProjectSection(SolutionItems) = preProject
PostgresIdentity.sql = PostgresIdentity.sql
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9CE6763F-FE14-4FFB-AEF5-F69AA712F55E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9CE6763F-FE14-4FFB-AEF5-F69AA712F55E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9CE6763F-FE14-4FFB-AEF5-F69AA712F55E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9CE6763F-FE14-4FFB-AEF5-F69AA712F55E}.Release|Any CPU.Build.0 = Release|Any CPU
{1F910597-E02B-4F95-9F1B-67E27368891C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F910597-E02B-4F95-9F1B-67E27368891C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F910597-E02B-4F95-9F1B-67E27368891C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F910597-E02B-4F95-9F1B-67E27368891C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="1.1.2" />
<PackageReference Include="Npgsql" Version="3.2.2" />
</ItemGroup>

<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\..\..\.nuget\packages\newtonsoft.json\9.0.1\lib\netstandard1.0\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
249 changes: 249 additions & 0 deletions AspNetCore.Identity.PostgreSQL/Context/PostgreSQLDatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using Npgsql;
using NpgsqlTypes;

namespace AspNetCore.Identity.PostgreSQL.Context
{
public class PostgreSQLDatabase
{
private static NpgsqlConnection _connection;
private readonly IConfigurationRoot _configurationRoot;
private static object _consulta = new object();

public PostgreSQLDatabase(IConfigurationRoot configurationRoot)
{
_configurationRoot = configurationRoot;
_connection = new Npgsql.NpgsqlConnection(_configurationRoot.GetConnectionString("PostgreSQLBaseConnection"));
}


public int ExecuteSQL(string commandText, Dictionary<string, object> parameters)
{
lock (_consulta)
{
var result = 0;

if (string.IsNullOrEmpty(commandText))
{
throw new ArgumentException("Command text cannot be null or empty.");
}

try
{
OpenConnection();
var command = CreateCommand(commandText, parameters);
result = command.ExecuteNonQuery();
}
finally
{

}

return result;
}
}


public object ExecuteQueryGetSingleObject(string commandText, Dictionary<string, object> parameters)
{
lock (_consulta)
{
object result = null;

if (string.IsNullOrEmpty(commandText))
{
throw new ArgumentException("Command text cannot be null or empty.");
}

try
{
OpenConnection();
var command = CreateCommand(commandText, parameters);
result = command.ExecuteScalar();
}
finally
{
CloseConnection();
}

return result;
}
}

public Dictionary<string, string> ExecuteQueryGetSingleRow(string commandText,
Dictionary<string, object> parameters)
{
lock (_consulta)
{
Dictionary<string, string> row = null;
if (string.IsNullOrEmpty(commandText))
{
throw new ArgumentException("Command text cannot be null or empty.");
}

try
{
OpenConnection();
var command = CreateCommand(commandText, parameters);
using (NpgsqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
row = new Dictionary<string, string>();
for (var i = 0; i < reader.FieldCount; i++)
{
var columnName = reader.GetName(i);
var columnValue = reader.IsDBNull(i) ? null : reader.GetValue(i).ToString();
row.Add(columnName, columnValue);
}
break;
}
}
}
finally
{
CloseConnection();
}

return row;
}
}

public List<Dictionary<string, string>> ExecuteQuery(string commandText, Dictionary<string, object> parameters)
{
lock (_consulta)
{
List<Dictionary<string, string>> rows = null;
if (string.IsNullOrEmpty(commandText))
{
throw new ArgumentException("Command text cannot be null or empty.");
}

try
{
OpenConnection();
var command = CreateCommand(commandText, parameters);
using (NpgsqlDataReader reader = command.ExecuteReader())
{
rows = new List<Dictionary<string, string>>();
while (reader.Read())
{
var row = new Dictionary<string, string>();
for (var i = 0; i < reader.FieldCount; i++)
{
var columnName = reader.GetName(i);
var columnValue = reader.IsDBNull(i) ? null : reader.GetValue(i).ToString();
row.Add(columnName, columnValue);
}
rows.Add(row);
}
}
}
finally
{
CloseConnection();
}

return rows;
}
}

private NpgsqlCommand CreateCommand(string commandText, Dictionary<string, object> parameters)
{

NpgsqlCommand command = _connection.CreateCommand();
command.CommandText = commandText;
AddParameters(command, parameters);

return command;

}

/// <summary>
/// Adds the parameters to a PostgreSQL command.
/// </summary>
/// <param name="commandText">The PostgreSQL query to execute.</param>
/// <param name="parameters">Parameters to pass to the PostgreSQL query.</param>
private static void AddParameters(NpgsqlCommand command, Dictionary<string, object> parameters)
{
if (parameters == null)
{
return;
}

foreach (var param in parameters)
{
var parameter = command.CreateParameter();
parameter.ParameterName = param.Key;
parameter.Value = param.Value ?? DBNull.Value;

var s = param.Value as string;
if ((s != null) && s.StartsWith("JSON"))
{
parameter.Value = JObject.Parse(s.Replace("JSON", ""));
parameter.NpgsqlDbType = NpgsqlDbType.Json;
}

command.Parameters.Add(parameter);
}
}

/// <summary>
/// Helper method to return query a string value.
/// </summary>
/// <param name="commandText">The PostgreSQL query to execute.</param>
/// <param name="parameters">Parameters to pass to the PostgreSQL query.</param>
/// <returns>The string value resulting from the query.</returns>
private string GetStrValue(string commandText, Dictionary<string, object> parameters)
{
var value = ExecuteQueryGetSingleObject(commandText, parameters) as string;
return value;
}

/// <summary>
/// Opens a connection if not open.
/// </summary>
public void OpenConnection()
{
if (_connection == null) _connection = new Npgsql.NpgsqlConnection(_configurationRoot.GetConnectionString("PostgreSQLBaseConnection"));
var retries = 20;
if (_connection.State == ConnectionState.Open)
{
}
else
{
while (retries >= 0 && _connection.State != ConnectionState.Open)
{
lock (_consulta)
{
_connection.Close();
_connection.Open();
}
retries--;
Thread.Sleep(50);
}
}
}

/// <summary>
/// Closes the connection if it is open.
/// </summary>
public void CloseConnection()
{
/* if (_connection.State == ConnectionState.Open)
{
// _connection.Close();
}*/
}

public void Dispose()
{

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace AspNetCore.Identity.PostgreSQL
/// </summary>
public class IdentityRole : IdentityRole<Guid>
{
private string roleId;
private Guid roleId;

/// <summary>
/// Initializes a new instance of <see cref="IdentityRole"/>.
Expand All @@ -19,7 +19,7 @@ public class IdentityRole : IdentityRole<Guid>
/// </remarks>
public IdentityRole()
{
Id = Guid.NewGuid();
this.Id = roleId;
}

/// <summary>
Expand All @@ -36,7 +36,7 @@ public IdentityRole(string roleName) : this()

public IdentityRole(string roleName, string roleId) : this(roleName)
{
this.roleId = roleId;
this.roleId = new Guid(roleId);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Linq;
using AspNetCore.Identity.PostgreSQL.Tables;
using Microsoft.AspNetCore.Identity;
using System.Threading;
using System.Threading.Tasks;

using AspNetCore.Identity.PostgreSQL.Context;
using AspNetCore.Identity.PostgreSQL.Tables;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;

namespace AspNetCore.Identity.PostgreSQL.Stores
{
Expand All @@ -15,7 +16,7 @@ public class RoleStore<TRole> : IQueryableRoleStore<TRole>
where TRole : IdentityRole
{
private RoleTable roleTable;
public PostgreSQLDatabase Database { get; private set; }
public PostgreSQLDatabase Database { get; }

public IQueryable<TRole> Roles
{
Expand All @@ -29,20 +30,16 @@ public IQueryable<TRole> Roles
/// <summary>
/// Default constructor that initializes a new PostgreSQLDatabase instance using the Default Connection string.
/// </summary>
public RoleStore()
{
this.Database = new PostgreSQLDatabase();
this.roleTable = new RoleTable(Database);
}


/// <summary>
/// Constructor that takes a PostgreSQLDatabase as argument.
/// Constructor that takes a IConfigurationRoot as argument.
/// </summary>
/// <param name="database"></param>
public RoleStore(PostgreSQLDatabase database)
/// <param name="config"></param>
public RoleStore(IConfigurationRoot config)
{
this.Database = database;
this.roleTable = new RoleTable(database);
this.Database = new PostgreSQLDatabase(config);
this.roleTable = new RoleTable(Database);
}

public void Dispose()
Expand Down
Loading

0 comments on commit 8d81030

Please sign in to comment.