fix logging in createPerson

This commit is contained in:
2024-04-15 23:46:32 +02:00
parent f5e95095c7
commit d49601b871

View File

@@ -25,6 +25,8 @@ func CreatePerson(driver neo4j.DriverWithContext) gin.HandlerFunc {
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
@@ -32,15 +34,23 @@ func CreatePerson(driver neo4j.DriverWithContext) gin.HandlerFunc {
if err != nil {
log.Printf("ip: %s error: %s", c.ClientIP(), err)
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"})
return
}
person.ID = uuid.New().String()
if person.Verify() != nil {
log.Printf("ip: %s error: %s", c.ClientIP(), err)
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid person information"})
return
}
query := fmt.Sprintf("CREATE (n:Person {%s}) RETURN n;", person.ToString())
result, err := session.Run(ctx, query, nil)
if err != nil {
log.Println(err)
log.Printf("ip: %s error: %s", c.ClientIP(), err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"})
return
@@ -48,7 +58,7 @@ func CreatePerson(driver neo4j.DriverWithContext) gin.HandlerFunc {
rec, err := result.Single(ctx)
if err != nil {
log.Println(err)
log.Printf("ip: %s error: %s", c.ClientIP(), err)
c.JSON(http.StatusNotFound, gin.H{"error": "could not find person with information provided"})
return