Skip to content

Commit

Permalink
#40 Create Neo4jRepository class 🚧
Browse files Browse the repository at this point in the history
Add methods
Task<INode> CreateCypherAsync(string query);
Task<List<INode>> MatchSingleKeyCypherAsync(string query);
Task<INode> MatchLabelByUUIDCypherAsync(string label, string uuid);
Task DeleteLabelByUUIDCypherAsync(string label, string uuid);
  • Loading branch information
italopessoa committed Mar 4, 2018
1 parent e6509ec commit b3c94ce
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/BitcoinShow.Neo4j.Core/BitcoinShow.Neo4j.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
Expand Down
8 changes: 0 additions & 8 deletions src/BitcoinShow.Neo4j.Core/Class1.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Neo4j.Driver.V1;

namespace BitcoinShow.Neo4j.Core.Repository.Interface
{
/// <summary>
/// Neo4j query methods definition
/// </summary>
internal interface INeo4jRepository : IDisposable
{
/// <summary>
/// Create a new node
/// </summary>
/// <param name="query">Cypher query</param>
/// <returns>Return the node created.</returns>
/// <remarks>The uuid is not returned when a node is created.</remarks>
Task<INode> CreateCypherAsync(string query);

/// <summary>
/// Match nodes with only one key
/// </summary>
/// <param name="query">Cypher query</param>
/// <returns>List of <see cref="INode"/></returns>
Task<List<INode>> MatchSingleKeyCypherAsync(string query);

/// <summary>
/// Match a label, (node type) by its uuid
/// </summary>
/// <param name="label">Label name</param>
/// <param name="uuid">UUID</param>
/// <returns><see cref="INode"/></returns>
Task<INode> MatchLabelByUUIDCypherAsync(string label, string uuid);

/// <summary>
/// Remove relations and delete node by its uuid
/// </summary>
/// <param name="label">Label name</param>
/// <param name="uuid">UUID</param>
Task DeleteLabelByUUIDCypherAsync(string label, string uuid);
}
}
149 changes: 149 additions & 0 deletions src/BitcoinShow.Neo4j.Core/Repository/Neo4jRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BitcoinShow.Neo4j.Core.Repository.Interface;
using Neo4j.Driver.V1;

namespace BitcoinShow.Neo4j.Core
{
/// <summary>
/// Neo4j Repository
/// </summary>
public class Neo4jRepository : INeo4jRepository
{
private readonly IDriver _driver;

/// <summary>
/// Constructor
/// </summary>
/// <param name="uri">Neo4j bolt uri like bolt://127.0.0.1:7687</param>
public Neo4jRepository(string uri)
{
_driver = GraphDatabase.Driver(uri, AuthTokens.None);
}

/// <summary>
/// Constructor
/// </summary>
/// <param name="uri">Neo4j bolt uri like bolt://127.0.0.1:7687</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
public Neo4jRepository(string uri, string username, string password)
{
_driver = GraphDatabase.Driver(uri, AuthTokens.Basic(username, password));
}

/// <summary>
/// Create a new node
/// </summary>
/// <param name="query">Cypher query.</param>
/// <returns>Return the node created.</returns>
/// <example>
/// This sample shows how write a cypher query to call the <see cref="CreateCypherAsync(string)"/> method.
/// <code>
/// CREATE (n:Person {name: 'Jhon'}) RETURN n
/// </code>
/// </example>
/// <remarks>The uuid is not returned when a node is created.</remarks>
public async Task<INode> CreateCypherAsync(string query)
{
using (ISession session = _driver.Session(AccessMode.Write))
{
IStatementResultCursor result = await session.RunAsync(query);
INode node = null;
if (await result.FetchAsync())
{
node = result.Current[result.Keys[0]].As<INode>();
}
return node;

}
}

/// <summary>
/// Match a label, (node type) by its uuid
/// </summary>
/// <param name="label">Label name</param>
/// <param name="uuid">UUID</param>
/// <returns><see cref="INode"/></returns>
/// /// <example>
/// This sample shows how write a cypher query to call the <see cref="MatchLabelByUUIDCypherAsync(string, string)"/> method.
/// <code>
/// MATCH (p:Person {uuid: '75e88b00-1fc6-11e8-b7fc-2cd05a628834'}) RETURN p
/// </code>
/// </example>
public async Task<INode> MatchLabelByUUIDCypherAsync(string label, string uuid)
{
if (string.IsNullOrEmpty(label) || string.IsNullOrWhiteSpace(label))
throw new ArgumentNullException(nameof(label));
if (string.IsNullOrEmpty(uuid) || string.IsNullOrWhiteSpace(uuid))
throw new ArgumentNullException(nameof(uuid));

using (ISession session = _driver.Session(AccessMode.Read))
{
List<INode> nodes = new List<INode>();
IStatementResultCursor result = await session.RunAsync($"MATCH (label:{label} {{uuid: '{uuid}' }}) RETURN label");

INode node = null;
if (await result.FetchAsync())
{
node = result.Current[result.Current.Keys[0]].As<INode>();
}
return node;
}
}

/// <summary>
/// Match nodes with only one key
/// </summary>
/// <param name="query">Cypher query</param>
/// <returns>List of <see cref="INode"/></returns>
/// <example>
/// This sample shows how write a cypher query to call the <see cref="MatchSingleKeyCypherAsync(string)"/> method.
/// <code>
/// MATCH (key:Person) RETURN key
/// </code>
/// </example>
public async Task<List<INode>> MatchSingleKeyCypherAsync(string query)
{
using (ISession session = _driver.Session(AccessMode.Read))
{
List<INode> nodes = new List<INode>();
IStatementResultCursor result = await session.RunAsync(query);
await result.ForEachAsync(r =>
{
nodes.Add(r.Keys[0].As<INode>());
});
return nodes;
}
}

/// <summary>
/// Remove relations and delete node by its uuid
/// </summary>
/// <param name="label">Label name</param>
/// <param name="uuid">UUID</param>
/// /// <example>
/// This sample shows how write a cypher query to call the <see cref="DeleteLabelByUUIDCypherAsync(string, string)"/> method.
/// <code>
/// MATCH (p:Person {uuid: '75e88b00-1fc6-11e8-b7fc-2cd05a628834'}) DETACH DELETE p
/// </code>
/// </example>
public async Task DeleteLabelByUUIDCypherAsync(string label, string uuid)
{
using (ISession session = _driver.Session(AccessMode.Write))
{
List<INode> nodes = new List<INode>();
await session.RunAsync($"MATCH (label:{label} {{uuid: '{uuid}' }}) DETACH DELETE label");
}
}

/// <summary>
/// Dispose method
/// </summary>
public void Dispose()
{
_driver.Dispose();
}
}
}

0 comments on commit b3c94ce

Please sign in to comment.