wtf so mutch pain cus i named a package wrong lol
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"log"
|
||||
"ti1/config"
|
||||
"ti1/valkey"
|
||||
"ti1/valki"
|
||||
|
||||
"github.com/valkey-io/valkey-go"
|
||||
)
|
||||
|
||||
func InsertOrUpdateEstimatedCall(db *sql.DB, values []interface{}) (int, string, error) {
|
||||
func InsertOrUpdateEstimatedCall(ctx context.Context, db *sql.DB, values []interface{}, valkeyClient valkey.Client) (int, string, error) {
|
||||
// Replace empty strings with nil for timestamp fields
|
||||
for i, v := range values {
|
||||
if str, ok := v.(string); ok && str == "" {
|
||||
@@ -27,38 +28,30 @@ func InsertOrUpdateEstimatedCall(db *sql.DB, values []interface{}) (int, string,
|
||||
}
|
||||
hash := md5.Sum([]byte(valuesString))
|
||||
hashString := hex.EncodeToString(hash[:])
|
||||
println(hashString)
|
||||
fmt.Println("HashString:", hashString)
|
||||
|
||||
estimatedVehicleJourneyID := values[0]
|
||||
orderID := values[1]
|
||||
key := fmt.Sprintf("%v.%v", estimatedVehicleJourneyID, orderID)
|
||||
fmt.Printf("Estimated Vehicle Journey ID: %v, Order ID: %v\n", estimatedVehicleJourneyID, orderID)
|
||||
|
||||
// Connect to Valkey
|
||||
valkeyClient, err := config.ConnectToValkey("config/conf.json")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to Valkey: %v", err)
|
||||
}
|
||||
defer config.DisconnectFromValkey(valkeyClient)
|
||||
|
||||
// Set the MD5 hash in Valkey
|
||||
err = valkey.SetValkeyValue(valkeyClient, key, hashString)
|
||||
err := valki.SetValkeyValue(ctx, valkeyClient, key, hashString)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to set value in Valkey: %v", err)
|
||||
return 0, "", fmt.Errorf("failed to set value in Valkey: %v", err)
|
||||
}
|
||||
|
||||
// Get the MD5 hash from Valkey
|
||||
retrievedHash, err := valkey.GetValkeyValue(valkeyClient, key)
|
||||
retrievedHash, err := valki.GetValkeyValue(ctx, valkeyClient, key)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get value from Valkey: %v", err)
|
||||
return 0, "", fmt.Errorf("failed to get value from Valkey: %v", err)
|
||||
}
|
||||
|
||||
// Check if the retrieved value matches the original MD5 hash
|
||||
if retrievedHash != hashString {
|
||||
log.Fatalf("Retrieved hash does not match the original hash. Original: %s, Retrieved: %s", hashString, retrievedHash)
|
||||
} else {
|
||||
fmt.Println("Retrieved hash matches the original hash.")
|
||||
return 0, "", fmt.Errorf("hash mismatch: original %s, retrieved %s", hashString, retrievedHash)
|
||||
}
|
||||
fmt.Println("Retrieved hash matches the original hash.")
|
||||
|
||||
query := `
|
||||
INSERT INTO calls (
|
||||
@@ -89,13 +82,6 @@ func InsertOrUpdateEstimatedCall(db *sql.DB, values []interface{}) (int, string,
|
||||
var id int
|
||||
err = stmt.QueryRow(values...).Scan(&action, &id)
|
||||
if err != nil {
|
||||
if 1 == 0 {
|
||||
fmt.Println("Executing query:", query)
|
||||
for i, v := range values {
|
||||
fmt.Printf("Value %d: (%v)\n", i+1, v)
|
||||
}
|
||||
|
||||
}
|
||||
return 0, "", fmt.Errorf("error executing statement: %v", err)
|
||||
}
|
||||
return id, action, nil
|
||||
|
||||
Reference in New Issue
Block a user