mirror of
https://github.com/vcscsvcscs/GenerationsHeritage.git
synced 2025-08-13 22:39:06 +02:00
create Relationship and person
This commit is contained in:
32
backend/handlers/create_relationship_and_person.go
Normal file
32
backend/handlers/create_relationship_and_person.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
||||
"github.com/vcscsvcscs/GenerationsHeritage/backend/memgraph"
|
||||
)
|
||||
|
||||
func CreateRelationshipAndPerson(driver neo4j.DriverWithContext) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var rp memgraph.RelationshipAndPerson
|
||||
if err := c.ShouldBindJSON(&rp); err != nil {
|
||||
log.Printf("ip: %s error: %s", c.ClientIP(), err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
rec, err := rp.CreateRelationshipAndPerson(driver)
|
||||
if err != nil {
|
||||
log.Printf("ip: %s error: %s", c.ClientIP(), err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{"relationship": rec.AsMap()})
|
||||
}
|
||||
}
|
@@ -19,8 +19,7 @@ func (r *Relationship) CreateRelationship(driver neo4j.DriverWithContext) (*neo4
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`MATCH (a:Person), (b:Person)
|
||||
WHERE a.ID = %s AND b.ID = %s`, r.FirstPersonID, r.SecondPersonID)
|
||||
query := fmt.Sprintf(`MATCH (a:Person), (b:Person) WHERE a.ID = %s AND b.ID = %s`, r.FirstPersonID, r.SecondPersonID)
|
||||
|
||||
if r.Direction == "->" {
|
||||
query = fmt.Sprintf(`%s CREATE (a)-[r:%s {verified: false}]->(b) RETURN r;`, query, r.Relationship)
|
||||
|
40
backend/memgraph/create_relationship_and_person.go
Normal file
40
backend/memgraph/create_relationship_and_person.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package memgraph
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func (rp *RelationshipAndPerson) CreateRelationshipAndPerson(driver neo4j.DriverWithContext) (*neo4j.Record, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
session := driver.NewSession(ctx, neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite})
|
||||
defer session.Close(ctx)
|
||||
|
||||
if err := rp.Verify(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`MATCH (a:Person) WHERE a.ID = %s`, rp.Relationship.FirstPersonID)
|
||||
|
||||
query = fmt.Sprintf("%s CREATE (b:Person {%s})", query, rp.Person.ToString())
|
||||
|
||||
if rp.Relationship.Direction == "->" {
|
||||
query = fmt.Sprintf(`%s CREATE (a)-[r:%s {verified: True}]->(b) RETURN r;`, query, rp.Relationship.Relationship)
|
||||
} else if rp.Relationship.Direction == "<-" {
|
||||
query = fmt.Sprintf(`%s CREATE (a)<-[r:%s {verified: True}]-(b) RETURN r;`, query, rp.Relationship.Relationship)
|
||||
} else {
|
||||
query = fmt.Sprintf(`%s CREATE (a)-[r:%s {verified: True}]-(b) RETURN r;`, query, rp.Relationship.Relationship)
|
||||
}
|
||||
|
||||
result, err := session.Run(ctx, query, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.Single(ctx)
|
||||
}
|
Reference in New Issue
Block a user