implement google queries

This commit is contained in:
2025-03-28 22:15:12 +01:00
parent 2d3f8cfa34
commit 723c679ad0
6 changed files with 235 additions and 4 deletions

View File

@@ -1,11 +1,71 @@
package api
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/internal/memgraph"
"github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/pkg/api"
)
func (srv *server) CreatePerson(c *gin.Context) {}
func (srv *server) CreatePerson(c *gin.Context, params api.CreatePersonParams) {
var person *api.PersonProperties
if err := c.ShouldBindJSON(&person); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
return
}
adminList := []struct {
Id *int "json:\"id,omitempty\""
Name *string "json:\"name,omitempty\""
}{
{Id: &params.XUserID, Name: &params.XUserName},
}
person.AllowAdminAccess = &adminList
ctx, cancel := context.WithTimeout(context.Background(), srv.dbOpTimeout)
defer cancel()
session := srv.db.NewSession(ctx, neo4j.SessionConfig{})
qctx, qCancel := context.WithTimeout(context.Background(), srv.dbOpTimeout)
defer qCancel()
res, err := session.ExecuteRead(qctx, memgraph.CreatePerson(qctx, person))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()})
return
}
c.JSON(http.StatusOK, res)
}
func (srv *server) GetPersonById(c *gin.Context, id int, params api.GetPersonByIdParams) {
ctx, cancel := context.WithTimeout(context.Background(), srv.dbOpTimeout)
defer cancel()
session := srv.db.NewSession(ctx, neo4j.SessionConfig{})
actx, acancel := context.WithTimeout(context.Background(), srv.dbOpTimeout)
defer acancel()
if !userWithIdHasAccessToGivenPerson(actx, session, params.XUserID, id) {
c.JSON(http.StatusUnauthorized, gin.H{"msg": "User does not have access to this person"})
return
}
qctx, qCancel := context.WithTimeout(context.Background(), srv.dbOpTimeout)
defer qCancel()
res, err := session.ExecuteRead(qctx, memgraph.GetPersonById(qctx, id))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()})
return
}
c.JSON(http.StatusOK, res)
}
func (srv *server) SoftDeletePerson(c *gin.Context, id int, params api.SoftDeletePersonParams) {}

View File

@@ -2,18 +2,81 @@ package api
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/internal/memgraph"
"github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/pkg/api"
)
func (srv *server) GetPersonByGoogleId(c *gin.Context, googleId string) {
ctx, cancel := context.WithTimeout(context.Background(), srv.dbOpTimeout)
defer cancel()
session := srv.db.NewSession(ctx, neo4j.SessionConfig{})
session.ExecuteRead()
qctx, qCancel := context.WithTimeout(context.Background(), srv.dbOpTimeout)
defer qCancel()
res, err := session.ExecuteRead(qctx, memgraph.GetPersonByGoogleId(qctx, googleId))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()})
return
}
c.JSON(http.StatusOK, res)
}
func (srv *server) CreatePersonByGoogleIdAndInviteCode(c *gin.Context, googleId string) {}
func (srv *server) CreatePersonByGoogleIdAndInviteCode(c *gin.Context, googleId string) {
var person struct {
InviteCode string `json:"invite_code"`
Props *api.PersonProperties `json:"person"`
}
if err := c.ShouldBindJSON(&person); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
func (srv *server) CreatePersonByGoogleId(c *gin.Context, googleId string) {}
return
}
emptyString := ""
person.Props.InviteCode = &emptyString
ctx, cancel := context.WithTimeout(context.Background(), srv.dbOpTimeout)
defer cancel()
session := srv.db.NewSession(ctx, neo4j.SessionConfig{})
qctx, qCancel := context.WithTimeout(context.Background(), srv.dbOpTimeout)
defer qCancel()
res, err := session.ExecuteRead(qctx, memgraph.UpdatePersonByInviteCode(qctx, person.InviteCode, person.Props))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()})
return
}
c.JSON(http.StatusOK, res)
}
func (srv *server) CreatePersonByGoogleId(c *gin.Context, googleId string) {
var person *api.PersonProperties
if err := c.ShouldBindJSON(&person); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
return
}
person.GoogleId = &googleId // just making sure :)
ctx, cancel := context.WithTimeout(context.Background(), srv.dbOpTimeout)
defer cancel()
session := srv.db.NewSession(ctx, neo4j.SessionConfig{})
qctx, qCancel := context.WithTimeout(context.Background(), srv.dbOpTimeout)
defer qCancel()
res, err := session.ExecuteRead(qctx, memgraph.CreatePerson(qctx, person))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()})
return
}
c.JSON(http.StatusOK, res)
}

