Return empty array when there're no rows

This commit is contained in:
Jan Zípek 2022-09-03 22:54:53 +02:00
parent 3d37331fce
commit 79c7bf3851
Signed by: kamen
GPG Key ID: A17882625B33AC31
3 changed files with 19 additions and 1 deletions

View File

@ -1,5 +1,7 @@
package services package services
import "database/sql"
type DashboardsService struct { type DashboardsService struct {
ctx *Context ctx *Context
} }
@ -16,6 +18,10 @@ func (s *DashboardsService) GetList() ([]DashboardItem, error) {
rows, err := s.ctx.DB.Query("SELECT id, name, contents FROM dashboards") rows, err := s.ctx.DB.Query("SELECT id, name, contents FROM dashboards")
if err != nil { if err != nil {
if err == sql.ErrNoRows {
return items, nil
}
return nil, err return nil, err
} }

View File

@ -1,6 +1,9 @@
package services package services
import "time" import (
"database/sql"
"time"
)
type SensorValuesService struct { type SensorValuesService struct {
ctx *Context ctx *Context
@ -29,6 +32,10 @@ func (s *SensorValuesService) GetList(sensorId int64, from int64, to int64) ([]s
rows, err := s.ctx.DB.Query("SELECT timestamp, value FROM sensor_values WHERE sensor_id = ? AND timestamp > ? AND timestamp < ? ORDER BY timestamp ASC", sensorId, from, to) rows, err := s.ctx.DB.Query("SELECT timestamp, value FROM sensor_values WHERE sensor_id = ? AND timestamp > ? AND timestamp < ? ORDER BY timestamp ASC", sensorId, from, to)
if err != nil { if err != nil {
if err == sql.ErrNoRows {
return values, nil
}
return nil, err return nil, err
} }

View File

@ -2,6 +2,7 @@ package services
import ( import (
"crypto/rand" "crypto/rand"
"database/sql"
"math/big" "math/big"
) )
@ -21,6 +22,10 @@ func (s *SensorsService) GetList() ([]SensorItem, error) {
rows, err := s.ctx.DB.Query("SELECT id, name, auth_key FROM sensors") rows, err := s.ctx.DB.Query("SELECT id, name, auth_key FROM sensors")
if err != nil { if err != nil {
if err == sql.ErrNoRows {
return sensors, nil
}
return nil, err return nil, err
} }