From 4f4438b4b2cbd98f01ae0c58a5b32deb7d52d0a8 Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Wed, 28 Sep 2022 14:09:39 -0700 Subject: [PATCH] add device api endpoints --- api/handlers/api.go | 7 +++++++ api/handlers/device.go | 8 ++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/api/handlers/api.go b/api/handlers/api.go index d38b9eb..b845668 100644 --- a/api/handlers/api.go +++ b/api/handlers/api.go @@ -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 } diff --git a/api/handlers/device.go b/api/handlers/device.go index 65ea613..eeadd58 100644 --- a/api/handlers/device.go +++ b/api/handlers/device.go @@ -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