diff --git a/src/BitcoinShow.Neo4j.Core/BitcoinShow.Neo4j.Core.csproj b/src/BitcoinShow.Neo4j.Core/BitcoinShow.Neo4j.Core.csproj index 95f43e1..b09f0b2 100644 --- a/src/BitcoinShow.Neo4j.Core/BitcoinShow.Neo4j.Core.csproj +++ b/src/BitcoinShow.Neo4j.Core/BitcoinShow.Neo4j.Core.csproj @@ -1,4 +1,4 @@ - + netcoreapp2.0 diff --git a/src/BitcoinShow.Neo4j.Core/Class1.cs b/src/BitcoinShow.Neo4j.Core/Class1.cs deleted file mode 100644 index 6c909b7..0000000 --- a/src/BitcoinShow.Neo4j.Core/Class1.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace BitcoinShow.Neo4j.Core -{ - public class Class1 - { - } -} diff --git a/src/BitcoinShow.Neo4j.Core/Repository/Interface/INeo4jRepository.cs b/src/BitcoinShow.Neo4j.Core/Repository/Interface/INeo4jRepository.cs new file mode 100644 index 0000000..8a99d79 --- /dev/null +++ b/src/BitcoinShow.Neo4j.Core/Repository/Interface/INeo4jRepository.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Neo4j.Driver.V1; + +namespace BitcoinShow.Neo4j.Core.Repository.Interface +{ + /// + /// Neo4j query methods definition + /// + internal interface INeo4jRepository : IDisposable + { + /// + /// Create a new node + /// + /// Cypher query + /// Return the node created. + /// The uuid is not returned when a node is created. + Task CreateCypherAsync(string query); + + /// + /// Match nodes with only one key + /// + /// Cypher query + /// List of + Task> MatchSingleKeyCypherAsync(string query); + + /// + /// Match a label, (node type) by its uuid + /// + /// Label name + /// UUID + /// + Task MatchLabelByUUIDCypherAsync(string label, string uuid); + + /// + /// Remove relations and delete node by its uuid + /// + /// Label name + /// UUID + Task DeleteLabelByUUIDCypherAsync(string label, string uuid); + } +} diff --git a/src/BitcoinShow.Neo4j.Core/Repository/Neo4jRepository.cs b/src/BitcoinShow.Neo4j.Core/Repository/Neo4jRepository.cs new file mode 100644 index 0000000..1af9e7c --- /dev/null +++ b/src/BitcoinShow.Neo4j.Core/Repository/Neo4jRepository.cs @@ -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 +{ + /// + /// Neo4j Repository + /// + public class Neo4jRepository : INeo4jRepository + { + private readonly IDriver _driver; + + /// + /// Constructor + /// + /// Neo4j bolt uri like bolt://127.0.0.1:7687 + public Neo4jRepository(string uri) + { + _driver = GraphDatabase.Driver(uri, AuthTokens.None); + } + + /// + /// Constructor + /// + /// Neo4j bolt uri like bolt://127.0.0.1:7687 + /// Username + /// Password + public Neo4jRepository(string uri, string username, string password) + { + _driver = GraphDatabase.Driver(uri, AuthTokens.Basic(username, password)); + } + + /// + /// Create a new node + /// + /// Cypher query. + /// Return the node created. + /// + /// This sample shows how write a cypher query to call the method. + /// + /// CREATE (n:Person {name: 'Jhon'}) RETURN n + /// + /// + /// The uuid is not returned when a node is created. + public async Task 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(); + } + return node; + + } + } + + /// + /// Match a label, (node type) by its uuid + /// + /// Label name + /// UUID + /// + /// /// + /// This sample shows how write a cypher query to call the method. + /// + /// MATCH (p:Person {uuid: '75e88b00-1fc6-11e8-b7fc-2cd05a628834'}) RETURN p + /// + /// + public async Task 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 nodes = new List(); + 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(); + } + return node; + } + } + + /// + /// Match nodes with only one key + /// + /// Cypher query + /// List of + /// + /// This sample shows how write a cypher query to call the method. + /// + /// MATCH (key:Person) RETURN key + /// + /// + public async Task> MatchSingleKeyCypherAsync(string query) + { + using (ISession session = _driver.Session(AccessMode.Read)) + { + List nodes = new List(); + IStatementResultCursor result = await session.RunAsync(query); + await result.ForEachAsync(r => + { + nodes.Add(r.Keys[0].As()); + }); + return nodes; + } + } + + /// + /// Remove relations and delete node by its uuid + /// + /// Label name + /// UUID + /// /// + /// This sample shows how write a cypher query to call the method. + /// + /// MATCH (p:Person {uuid: '75e88b00-1fc6-11e8-b7fc-2cd05a628834'}) DETACH DELETE p + /// + /// + public async Task DeleteLabelByUUIDCypherAsync(string label, string uuid) + { + using (ISession session = _driver.Session(AccessMode.Write)) + { + List nodes = new List(); + await session.RunAsync($"MATCH (label:{label} {{uuid: '{uuid}' }}) DETACH DELETE label"); + } + } + + /// + /// Dispose method + /// + public void Dispose() + { + _driver.Dispose(); + } + } +}