Add Person model

This commit is contained in:
2024-04-14 23:49:33 +02:00
parent cb6628a83c
commit 3b12f4798c
8 changed files with 220 additions and 9 deletions

View File

@@ -18,6 +18,7 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.19.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect

View File

@@ -31,6 +31,8 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=

View File

@@ -0,0 +1,59 @@
package handlers
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/vcscsvcscs/GenerationsHeritage/backend/memgraph"
)
func CreatePerson(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)
if c.Request.Body == nil || c.ContentType() != "application/json" {
log.Printf("ip: %s error: request body is empty or content type is not application/json", c.ClientIP())
c.JSON(http.StatusBadRequest, gin.H{"error": "content type must be application/json and request body must not be empty"})
}
var person memgraph.Person
err := json.NewDecoder(c.Request.Body).Decode(&person)
if err != nil {
log.Printf("ip: %s error: %s", c.ClientIP(), err)
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"})
}
person.ID = uuid.New().String()
query := fmt.Sprintf("CREATE (n:Person {%s}) RETURN n;", person.ToString())
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

@@ -25,7 +25,7 @@ func ViewPerson(driver neo4j.DriverWithContext) gin.HandlerFunc {
return
}
query := fmt.Sprintf("MATCH (n:Person) WHERE n.ID = '%s' RETURN n", id)
query := fmt.Sprintf("MATCH (n:Person) WHERE n.ID = '%s' RETURN n;", id)
result, err := session.Run(ctx, query, nil)
if err != nil {

View File

@@ -45,6 +45,7 @@ func main() {
router := gin.Default()
router.GET("/health", hc.HealthCheckHandler())
router.GET("/person", handlers.ViewPerson(memgraphDriver))
router.POST("/createPerson", handlers.CreatePerson(memgraphDriver))
server := utilities.SetupHttpsServer(router, *cert, *key, *httpsPort, *httpPort, requestTimeout)

View File

@@ -19,11 +19,11 @@ func createIndexes(driver neo4j.DriverWithContext) error {
indexes := []string{
`CREATE INDEX ON :Person(ID);`,
`CREATE INDEX ON :Person(Surname);`,
`CREATE INDEX ON :Person(Lastname);`,
`CREATE INDEX ON :Person(Firstname);`,
`CREATE INDEX ON :Person(Born);`,
`CREATE INDEX ON :Person(MothersFirstName);`,
`CREATE INDEX ON :Person(MothersSurname);`,
`CREATE INDEX ON :Person(MothersFirstname);`,
`CREATE INDEX ON :Person(MothersLastname);`,
}
// Run index queries via implicit auto-commit transaction
@@ -46,13 +46,13 @@ func createConstraints(driver neo4j.DriverWithContext) error {
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.Lastname);`,
`CREATE CONSTRAINT ON (n:Person) ASSERT EXISTS (n.Firstame);`,
`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 EXISTS (n.MothersFirstname);`,
`CREATE CONSTRAINT ON (n:Person) ASSERT EXISTS (n.MothersLastname);`,
`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;`,
`CREATE CONSTRAINT ON (n:Person) ASSERT n.Lastname, n.Firstname, n.Born, n.MothersFirstname, n.MothersLastname IS UNIQUE;`,
}
// Run index queries via implicit auto-commit transaction

71
backend/memgraph/model.go Normal file
View File

@@ -0,0 +1,71 @@
package memgraph
import (
"fmt"
"time"
)
type Person struct {
ID string `json:"id"`
Firstname string `json:"first_name"`
Lastname string `json:"last_name"`
MothersFirstname string `json:"mothers_first_name"`
MothersLastname string `json:"mothers_last_name"`
Born time.Time `json:"born"`
Birthplace string `json:"birthplace"`
Residence string `json:"residence"`
Death time.Time `json:"death"`
Deathplace string `json:"deathplace"`
LifeEvents []map[string]string `json:"life_events"`
Occupations []string `json:"occupation"`
OccupationToDisplay string `json:"occupation_to_display"`
OthersSaid map[string]string `json:"others_said"`
Photos map[string]string `json:"photos"`
ProfilePicture string `json:"profile_picture"`
}
func (p *Person) ToString() string {
result := fmt.Sprintf("ID: '%s', Firstname: '%s', Lastname: '%s', MothersFirstname: '%s', MothersLastname: '%s'", p.ID, p.Firstname, p.Lastname, p.MothersFirstname, p.MothersLastname)
result = fmt.Sprintf("%s, Born: date({year:%d, month:%d, day:%d}), Death: date({year:%d, month:%d, day:%d})", result, p.Born.Year(), p.Born.Month(), p.Born.Day(), p.Death.Year(), p.Death.Month(), p.Death.Day())
result = fmt.Sprintf("%s, Birthplace: '%s', Residence: '%s', Deathplace: '%s', OccupationToDisplay: '%s', ProfilePicture: '%s'", result, p.Birthplace, p.Residence, p.Deathplace, p.OccupationToDisplay, p.ProfilePicture)
if p.LifeEvents != nil && len(p.LifeEvents) > 0 {
result = fmt.Sprintf("%s, LifeEvents: [", result)
for i := 0; i < len(p.LifeEvents); i++ {
date, dok := p.LifeEvents[i]["date"]
event, eok := p.LifeEvents[i]["event"]
if dok && eok {
result = fmt.Sprintf("%s{date: '%s', event: '%s'}, ", result, date, event)
}
}
result = fmt.Sprintf("%s]", result[:len(result)-2])
}
if p.Occupations != nil && len(p.Occupations) > 0 {
result = fmt.Sprintf("%s, Occupations: [", result)
for _, occupation := range p.Occupations {
result = fmt.Sprintf("%s'%s', ", result, occupation)
}
result = fmt.Sprintf("%s]", result[:len(result)-2])
}
if p.OthersSaid != nil {
result = fmt.Sprintf("%s, OthersSaid: {", result)
for key, value := range p.OthersSaid {
result = fmt.Sprintf("%s%s: '%s', ", result, key, value)
}
result = fmt.Sprintf("%s}", result[:len(result)-2])
}
if p.Photos != nil && len(p.Photos) > 0 {
result = fmt.Sprintf("%s, Photos: {", result)
for key, value := range p.Photos {
result = fmt.Sprintf("%s%s: '%s', ", result, key, value)
}
result = fmt.Sprintf("%s}", result[:len(result)-2])
}
return result
}

View File

@@ -0,0 +1,77 @@
package memgraph
import (
"testing"
"time"
)
func TestPerson_ToString(t *testing.T) {
tests := []struct {
name string
p *Person
want string
}{
{
name: "Test with nil values",
p: &Person{
ID: "1",
Firstname: "John",
Lastname: "Doe",
MothersFirstname: "Jane",
MothersLastname: "Doe",
Born: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Birthplace: "New York",
Residence: "New York",
Death: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Deathplace: "New York",
},
want: "ID: '1', Firstname: 'John', Lastname: 'Doe', MothersFirstname: 'Jane', MothersLastname: 'Doe', Born: date({year:2021, month:1, day:1}), Death: date({year:2021, month:1, day:1}), Birthplace: 'New York', Residence: 'New York', Deathplace: 'New York', OccupationToDisplay: '', ProfilePicture: ''",
}, {
name: "Test with All values",
p: &Person{
ID: "1",
Firstname: "John",
Lastname: "Doe",
MothersFirstname: "Jane",
MothersLastname: "Doe",
Born: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Birthplace: "New York",
Residence: "New York",
Death: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Deathplace: "New York",
LifeEvents: []map[string]string{
{
"date": "2021-01-01",
"event": "Event 1",
},
{
"date": "2021-01-02",
"event": "Event 2",
},
},
Occupations: []string{
"Welder",
"Plumber",
},
OccupationToDisplay: "Welder",
OthersSaid: map[string]string{
"Beni": "He is a good person",
"Jani": "He is a bad person",
},
Photos: map[string]string{
"Profile": "profile.jpg",
"Family": "family.jpg",
},
ProfilePicture: "profile.jpg",
},
want: "ID: '1', Firstname: 'John', Lastname: 'Doe', MothersFirstname: 'Jane', MothersLastname: 'Doe', Born: date({year:2021, month:1, day:1}), Death: date({year:2021, month:1, day:1}), Birthplace: 'New York', Residence: 'New York', Deathplace: 'New York', OccupationToDisplay: 'Welder', ProfilePicture: 'profile.jpg', LifeEvents: [{date: '2021-01-01', event: 'Event 1'}, {date: '2021-01-02', event: 'Event 2'}], Occupations: ['Welder', 'Plumber'], OthersSaid: {Beni: 'He is a good person', Jani: 'He is a bad person'}, Photos: {Profile: 'profile.jpg', Family: 'family.jpg'}",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.p.ToString(); got != tt.want {
t.Errorf("Person.ToString() = %v, want %v", got, tt.want)
}
})
}
}