Skip to content

Commit

Permalink
Merge pull request #2 from Klausdk1999/develop
Browse files Browse the repository at this point in the history
arrumadando leitura dos arrays
  • Loading branch information
cissavgonc authored Dec 6, 2023
2 parents fe5ea03 + 865102c commit c9b8cfd
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions readings_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"strconv"

"github.com/lib/pq"
_ "github.com/lib/pq"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c9b8cfd

Please sign in to comment.