Skip to content

Commit

Permalink
FIX honeynet#92 , Added /sensor route for Get and POST request .
Browse files Browse the repository at this point in the history
Added /Sensor route as GET request to list all sensors stored by owner id . POST request for adding sensor under current user id as owner id .
  • Loading branch information
Tushar-kalsi committed Nov 29, 2023
1 parent 930eeb8 commit c01f694
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion backend/entities/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package entities
type Sensor struct {
Id string `json:"id"`
Name string `json:"name"`
OwnerId string `json:"ownerid"`
OwnerId string `json:"ownerid,omitempty"`
}
29 changes: 29 additions & 0 deletions backend/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,32 @@ func (cs *server) getSensorsByUser(w http.ResponseWriter, r *http.Request, p htt
}

}

func (cs *server) addSensor(w http.ResponseWriter, r *http.Request, p httprouter.Params){


userId := userIDFromCtx(r.Context())

decoder := json.NewDecoder(r.Body)
defer r.Body.Close()
var sensor entities.Sensor
err := decoder.Decode(&sensor)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return

}

err = cs.sensorRepo.AddSensors(sensor, userId)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return

}

w.WriteHeader(http.StatusOK)

}

12 changes: 10 additions & 2 deletions backend/repos/sensors.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,16 @@ func (r *SensorRepo) GetSensorsByOwnerId(ownerId string) ([]entities.Sensor, err

}

func (r *SensorRepo) AddSensors(s entities.Sensor) error {
func (r *SensorRepo) AddSensors(sensor entities.Sensor , userId string) error {

r.addSensor.Exec()
s:=entities.Sensor{Id: sensor.Id , OwnerId: userId, Name:sensor.Name}

_, err:=r.addSensor.Exec(s)

if err != nil {
return err
}

return nil

}
1 change: 1 addition & 0 deletions backend/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func newRouter(cs *server) (*httprouter.Router, error) {

// sensor
r.GET("/sensors", handlers.CorsMiddleware(handlers.BearerMiddleware(cs.getSensorsByUser, os.Args[3])))
r.POST("/sensors", handlers.CorsMiddleware(handlers.BearerMiddleware(cs.addSensor, os.Args[3])))

return r, nil
}

0 comments on commit c01f694

Please sign in to comment.