implement Get person endpoint

This commit is contained in:
2024-04-14 19:21:20 +02:00
parent a4c1bc56f8
commit cb6628a83c
4 changed files with 131 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
package handlers
import (
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)
func ViewPerson(driver neo4j.DriverWithContext) gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
session := driver.NewSession(ctx, neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead})
defer session.Close(ctx)
id := c.Query("id")
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "id is required"})
return
}
query := fmt.Sprintf("MATCH (n:Person) WHERE n.ID = '%s' RETURN n", id)
result, err := session.Run(ctx, query, nil)
if err != nil {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"})
return
}
rec, err := result.Single(ctx)
if err != nil {
log.Println(err)
c.JSON(http.StatusNotFound, gin.H{"error": "could not find person with information provided"})
return
}
c.JSON(200, gin.H{"person": rec.AsMap()})
}
}

View File

@@ -10,6 +10,8 @@ import (
"time"
"github.com/gin-gonic/gin"
"github.com/vcscsvcscs/GenerationsHeritage/backend/handlers"
"github.com/vcscsvcscs/GenerationsHeritage/backend/memgraph"
"github.com/vcscsvcscs/GenerationsHeritage/utilities"
"github.com/vcscsvcscs/GenerationsHeritage/utilities/gin_liveness"
)
@@ -19,6 +21,9 @@ var (
key = flag.String("key", "./private/keys/key.pem", "Specify the path of TLS key")
httpsPort = flag.String("https", ":443", "Specify port for http secure hosting(example for format :443)")
httpPort = flag.String("http", ":80", "Specify port for http hosting(example for format :80)")
memgraphURI = flag.String("memgraph", "bolt+s://memgraph:7687", "Specify the Memgraph database URI")
memgraphUser = flag.String("memgraph-user", "", "Specify the Memgraph database user")
memgraphPass = flag.String("memgraph-pass", "", "Specify the Memgraph database password")
release = flag.Bool("release", false, "Set true to release build")
logToFile = flag.Bool("log-to-file", false, "Set true to log to file")
logToFileAndStd = flag.Bool("log-to-file-and-std", false, "Set true to log to file and std")
@@ -35,8 +40,11 @@ func main() {
hc := gin_liveness.New()
memgraphDriver := memgraph.InitDatabase(*memgraphURI, *memgraphUser, *memgraphPass)
router := gin.Default()
router.GET("/health", hc.HealthCheckHandler())
router.GET("/person", handlers.ViewPerson(memgraphDriver))
server := utilities.SetupHttpsServer(router, *cert, *key, *httpsPort, *httpPort, requestTimeout)

View File

@@ -0,0 +1,67 @@
package memgraph
import (
"log"
"time"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"golang.org/x/net/context"
)
const dbCreateSchemaTimeout = 10 * time.Second
func createIndexes(driver neo4j.DriverWithContext) error {
ctx, cancel := context.WithTimeout(context.Background(), dbCreateSchemaTimeout)
defer cancel()
session := driver.NewSession(ctx, neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite})
defer session.Close(ctx)
indexes := []string{
`CREATE INDEX ON :Person(ID);`,
`CREATE INDEX ON :Person(Surname);`,
`CREATE INDEX ON :Person(Firstname);`,
`CREATE INDEX ON :Person(Born);`,
`CREATE INDEX ON :Person(MothersFirstName);`,
`CREATE INDEX ON :Person(MothersSurname);`,
}
// Run index queries via implicit auto-commit transaction
for _, index := range indexes {
_, err := session.Run(ctx, index, nil)
if err != nil {
log.Panicln(err)
}
}
return nil
}
func createConstraints(driver neo4j.DriverWithContext) error {
ctx, cancel := context.WithTimeout(context.Background(), dbCreateSchemaTimeout)
defer cancel()
session := driver.NewSession(ctx, neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite})
defer session.Close(ctx)
constraints := []string{
`CREATE CONSTRAINT ON (n:Person) ASSERT EXISTS (n.ID);`,
`CREATE CONSTRAINT ON (n:Person) ASSERT EXISTS (n.Surname);`,
`CREATE CONSTRAINT ON (n:Person) ASSERT EXISTS (n.Firstname);`,
`CREATE CONSTRAINT ON (n:Person) ASSERT EXISTS (n.Born);`,
`CREATE CONSTRAINT ON (n:Person) ASSERT EXISTS (n.MothersFirstName);`,
`CREATE CONSTRAINT ON (n:Person) ASSERT EXISTS (n.MothersSurname);`,
`CREATE CONSTRAINT ON (n:Person) ASSERT n.ID IS UNIQUE;`,
`CREATE CONSTRAINT ON (n:Person) ASSERT n.Surname, n.Firstname, n.Born, n.MothersFirstName, n.MothersSurname IS UNIQUE;`,
}
// Run index queries via implicit auto-commit transaction
for _, constraint := range constraints {
_, err := session.Run(ctx, constraint, nil)
if err != nil {
return err
}
}
return nil
}

View File

@@ -21,5 +21,13 @@ func InitDatabase(dbUri, dbUser, dbPassword string) neo4j.DriverWithContext {
log.Panicln(err)
}
if err := createIndexes(driver); err != nil {
log.Panicln(err)
}
if err := createConstraints(driver); err != nil {
log.Panicln(err)
}
return driver
}