Skip to content

Commit

Permalink
Merge pull request #3 from Klausdk1999/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
cissavgonc authored Dec 6, 2023
2 parents c9b8cfd + a8b3497 commit 950e755
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
13 changes: 13 additions & 0 deletions documentation/Get_all_readings-1701839548674.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
HTTP/2 500
date: Wed, 06 Dec 2023 05:11:59 GMT
content-type: text/plain; charset=utf-8
cf-ray: 831207fd8bbff8f9-NVT
cf-cache-status: DYNAMIC
vary: Accept-Encoding
rndr-id: 26e2e19f-af75-49a5
x-content-type-options: nosniff
x-render-origin-server: Render
server: cloudflare
alt-svc: h3=":443"; ma=86400

Database query error
13 changes: 6 additions & 7 deletions readings_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"strconv"

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

Expand All @@ -24,7 +23,7 @@ func getAllReadings(w http.ResponseWriter, r *http.Request) {
db := OpenConnection()
defer db.Close()

rows, err := db.Query("SELECT id, user_id, timestamp, value, torque_values, asm_times, motion_wastes, set_value FROM readings")
rows, err := db.Query("SELECT id, user_id, timestamp, value FROM readings")
if err != nil {
http.Error(w, "Database query error", http.StatusInternalServerError)
return
Expand All @@ -34,7 +33,7 @@ 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, pq.Array(&reading.TorqueValues), pq.Array(&reading.AsmTimes), pq.Array(&reading.MotionWastes), &reading.SetValue)
err := rows.Scan(&reading.ID, &reading.UserID, &reading.Timestamp, &reading.Value)

if err != nil {
http.Error(w, "Error scanning readings", http.StatusInternalServerError)
Expand Down Expand Up @@ -65,8 +64,8 @@ func createReading(w http.ResponseWriter, r *http.Request) {
}

// Assuming timestamp is auto-generated by the database
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)
sqlStatement := `INSERT INTO readings (user_id, value) VALUES ($1, $2)`
_, err = db.Exec(sqlStatement, reading.UserID, reading.Value)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -90,7 +89,7 @@ func getUserReadings(w http.ResponseWriter, r *http.Request) {
}

// Query database for readings belonging to the user
rows, err := db.Query("SELECT id, user_id, timestamp, value, torque_values, asm_times, motion_wastes, set_value FROM readings WHERE user_id = $1", userID)
rows, err := db.Query("SELECT id, user_id, timestamp, value FROM readings WHERE user_id = $1", userID)
if err != nil {
http.Error(w, "Database query error", http.StatusInternalServerError)
return
Expand All @@ -100,7 +99,7 @@ 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, pq.Array(&reading.TorqueValues), pq.Array(&reading.AsmTimes), pq.Array(&reading.MotionWastes), &reading.SetValue)
err := rows.Scan(&reading.ID, &reading.UserID, &reading.Timestamp, &reading.Value)

if err != nil {
http.Error(w, "Error scanning readings", http.StatusInternalServerError)
Expand Down
8 changes: 5 additions & 3 deletions user_readings_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"

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

Expand Down Expand Up @@ -33,17 +34,18 @@ func UserReadingsHandler(w http.ResponseWriter, r *http.Request) {
}

// Query database for readings belonging to the user
rows, err := db.Query("SELECT id, user_id, timestamp, value FROM readings WHERE user_id = $1", userID)
rows, err := db.Query("SELECT id, user_id, timestamp, value, torque_values, asm_times, motion_wastes, set_value FROM readings WHERE user_id = $1", userID)
if err != nil {
http.Error(w, "Database query error", http.StatusInternalServerError)
return
}
defer rows.Close()

var readings []Reading
var readings []Reading
for rows.Next() {
var reading Reading
err := rows.Scan(&reading.ID, &reading.UserID, &reading.Timestamp, &reading.Value)
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 950e755

Please sign in to comment.