add update sensor queries and memory

This commit is contained in:
Pedro Pérez 2025-10-10 00:14:29 +02:00
parent 78723d3fcd
commit 23ec2d5789

View File

@ -6,6 +6,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool" "github.com/jackc/pgx/v5/pgxpool"
) )
@ -42,15 +43,31 @@ func (p *pgxRepo) CreateSensorData(s SensorData) error {
return err return err
} }
const updateSensorQuery = `update sensors set sensor_type = $1, sampling_interval = $2, threshold_above = $3, threshold_below = $4 where sensor_id = $5`
func (p *pgxRepo) UpdateSensor(s Sensor) error { func (p *pgxRepo) UpdateSensor(s Sensor) error {
panic("unimplemented") _, err := p.Exec(context.Background(), updateSensorQuery, string(s.SensorType), s.SamplingInterval, s.ThresholdAbove, s.ThresholdBelow, s.SensorID)
return err
} }
const readSensorBySensorID = ``
func (p *pgxRepo) ReadSensor(sensorID string) (Sensor, error) { func (p *pgxRepo) ReadSensor(sensorID string) (Sensor, error) {
panic("unimplemented") panic("unimplemented")
} }
func (p *pgxRepo) ReadSensorValues(sensorID string, from time.Time, to time.Time) ([]SensorData, error) { const readSensorValuesBySensorID = ``
func (p *pgxRepo) ReadSensorValues(sensorID string, from, to time.Time) ([]SensorData, error) {
fromPgType := pgtype.Timestamp{
Time: from,
Valid: true,
}
toPgType := pgtype.Timestamp{
Time: to,
Valid: true,
}
panic("unimplemented") panic("unimplemented")
} }
@ -139,7 +156,20 @@ func (i *inMemory) CreateSensorData(data SensorData) error {
} }
func (i *inMemory) UpdateSensor(s Sensor) error { func (i *inMemory) UpdateSensor(s Sensor) error {
panic("unimplemented") i.mu.Lock()
defer i.mu.Unlock()
sensor, exists := i.sensors[s.SensorID]
if !exists {
return ErrSensorNotFound
}
sensor.SensorType = s.SensorType
sensor.SamplingInterval = s.SamplingInterval
sensor.ThresholdAbove = s.ThresholdAbove
sensor.ThresholdBelow = s.ThresholdBelow
return nil
} }
func (i *inMemory) ReadSensor(sensorID string) (Sensor, error) { func (i *inMemory) ReadSensor(sensorID string) (Sensor, error) {