From 1dd14b9c827276014fb19e52325e19d1afd9f887 Mon Sep 17 00:00:00 2001 From: David Janowski Date: Mon, 22 May 2023 16:00:01 +0200 Subject: [PATCH] added first neo4j integration --- neo4jAPI/connection.go | 15 +++++++++++++++ neo4jAPI/tag.go | 24 ++++++++++++++++++++++++ neo4jAPI/user.go | 27 +++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 neo4jAPI/connection.go create mode 100644 neo4jAPI/tag.go create mode 100644 neo4jAPI/user.go diff --git a/neo4jAPI/connection.go b/neo4jAPI/connection.go new file mode 100644 index 0000000..872da04 --- /dev/null +++ b/neo4jAPI/connection.go @@ -0,0 +1,15 @@ +package neo4jAPI + +import ( + "fmt" + "github.com/neo4j/neo4j-go-driver/v5/neo4j" +) + +func NewConnection(uri string, username string, password string) (neo4j.DriverWithContext, error) { + driver, err := neo4j.NewDriverWithContext(uri, neo4j.BasicAuth(username, password, "")) + if err != nil { + return nil, fmt.Errorf("failed to create Neo4j driver: %v", err) + } + return driver, nil + +} diff --git a/neo4jAPI/tag.go b/neo4jAPI/tag.go new file mode 100644 index 0000000..f6b6e24 --- /dev/null +++ b/neo4jAPI/tag.go @@ -0,0 +1,24 @@ +package neo4jAPI + +import ( + "context" + "github.com/neo4j/neo4j-go-driver/v5/neo4j" +) + +func CreateTagNode(ctx context.Context, driver neo4j.DriverWithContext, name string, tagType string) error { + query := ` + MERGE (u:e621Tag {e621Tag: $name, e621TagType: $tagType}) + RETURN u +` + params := map[string]interface{}{ + "name": name, + "tagType": tagType, + } + + _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) + if err != nil { + return err + } + + return nil +} diff --git a/neo4jAPI/user.go b/neo4jAPI/user.go new file mode 100644 index 0000000..5a3073b --- /dev/null +++ b/neo4jAPI/user.go @@ -0,0 +1,27 @@ +package neo4jAPI + +import ( + "context" + "e621_to_neo4j/e621/api/models" + "github.com/neo4j/neo4j-go-driver/v5/neo4j" + "log" +) + +func CreateUserNode(ctx context.Context, driver neo4j.DriverWithContext, user models.E621User) error { + query := ` + MERGE (u:e621User {e621ID: $id, e621Username: $name}) + RETURN u + ` + params := map[string]interface{}{ + "id": user.ID, + "name": user.Name, + } + + _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) + if err != nil { + return err + } + + log.Println("User node created successfully!") + return nil +}