diff --git a/Database.go b/Database.go index 47deb46..c1f75c6 100644 --- a/Database.go +++ b/Database.go @@ -26,17 +26,23 @@ func CloseDb() { } } -func writeSnapshotToDb(snapshotSub *SnapshotSubmission) error { - query := fmt.Sprintf("INSERT INTO `snapshots` (`temp`, `humidity`, `co2`, `time`, `id`) VALUES (%v, %v, %v, '%v', NULL);", +func writeSnapshotToDb(snapshotSub *SnapshotSubmission) (int64, error) { + queryStr := "INSERT INTO `snapshots` (`temp`, `humidity`, `co2`, `time`, `id`) VALUES (%v, %v, %v, '%v', NULL);" + query, err := ClimateDb.Prepare(queryStr) + if err != nil { + return -1, fmt.Errorf("couldn't prepare snapshot query: %w", err) + } + result, err := query.Exec( snapshotSub.Temp, snapshotSub.Humidity, snapshotSub.Co2, - snapshotSub.Timestamp) - _, err := ClimateDb.Query(query) + snapshotSub.Timestamp, + ) if err != nil { - return err + return -1, fmt.Errorf("couldn't execute query to write snapshot to db: %w", err) } - return nil + id, _ := result.LastInsertId() + return id, nil } func getLastSnapshotRecordFromDb() (*SnapshotRecord, error) { @@ -55,7 +61,7 @@ func getLastSnapshotRecordFromDb() (*SnapshotRecord, error) { &snapshotRecord.Timestamp, ) if err != nil { - return nil, err + return nil, fmt.Errorf("couldn't read last snapshot from the database: %w", err) } return &snapshotRecord, nil } @@ -67,7 +73,7 @@ func getSnapshotRecordsFromDb(minuteAgo int) ([]*SnapshotRecord, error) { if err != nil { return nil, err } - for i := 0; rows.Next(); i++ { + for rows.Next() { var snapshotRecord SnapshotRecord err = rows.Scan( &snapshotRecord.Id, @@ -77,7 +83,7 @@ func getSnapshotRecordsFromDb(minuteAgo int) ([]*SnapshotRecord, error) { &snapshotRecord.Timestamp, ) if err != nil { - return nil, err + return nil, fmt.Errorf("couldn't read rows from the database: %w", err) } snapshots = append(snapshots, &snapshotRecord) } diff --git a/HandlerHelpers.go b/HandlerHelpers.go index 3cc5d13..e7ad6f0 100644 --- a/HandlerHelpers.go +++ b/HandlerHelpers.go @@ -71,7 +71,7 @@ func getPatternHandler(pattern string) http.HandlerFunc { err = cb(w, r) } if err != nil { - sendInternalError(err, w, r) + internalErrorOnErr(err, w, r) } } } diff --git a/Snapshot.go b/Snapshot.go index e7f85b3..57628e8 100644 --- a/Snapshot.go +++ b/Snapshot.go @@ -43,7 +43,7 @@ func createSnapshotSubFromJsonBodyStream(jsonBodyStream io.ReadCloser) (*Snapsho func createJsonFromSnapshotRecords(records []*SnapshotRecord) ([]byte, error) { jsonResult, err := json.Marshal(SnapshotPayload{records}) if err != nil { - return nil, err + return nil, fmt.Errorf("couldn't marshal json: %w", err) } return jsonResult, nil } \ No newline at end of file diff --git a/climate-server.go b/climate-server.go index 62c8840..273d691 100644 --- a/climate-server.go +++ b/climate-server.go @@ -66,61 +66,43 @@ func sendData(w http.ResponseWriter, r *http.Request) { count = newCount } records, err := getSnapshotRecordsFromDb(int(count)) - if err != nil { - sendInternalError(fmt.Errorf("couldn't read rows from the database: %w", err), w, r) - return - } + if internalErrorOnErr(fmt.Errorf("couldn't get snapshots from db: %w", err), w, r) { return } json, err := createJsonFromSnapshotRecords(records) - if err != nil { - sendInternalError(fmt.Errorf("couldn't create a json from the records: %w", err), w, r) - return - } + if internalErrorOnErr(fmt.Errorf("couldn't create json from snapshots: %w", err), w, r) { return } w.Header().Set("Content-Type", "application/json") - _, err = fmt.Fprintf(w, string(json)) - if err != nil { - sendInternalError(err, w, r) - return - } + fmt.Fprintf(w, string(json)) } func sendLastSnapshot(w http.ResponseWriter, r *http.Request) { record, err := getLastSnapshotRecordFromDb() - if err != nil { - sendInternalError(fmt.Errorf("couldn't read last snapshot from the database: %w", err), w, r) - return - } + if internalErrorOnErr(fmt.Errorf("couldn't get last snapshot from db: %w", err), w, r) { return } json, err := createJsonFromSnapshotRecords([]*SnapshotRecord{record}) - if err != nil { - sendInternalError(fmt.Errorf("couldn't create a json from the last record: %w", err), w, r) - return - } + if internalErrorOnErr(fmt.Errorf("couldn't create json from last snapshots: %w", err), w, r) { return } w.Header().Set("Content-Type", "application/json") - _, err = fmt.Fprintf(w, string(json)) - if err != nil { - sendInternalError(err, w, r) - return - } + fmt.Fprintf(w, string(json)) } func saveSnapshot(w http.ResponseWriter, r *http.Request) { snapshotSub, err := createSnapshotSubFromJsonBodyStream(r.Body) - if err != nil { - sendInternalError(fmt.Errorf("couldn't create snapshot from JSON: %w", err), w, r) - } - err = writeSnapshotToDb(snapshotSub) - if err != nil { - sendInternalError(fmt.Errorf("couldn't submit snapshot into the database: %w", err), w, r) - } + if internalErrorOnErr(fmt.Errorf("couldn't create snapshot from JSON: %w", err), w, r) { return } + newId, err := writeSnapshotToDb(snapshotSub) + if internalErrorOnErr(fmt.Errorf("couldn't submit snapshot into the database: %w", err), w, r) { return } + fmt.Fprintf(w, "{id: %v}", newId) } -func sendInternalError(err error, w http.ResponseWriter, r *http.Request) { - errorMessage := "Internal Server Error!" - if DEBUG { - errorMessage += fmt.Sprintf(" Happened during %s request for pattern '%s': %s", - r.Method, - r.URL, - err.Error()) +func internalErrorOnErr(err error, w http.ResponseWriter, r *http.Request) bool { + if err != nil { + errorMessage := "Internal Server Error!" + if DEBUG { + errorMessage += fmt.Sprintf(" Happened during %s request for pattern '%s': %s", + r.Method, + r.URL, + err.Error()) + } + fmt.Println(errorMessage) + http.Error(w, errorMessage, 500) + return true } - fmt.Println(errorMessage) - http.Error(w, errorMessage, 500) + return false } +