mirror of
https://github.com/vcscsvcscs/GenerationsHeritage.git
synced 2025-08-12 22:09:07 +02:00
verify relationship
This commit is contained in:
@@ -1,12 +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 VerifyRelationship(driver neo4j.DriverWithContext) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var relationship memgraph.Relationship
|
||||
if err := c.ShouldBindJSON(&relationship); err != nil {
|
||||
log.Printf("ip: %s error: %s", c.ClientIP(), err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
rec, err := relationship.VerifyRelationship(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.StatusOK, gin.H{"relationship": rec.AsMap()})
|
||||
}
|
||||
}
|
||||
|
39
backend/memgraph/verify_relationship.go
Normal file
39
backend/memgraph/verify_relationship.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package memgraph
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func (r *Relationship) VerifyRelationship(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 := r.Verify(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query := ""
|
||||
if r.Direction == "->" {
|
||||
query = fmt.Sprintf(`MATCH (a)-[r:%s]->(b)`, r.Relationship)
|
||||
} else if r.Direction == "<-" {
|
||||
query = fmt.Sprintf(`MATCH (a)<-[r:%s]-(b)`, r.Relationship)
|
||||
} else {
|
||||
query = fmt.Sprintf(`MATCH (a)-[r:%s]-(b)`, r.Relationship)
|
||||
}
|
||||
|
||||
query = fmt.Sprintf(`%s WHERE a.ID = %s AND b.ID = %s set r.verified = true return r;`, query, r.FirstPersonID, r.SecondPersonID)
|
||||
|
||||
result, err := session.Run(ctx, query, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.Single(ctx)
|
||||
}
|
Reference in New Issue
Block a user