This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/neo4jAPI/tag.go
2023-05-24 16:05:27 +02:00

111 lines
2.5 KiB
Go

package neo4jAPI
import (
"context"
"e621_to_neo4j/neo4jAPI/models"
"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
}
func CreateTagNode2(ctx context.Context, driver neo4j.DriverWithContext, name string, tagType string) error {
session := driver.NewSession(ctx, neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite})
defer session.Close(ctx)
query := `
MERGE (u:e621Tag {e621Tag: $name, e621TagType: $tagType})
RETURN u
`
params := map[string]interface{}{
"name": name,
"tagType": tagType,
}
_, err := session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
result, err := tx.Run(ctx, query, params)
if err != nil {
return nil, err
}
return result.Consume(ctx)
})
return err
}
func GetAllTagNodes(ctx context.Context, driver neo4j.DriverWithContext, tagType string) ([]string, error) {
query := `
MATCH (u:e621Tag {e621TagType: $tagType})
RETURN u.e621Tag as eTag
`
params := map[string]interface{}{
"tagType": tagType,
}
result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
if err != nil {
return nil, err
}
var tags []string
for _, record := range result.Records {
tag, _, _ := neo4j.GetRecordValue[string](record, "eTag")
tags = append(tags, tag)
}
return tags, nil
}
func GetTagNode(ctx context.Context, driver neo4j.DriverWithContext, name string) (models.DBTag, bool, error) {
var tag models.DBTag
query := `
MATCH (u:e621Tag {e621Tag: $name})
RETURN u.e621Tag as e621Tag, u.e621TagType as e621TagType
`
params := map[string]interface{}{
"name": name,
}
result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
if err != nil {
return tag, false, err
}
if len(result.Records) > 0 {
record := result.Records[0]
e621Tag, _, _ := neo4j.GetRecordValue[string](record, "e621Tag")
e621TagType, _, _ := neo4j.GetRecordValue[string](record, "e621TagType")
tag = models.DBTag{
Tag: e621Tag,
TagType: e621TagType,
}
if e621Tag != name {
return tag, false, nil
}
return tag, true, nil
}
return tag, false, err
}