This commit is contained in:
Daniel Ledda
2020-11-04 18:51:28 +01:00
parent 997e997d84
commit ff7e787ac2

View File

@@ -40,17 +40,32 @@ func writeSnapshotToDb(snapshotSub *SnapshotSubmission) error {
}
func getSnapshotRecordsFromDb(rowCount int) ([]*SnapshotRecord, error) {
records := make([]*SnapshotRecord, rowCount)
query := fmt.Sprintf("SELECT * FROM `snapshots` ORDER BY `id` DESC LIMIT %v;", rowCount)
snapshots := make([]*SnapshotRecord, rowCount)
query := fmt.Sprintf("SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `snapshots` ORDER BY `id` DESC LIMIT %v;",
"id",
"temp",
"humidity",
"co2",
"time",
rowCount,
)
rows, err := ClimateDb.Query(query)
if err != nil {
return nil, err
}
for i := 0; rows.Next(); i++ {
err = rows.Scan(records[i])
var snapshotRecord SnapshotRecord
err = rows.Scan(
snapshotRecord.Id,
snapshotRecord.Temp,
snapshotRecord.Humidity,
snapshotRecord.Co2,
snapshotRecord.Timestamp,
)
if err != nil {
return nil, err
}
snapshots = append(snapshots, &snapshotRecord)
}
return records, nil
return snapshots, nil
}