add device api endpoints
This commit is contained in:
2 files changed
+11
-4
No files matched your search
@@ -29,6 +29,7 @@ type App struct {
|
||||
func (a *App) New() *mux.Router {
|
||||
r := mux.NewRouter()
|
||||
cow := Cow{DB: databases.NewCowDatabase(a.dbHelper)}
|
||||
device := Device{DB: databases.NewDeviceDatabase(a.dbHelper)}
|
||||
|
||||
// healthcheck
|
||||
r.HandleFunc("/health", healthCheckHandler)
|
||||
@@ -41,6 +42,12 @@ func (a *App) New() *mux.Router {
|
||||
apiCreate.Handle("/cows/new", api.Middleware(http.HandlerFunc(cow.NewCowHandler))).Methods("POST")
|
||||
apiCreate.Handle("/cows/update/{cow_id}", api.Middleware(http.HandlerFunc(cow.UpdateCowHandler))).Methods("POST") // By Object ID not CowCode
|
||||
|
||||
apiCreate.Handle("/device/{device_id}", api.Middleware(http.HandlerFunc(device.DeviceByIDHandler))).Methods("GET")
|
||||
apiCreate.Handle("/devices", api.Middleware(http.HandlerFunc(device.DeviceHandler))).Methods("GET")
|
||||
apiCreate.Handle("/devices", api.Middleware(http.HandlerFunc(device.DeviceHandlerQuery))).Methods("POST")
|
||||
apiCreate.Handle("/devices/new", api.Middleware(http.HandlerFunc(device.NewDeviceHandler))).Methods("POST")
|
||||
apiCreate.Handle("/devices/update/{device_id}", api.Middleware(http.HandlerFunc(device.UpdateDeviceHandler))).Methods("POST")
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ func (d Device) DeviceByIDHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// NewCowHandler inserts a new cow into the collection and returns a result and error
|
||||
func (c Cow) NewDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (d Device) NewDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
var deviceDetails models.DeviceDetails // Json data will represent the cow details model
|
||||
defer cancel()
|
||||
@@ -119,7 +119,7 @@ func (c Cow) NewDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
Details: deviceDetails,
|
||||
}
|
||||
|
||||
result, err := c.DB.InsertOne(ctx, newDevice)
|
||||
result, err := d.DB.InsertOne(ctx, newDevice)
|
||||
if err != nil {
|
||||
config.ErrorStatus("failed to insert device", http.StatusBadRequest, w, err)
|
||||
return
|
||||
@@ -131,7 +131,7 @@ func (c Cow) NewDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// UpdateCowHandler gets updates the data for an existing cow and returns a result and error
|
||||
func (c Cow) UpdateDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (d Device) UpdateDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
deviceID := mux.Vars(r)["cow_id"]
|
||||
|
||||
// TODO: Collect data to update from passed json
|
||||
@@ -141,7 +141,7 @@ func (c Cow) UpdateDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
},
|
||||
}
|
||||
|
||||
dbResp, err := c.DB.UpdateOne(context.Background(), bson.M{"_id": deviceID}, update)
|
||||
dbResp, err := d.DB.UpdateOne(context.Background(), bson.M{"_id": deviceID}, update)
|
||||
if err != nil {
|
||||
config.ErrorStatus("the device could not be updated", http.StatusNotFound, w, err)
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user