diff --git a/readings_handler.go b/readings_handler.go index 535a9af..710fffe 100644 --- a/readings_handler.go +++ b/readings_handler.go @@ -5,6 +5,7 @@ import ( "net/http" "strconv" + "github.com/lib/pq" _ "github.com/lib/pq" ) @@ -33,7 +34,8 @@ func getAllReadings(w http.ResponseWriter, r *http.Request) { var readings []Reading for rows.Next() { var reading Reading - err := rows.Scan(&reading.ID, &reading.UserID, &reading.Timestamp, &reading.Value, &reading.TorqueValues, &reading.AsmTimes, &reading.MotionWastes, &reading.SetValue) + err := rows.Scan(&reading.ID, &reading.UserID, &reading.Timestamp, &reading.Value, pq.Array(&reading.TorqueValues), pq.Array(&reading.AsmTimes), pq.Array(&reading.MotionWastes), &reading.SetValue) + if err != nil { http.Error(w, "Error scanning readings", http.StatusInternalServerError) return @@ -63,8 +65,9 @@ func createReading(w http.ResponseWriter, r *http.Request) { } // Assuming timestamp is auto-generated by the database - sqlStatement := `INSERT INTO readings (user_id, value) VALUES ($1, $2)` - _, err = db.Exec(sqlStatement, reading.UserID, reading.Value) + sqlStatement := `INSERT INTO readings (user_id, value, torque_values, asm_times, motion_wastes, set_value) VALUES ($1, $2, $3, $4, $5, $6)` + _, err = db.Exec(sqlStatement, reading.UserID, reading.Value, pq.Array(reading.TorqueValues), pq.Array(reading.AsmTimes), pq.Array(reading.MotionWastes), reading.SetValue) + if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -97,7 +100,8 @@ func getUserReadings(w http.ResponseWriter, r *http.Request) { var readings []Reading for rows.Next() { var reading Reading - err := rows.Scan(&reading.ID, &reading.UserID, &reading.Timestamp, &reading.Value, &reading.TorqueValues, &reading.AsmTimes, &reading.MotionWastes, &reading.SetValue) + err := rows.Scan(&reading.ID, &reading.UserID, &reading.Timestamp, &reading.Value, pq.Array(&reading.TorqueValues), pq.Array(&reading.AsmTimes), pq.Array(&reading.MotionWastes), &reading.SetValue) + if err != nil { http.Error(w, "Error scanning readings", http.StatusInternalServerError) return