hash validation and database insertion logic
This commit is contained in:
@@ -37,12 +37,6 @@ func InsertOrUpdateEstimatedCall(ctx context.Context, db *sql.DB, values []inter
|
|||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// Set the MD5 hash in Valkey
|
|
||||||
err = valki.SetValkeyValue(ctx, valkeyClient, key, hashString)
|
|
||||||
if err != nil {
|
|
||||||
return 0, "", fmt.Errorf("failed to set value in Valkey: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the MD5 hash from Valkey
|
// Get the MD5 hash from Valkey
|
||||||
retrievedHash, err := valki.GetValkeyValue(ctx, valkeyClient, key)
|
retrievedHash, err := valki.GetValkeyValue(ctx, valkeyClient, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -51,10 +45,6 @@ func InsertOrUpdateEstimatedCall(ctx context.Context, db *sql.DB, values []inter
|
|||||||
|
|
||||||
// Check if the retrieved value matches the original MD5 hash
|
// Check if the retrieved value matches the original MD5 hash
|
||||||
if retrievedHash != hashString {
|
if retrievedHash != hashString {
|
||||||
return 0, "", fmt.Errorf("hash mismatch: original %s, retrieved %s", hashString, retrievedHash)
|
|
||||||
}
|
|
||||||
fmt.Println("Retrieved hash matches the original hash.")
|
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
INSERT INTO calls (
|
INSERT INTO calls (
|
||||||
estimatedvehiclejourney, "order", stoppointref,
|
estimatedvehiclejourney, "order", stoppointref,
|
||||||
@@ -80,6 +70,11 @@ func InsertOrUpdateEstimatedCall(ctx context.Context, db *sql.DB, values []inter
|
|||||||
}
|
}
|
||||||
defer stmt.Close()
|
defer stmt.Close()
|
||||||
|
|
||||||
|
err = valki.SetValkeyValue(ctx, valkeyClient, key, hashString)
|
||||||
|
if err != nil {
|
||||||
|
return 0, "", fmt.Errorf("failed to set value in Valkey: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
var action string
|
var action string
|
||||||
var id int
|
var id int
|
||||||
err = stmt.QueryRow(values...).Scan(&action, &id)
|
err = stmt.QueryRow(values...).Scan(&action, &id)
|
||||||
@@ -87,4 +82,8 @@ func InsertOrUpdateEstimatedCall(ctx context.Context, db *sql.DB, values []inter
|
|||||||
return 0, "", fmt.Errorf("error executing statement: %v", err)
|
return 0, "", fmt.Errorf("error executing statement: %v", err)
|
||||||
}
|
}
|
||||||
return id, action, nil
|
return id, action, nil
|
||||||
|
} else {
|
||||||
|
fmt.Println("Hashes match")
|
||||||
|
return 0, "no update", nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user