improved sensor-handling
This commit is contained in:
parent
a230cfd30e
commit
f623f76c1a
4 changed files with 38 additions and 54 deletions
|
@ -80,10 +80,10 @@ func (api *weatherRestApi) handleRequests() *mux.Router {
|
|||
sensorRouter.HandleFunc("/{id}/{_dummy:(?i)weather-data}", api.addWeatherDataHandler).Methods("POST")
|
||||
|
||||
sensorRouter.HandleFunc("", api.getAllWeatherSensorHandler).Methods("GET")
|
||||
sensorRouter.HandleFunc("", api.registerWeatherSensorHandler).Methods("POST")
|
||||
sensorRouter.HandleFunc("/{id}", api.getWeatherSensorHandler).Methods("GET")
|
||||
sensorRouter.HandleFunc("/{id}", api.updateWeatherSensorHandler).Methods("PUT")
|
||||
sensorRouter.HandleFunc("/{id}", api.deleteWeatherSensorHandler).Methods("DELETE")
|
||||
sensorRouter.HandleFunc("/{name}", api.registerWeatherSensorHandler).Methods("POST")
|
||||
|
||||
return router
|
||||
}
|
||||
|
@ -165,17 +165,18 @@ func (api *weatherRestApi) addWeatherDataHandler(w http.ResponseWriter, r *http.
|
|||
}
|
||||
|
||||
func (api *weatherRestApi) registerWeatherSensorHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
name := vars["name"]
|
||||
var err error
|
||||
|
||||
sensor, err := api.sensorRegistry.RegisterSensorByName(name)
|
||||
sensor := new(storage.WeatherSensor)
|
||||
sensor.UserId = r.Header.Get(userIdHeader)
|
||||
|
||||
err = json.NewDecoder(r.Body).Decode(sensor)
|
||||
if err != nil {
|
||||
http.Error(w, "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
sensor.UserId = r.Header.Get(userIdHeader)
|
||||
err = api.sensorRegistry.UpdateSensor(sensor)
|
||||
sensor, err = api.sensorRegistry.RegisterSensor(sensor)
|
||||
if err != nil {
|
||||
http.Error(w, "", http.StatusBadRequest)
|
||||
return
|
||||
|
@ -211,7 +212,7 @@ func (api *weatherRestApi) getWeatherSensorHandler(w http.ResponseWriter, r *htt
|
|||
return
|
||||
}
|
||||
|
||||
weatherSensor, err := api.sensorRegistry.ResolveSensorById(sensorId)
|
||||
weatherSensor, err := api.sensorRegistry.GetSensor(sensorId)
|
||||
if err != nil {
|
||||
http.Error(w, "", http.StatusNotFound)
|
||||
return
|
||||
|
@ -232,23 +233,21 @@ func (api *weatherRestApi) updateWeatherSensorHandler(w http.ResponseWriter, r *
|
|||
return
|
||||
}
|
||||
|
||||
var sensor storage.WeatherSensor
|
||||
err = json.NewDecoder(r.Body).Decode(&sensor)
|
||||
sensor, err := api.sensorRegistry.GetSensor(sensorId)
|
||||
if err != nil {
|
||||
http.Error(w, "", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewDecoder(r.Body).Decode(sensor)
|
||||
if err != nil {
|
||||
http.Error(w, "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
sensor.UserId = r.Header.Get(userIdHeader)
|
||||
sensor.Id = sensorId
|
||||
|
||||
exist, err := api.sensorRegistry.ExistSensor(sensorId)
|
||||
if !exist || err != nil {
|
||||
http.Error(w, "", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
err = api.sensorRegistry.UpdateSensor(&sensor)
|
||||
err = api.sensorRegistry.UpdateSensor(sensor)
|
||||
if err != nil {
|
||||
http.Error(w, "", http.StatusBadRequest)
|
||||
return
|
||||
|
@ -303,12 +302,12 @@ func (api *weatherRestApi) IsAuthorized(next http.Handler) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
_, err = api.parseToken(r.Header)
|
||||
claims, err := api.parseToken(r.Header)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
r.Header.Set(userIdHeader, claims.Uid)
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
|
@ -335,11 +334,6 @@ func (api *weatherRestApi) parseToken(header http.Header) (*UserClaims, error) {
|
|||
return []byte(api.config.JwtTokenSecret), nil
|
||||
},
|
||||
)
|
||||
|
||||
if err == nil {
|
||||
header.Add(userIdHeader, claims.Uid)
|
||||
}
|
||||
|
||||
return claims, err
|
||||
}
|
||||
|
||||
|
|
2
main.go
2
main.go
|
@ -50,7 +50,7 @@ func main() {
|
|||
}
|
||||
|
||||
func handleNewWeatherData(wd storage.WeatherData) {
|
||||
_, err := sensorRegistry.ResolveSensorById(wd.SensorId)
|
||||
_, err := sensorRegistry.GetSensor(wd.SensorId)
|
||||
if config.AllowUnregisteredSensors || err == nil {
|
||||
weatherStorage.Save(wd)
|
||||
}
|
||||
|
|
|
@ -32,13 +32,13 @@ func NewMongodbSensorRegistry(mongoCfg config.MongoConfig) (*mongodbSensorRegist
|
|||
|
||||
err = client.Connect(context.Background())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Print(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = client.Ping(context.Background(), readpref.Primary())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Print(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -50,19 +50,9 @@ func NewMongodbSensorRegistry(mongoCfg config.MongoConfig) (*mongodbSensorRegist
|
|||
return sensorRegistry, nil
|
||||
}
|
||||
|
||||
func (registry *mongodbSensorRegistry) RegisterSensorByName(name string) (*WeatherSensor, error) {
|
||||
exist, err := registry.ExistSensorName(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if exist {
|
||||
return nil, errors.New("sensorname already exists")
|
||||
}
|
||||
sensor := new(WeatherSensor)
|
||||
sensor.Name = name
|
||||
func (registry *mongodbSensorRegistry) RegisterSensor(sensor *WeatherSensor) (*WeatherSensor, error) {
|
||||
sensor.Id = uuid.New()
|
||||
|
||||
_, err = registry.sensorCollection.InsertOne(context.Background(), sensor)
|
||||
_, err := registry.sensorCollection.InsertOne(context.Background(), sensor)
|
||||
|
||||
return sensor, err
|
||||
}
|
||||
|
@ -70,17 +60,17 @@ func (registry *mongodbSensorRegistry) RegisterSensorByName(name string) (*Weath
|
|||
func (registry *mongodbSensorRegistry) ExistSensorName(name string) (bool, error) {
|
||||
cursor, err := registry.sensorCollection.Find(context.Background(), bson.M{"name": name})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Print(err)
|
||||
return false, err
|
||||
}
|
||||
|
||||
return cursor.Next(context.Background()), nil
|
||||
}
|
||||
|
||||
func (registry *mongodbSensorRegistry) ResolveSensorById(sensorId uuid.UUID) (*WeatherSensor, error) {
|
||||
func (registry *mongodbSensorRegistry) GetSensor(sensorId uuid.UUID) (*WeatherSensor, error) {
|
||||
cursor, err := registry.sensorCollection.Find(context.Background(), bson.M{"id": sensorId})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Print(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -90,7 +80,7 @@ func (registry *mongodbSensorRegistry) ResolveSensorById(sensorId uuid.UUID) (*W
|
|||
|
||||
var sensor *WeatherSensor
|
||||
if err = cursor.Decode(&sensor); err != nil {
|
||||
log.Fatal(err)
|
||||
log.Print(err)
|
||||
return nil, err
|
||||
}
|
||||
return sensor, nil
|
||||
|
@ -99,7 +89,7 @@ func (registry *mongodbSensorRegistry) ResolveSensorById(sensorId uuid.UUID) (*W
|
|||
func (registry *mongodbSensorRegistry) ExistSensor(sensorId uuid.UUID) (bool, error) {
|
||||
cursor, err := registry.sensorCollection.Find(context.Background(), bson.M{"id": sensorId})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Print(err)
|
||||
return false, err
|
||||
}
|
||||
|
||||
|
@ -109,13 +99,13 @@ func (registry *mongodbSensorRegistry) ExistSensor(sensorId uuid.UUID) (bool, er
|
|||
func (registry *mongodbSensorRegistry) GetSensors() ([]*WeatherSensor, error) {
|
||||
cursor, err := registry.sensorCollection.Find(context.Background(), bson.M{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Print(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var readData []*WeatherSensor = make([]*WeatherSensor, 0)
|
||||
if err = cursor.All(context.Background(), &readData); err != nil {
|
||||
log.Fatal(err)
|
||||
log.Print(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -125,13 +115,13 @@ func (registry *mongodbSensorRegistry) GetSensors() ([]*WeatherSensor, error) {
|
|||
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)
|
||||
log.Print(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var readData []*WeatherSensor = make([]*WeatherSensor, 0)
|
||||
if err = cursor.All(context.Background(), &readData); err != nil {
|
||||
log.Fatal(err)
|
||||
log.Print(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -141,7 +131,7 @@ func (registry *mongodbSensorRegistry) GetSensorsOfUser(userId string) ([]*Weath
|
|||
func (registry *mongodbSensorRegistry) DeleteSensor(sensorId uuid.UUID) error {
|
||||
res, err := registry.sensorCollection.DeleteOne(context.Background(), bson.M{"id": sensorId})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Print(err)
|
||||
return err
|
||||
}
|
||||
if res.DeletedCount == 0 {
|
||||
|
@ -157,7 +147,7 @@ func (registry *mongodbSensorRegistry) UpdateSensor(sensor *WeatherSensor) error
|
|||
bson.M{"id": sensor.Id},
|
||||
sensor)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Print(err)
|
||||
return err
|
||||
}
|
||||
if res.MatchedCount == 0 || res.ModifiedCount == 0 {
|
||||
|
|
|
@ -3,14 +3,14 @@ package storage
|
|||
import "github.com/google/uuid"
|
||||
|
||||
type SensorRegistry interface {
|
||||
RegisterSensorByName(string) (*WeatherSensor, error)
|
||||
RegisterSensor(sensor *WeatherSensor) (*WeatherSensor, error)
|
||||
ExistSensor(sensorId uuid.UUID) (bool, error)
|
||||
ExistSensorName(name string) (bool, error)
|
||||
ResolveSensorById(uuid.UUID) (*WeatherSensor, error)
|
||||
DeleteSensor(uuid.UUID) error
|
||||
UpdateSensor(*WeatherSensor) error
|
||||
GetSensor(uuid.UUID) (*WeatherSensor, error)
|
||||
GetSensors() ([]*WeatherSensor, error)
|
||||
GetSensorsOfUser(userId string) ([]*WeatherSensor, error)
|
||||
UpdateSensor(*WeatherSensor) error
|
||||
DeleteSensor(uuid.UUID) error
|
||||
Close() error
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue