added first neo4j integration

This commit is contained in:
David Janowski 2023-05-22 16:00:01 +02:00
parent 7bf67372e9
commit 1dd14b9c82
3 changed files with 66 additions and 0 deletions

15
neo4jAPI/connection.go Normal file
View File

@ -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
}

24
neo4jAPI/tag.go Normal file
View File

@ -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
}

27
neo4jAPI/user.go Normal file
View File

@ -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
}