mirror of
https://github.com/vcscsvcscs/GenerationsHeritage.git
synced 2025-08-13 22:39:06 +02:00
delete person
This commit is contained in:
@@ -1 +1,48 @@
|
||||
package handlers
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
||||
"github.com/vcscsvcscs/GenerationsHeritage/backend/memgraph"
|
||||
)
|
||||
|
||||
func DeletePerson(driver neo4j.DriverWithContext) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
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"})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
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"})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if person.ID != "" {
|
||||
log.Printf("ip: %s error: %s", c.ClientIP(), err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "no person ID provided"})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err = person.DeletePerson(driver)
|
||||
if err != nil {
|
||||
log.Printf("ip: %s error: %s", c.ClientIP(), err)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "could not delete person with ID provided"})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "person deleted successfully"})
|
||||
}
|
||||
}
|
||||
|
27
backend/memgraph/delete_person.go
Normal file
27
backend/memgraph/delete_person.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package memgraph
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func (p *Person) DeletePerson(driver neo4j.DriverWithContext) 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 := p.Verify(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
query := fmt.Sprintf("MATCH (n:Person {ID: '%s'}) DELETE n;", p.ID)
|
||||
|
||||
_, err := session.Run(ctx, query, nil)
|
||||
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user