View File

@@ -0,0 +1,44 @@
package memgraph
import (
"context"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/pkg/api"
)
func CreatePerson(ctx context.Context, Person *api.PersonProperties) neo4j.ManagedTransactionWork {
return func(tx neo4j.ManagedTransaction) (any, error) {
result, err := tx.Run(ctx, CreatePersonCypherQuery, map[string]any{
"Person": *Person,
})
if err != nil {
return nil, err
}
record, err := result.Single(ctx)
if err != nil {
return nil, err
}
return record.AsMap(), nil
}
}
func GetPersonById(ctx context.Context, id int) neo4j.ManagedTransactionWork {
return func(tx neo4j.ManagedTransaction) (any, error) {
result, err := tx.Run(ctx, GetPersonCypherQuery, map[string]any{
"id": id,
})
if err != nil {
return nil, err
}
record, err := result.Single(ctx)
if err != nil {
return nil, err
}
return record.AsMap(), nil
}
}

View File

@@ -0,0 +1,45 @@
package memgraph
import (
"context"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/pkg/api"
)
func GetPersonByGoogleId(ctx context.Context, googleId string) neo4j.ManagedTransactionWork {
return func(tx neo4j.ManagedTransaction) (any, error) {
result, err := tx.Run(ctx, GetPersonByGoogleIdCypherQuery, map[string]any{
"google_id": googleId,
})
if err != nil {
return nil, err
}
record, err := result.Single(ctx)
if err != nil {
return nil, err
}
return record.AsMap(), nil
}
}
func UpdatePersonByInviteCode(ctx context.Context, inviteCode string, Person *api.PersonProperties) neo4j.ManagedTransactionWork {
return func(tx neo4j.ManagedTransaction) (any, error) {
result, err := tx.Run(ctx, UpdatePersonByInviteCodeCypherQuery, map[string]any{
"invite_code": inviteCode,
"props": *Person,
})
if err != nil {
return nil, err
}
record, err := result.Single(ctx)
if err != nil {
return nil, err
}
return record.AsMap(), nil
}
}

View File

@@ -16,45 +16,60 @@ var DropConstraintsCypherQuery string
// Requires Person parameter.
// Returns p as person
//
//go:embed queries/create_person.cypher
var CreatePersonCypherQuery string
// Requires id parameter.
// Returns n as person
//
//go:embed queries/get_person_by_id.cypher
var GetPersonCypherQuery string
// Requires id, props parameter.
// Returns n as person
//
//go:embed queries/update_person.cypher
var UpdatePersonCypherQuery string
// Requires invite_code, props parameter.
//
//go:embed queries/update_person_by_invite_code.cypher
var UpdatePersonByInviteCodeCypherQuery string
// Requires google_id parameter.
// Returns n as person
//
//go:embed queries/get_person_by_google_id.cypher
var GetPersonByGoogleIdCypherQuery string
// Requires id parameter.
// Returns labels(n) AS labels, n AS person
//
//go:embed queries/soft_delete_person_by_id.cypher
var SoftDeletePersonCypherQuery string
// Requires id parameter.
//
//go:embed queries/hard_delete_person_by_id.cypher
var HardDeletePersonCypherQuery string
// Requires id1, id2 parameters.
//
//go:embed queries/get_relationship.cypher
var GetRelationshipCypherQuery string
// Requires id1, id2, Relationship parameters.
//
//go:embed queries/create_directed_relationship.cypher
var CreateDirectedRelationshipCypherQuery string
// Requires id1, id2, Relationship1, Relationship2 parameters.
//
//go:embed queries/create_two_directed_relationships.cypher
var CreateTwoDirectedRelationshipCypherQuery string
// Requires id parameter.
//
//go:embed queries/get_family_tree_by_id.cypher
var GetFamilyTreeByIdCypherQuery string

View File

@@ -0,0 +1,4 @@
MATCH (n:Person)
WHERE n.invite_code = $invite_code
SET n += $props
RETURN n AS person