add userId to sensor
This commit is contained in:
parent
fafcf180e4
commit
7543dae153
3 changed files with 44 additions and 26 deletions
|
@ -167,7 +167,17 @@ func (api *weatherRestApi) registerWeatherSensorHandler(w http.ResponseWriter, r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *weatherRestApi) getAllWeatherSensorHandler(w http.ResponseWriter, r *http.Request) {
|
func (api *weatherRestApi) getAllWeatherSensorHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
weatherSensors, err := api.sensorRegistry.GetSensors()
|
var weatherSensors []*storage.WeatherSensor
|
||||||
|
var err error
|
||||||
|
|
||||||
|
userId := r.URL.Query().Get("userId")
|
||||||
|
|
||||||
|
if len(userId) == 0 {
|
||||||
|
weatherSensors, err = api.sensorRegistry.GetSensors()
|
||||||
|
} else {
|
||||||
|
weatherSensors, err = api.sensorRegistry.GetSensorsOfUser(userId)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "", http.StatusNotFound)
|
http.Error(w, "", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
|
||||||
"weather-data/config"
|
"weather-data/config"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
@ -15,7 +14,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type mongodbSensorRegistry struct {
|
type mongodbSensorRegistry struct {
|
||||||
weatherSensors []*WeatherSensor
|
|
||||||
sensorCollection *mongo.Collection
|
sensorCollection *mongo.Collection
|
||||||
client *mongo.Client
|
client *mongo.Client
|
||||||
}
|
}
|
||||||
|
@ -32,14 +30,13 @@ func NewMongodbSensorRegistry(mongoCfg config.MongoConfig) (*mongodbSensorRegist
|
||||||
|
|
||||||
sensorRegistry.client = client
|
sensorRegistry.client = client
|
||||||
|
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
err = client.Connect(context.Background())
|
||||||
err = client.Connect(ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = client.Ping(ctx, readpref.Primary())
|
err = client.Ping(context.Background(), readpref.Primary())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -65,32 +62,29 @@ func (registry *mongodbSensorRegistry) RegisterSensorByName(name string) (*Weath
|
||||||
sensor.Name = name
|
sensor.Name = name
|
||||||
sensor.Id = uuid.New()
|
sensor.Id = uuid.New()
|
||||||
|
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
_, err = registry.sensorCollection.InsertOne(context.Background(), sensor)
|
||||||
_, err = registry.sensorCollection.InsertOne(ctx, sensor)
|
|
||||||
|
|
||||||
return sensor, err
|
return sensor, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *mongodbSensorRegistry) ExistSensorName(name string) (bool, error) {
|
func (registry *mongodbSensorRegistry) ExistSensorName(name string) (bool, error) {
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
cursor, err := registry.sensorCollection.Find(context.Background(), bson.M{"name": name})
|
||||||
cursor, err := registry.sensorCollection.Find(ctx, bson.M{"name": name})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return cursor.Next(ctx), nil
|
return cursor.Next(context.Background()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *mongodbSensorRegistry) ResolveSensorById(sensorId uuid.UUID) (*WeatherSensor, error) {
|
func (registry *mongodbSensorRegistry) ResolveSensorById(sensorId uuid.UUID) (*WeatherSensor, error) {
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
cursor, err := registry.sensorCollection.Find(context.Background(), bson.M{"id": sensorId})
|
||||||
cursor, err := registry.sensorCollection.Find(ctx, bson.M{"id": sensorId})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !cursor.Next(ctx) {
|
if !cursor.Next(context.Background()) {
|
||||||
return nil, errors.New("sensor does not exist")
|
return nil, errors.New("sensor does not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,26 +97,40 @@ func (registry *mongodbSensorRegistry) ResolveSensorById(sensorId uuid.UUID) (*W
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *mongodbSensorRegistry) ExistSensor(sensorId uuid.UUID) (bool, error) {
|
func (registry *mongodbSensorRegistry) ExistSensor(sensorId uuid.UUID) (bool, error) {
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
cursor, err := registry.sensorCollection.Find(context.Background(), bson.M{"id": sensorId})
|
||||||
cursor, err := registry.sensorCollection.Find(ctx, bson.M{"id": sensorId})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return cursor.Next(ctx), nil
|
return cursor.Next(context.Background()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *mongodbSensorRegistry) GetSensors() ([]*WeatherSensor, error) {
|
func (registry *mongodbSensorRegistry) GetSensors() ([]*WeatherSensor, error) {
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
cursor, err := registry.sensorCollection.Find(context.Background(), bson.M{})
|
||||||
cursor, err := registry.sensorCollection.Find(ctx, bson.M{})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var readData []*WeatherSensor = make([]*WeatherSensor, 0)
|
var readData []*WeatherSensor = make([]*WeatherSensor, 0)
|
||||||
if err = cursor.All(ctx, &readData); err != nil {
|
if err = cursor.All(context.Background(), &readData); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return readData, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (registry *mongodbSensorRegistry) GetSensorsOfUser(userId string) ([]*WeatherSensor, error) {
|
||||||
|
cursor, err := registry.sensorCollection.Find(context.Background(), bson.M{"userid": userId})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var readData []*WeatherSensor = make([]*WeatherSensor, 0)
|
||||||
|
if err = cursor.All(context.Background(), &readData); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -131,8 +139,7 @@ func (registry *mongodbSensorRegistry) GetSensors() ([]*WeatherSensor, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *mongodbSensorRegistry) DeleteSensor(sensorId uuid.UUID) error {
|
func (registry *mongodbSensorRegistry) DeleteSensor(sensorId uuid.UUID) error {
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
res, err := registry.sensorCollection.DeleteOne(context.Background(), bson.M{"id": sensorId})
|
||||||
res, err := registry.sensorCollection.DeleteOne(ctx, bson.M{"id": sensorId})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return err
|
return err
|
||||||
|
@ -145,8 +152,8 @@ func (registry *mongodbSensorRegistry) DeleteSensor(sensorId uuid.UUID) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *mongodbSensorRegistry) UpdateSensor(sensor *WeatherSensor) error {
|
func (registry *mongodbSensorRegistry) UpdateSensor(sensor *WeatherSensor) error {
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
res, err := registry.sensorCollection.ReplaceOne(
|
||||||
res, err := registry.sensorCollection.ReplaceOne(ctx,
|
context.Background(),
|
||||||
bson.M{"id": sensor.Id},
|
bson.M{"id": sensor.Id},
|
||||||
sensor)
|
sensor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -160,7 +167,6 @@ func (registry *mongodbSensorRegistry) UpdateSensor(sensor *WeatherSensor) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *mongodbSensorRegistry) Close() error {
|
func (registry *mongodbSensorRegistry) Close() error {
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
err := registry.client.Disconnect(context.Background())
|
||||||
err := registry.client.Disconnect(ctx)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ type SensorRegistry interface {
|
||||||
DeleteSensor(uuid.UUID) error
|
DeleteSensor(uuid.UUID) error
|
||||||
UpdateSensor(*WeatherSensor) error
|
UpdateSensor(*WeatherSensor) error
|
||||||
GetSensors() ([]*WeatherSensor, error)
|
GetSensors() ([]*WeatherSensor, error)
|
||||||
|
GetSensorsOfUser(userId string) ([]*WeatherSensor, error)
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +18,7 @@ type SensorRegistry interface {
|
||||||
type WeatherSensor struct {
|
type WeatherSensor struct {
|
||||||
Name string
|
Name string
|
||||||
Id uuid.UUID
|
Id uuid.UUID
|
||||||
|
UserId string
|
||||||
Location string
|
Location string
|
||||||
Longitude float64
|
Longitude float64
|
||||||
Latitude float64
|
Latitude float64
|
||||||
|
|
Loading…
Add table
Reference in a new issue