diff --git a/data/device_database.go b/data/device_database.go index 4d562b4..09982a9 100644 --- a/data/device_database.go +++ b/data/device_database.go @@ -122,6 +122,15 @@ func (db *DeviceDatabase) CreateIntegration(name, token string) (string, error) return id, nil } +func (db *DeviceDatabase) IntegrationIdExists(id string) (bool, error) { + var exists bool + err := db.Connection.QueryRow("SELECT EXISTS(SELECT 1 FROM integrations WHERE id = ?)", id).Scan(&exists) + if err != nil { + return false, err + } + return exists, nil +} + func (db *DeviceDatabase) GetIntegration(id string) (*Integration, error) { var integration Integration err := db.Connection.QueryRow("SELECT id, name FROM integrations WHERE id = ?", id).Scan(&integration.ID, &integration.Name) diff --git a/management/device_manager.go b/management/device_manager.go index ab50636..dc4ec98 100644 --- a/management/device_manager.go +++ b/management/device_manager.go @@ -146,6 +146,14 @@ func (dm *DeviceManager) DeleteIntegration(id string) error { return error } +func (dm *DeviceManager) IntegrationIdExists(id string) (bool, error) { + exists, error := dm.deviceDatabase.IntegrationIdExists(id) + if error != nil { + return false, error + } + return exists, nil +} + func (dm *DeviceManager) checkCode(code string) bool { for _, c := range dm.registrationCodes { if c == code { diff --git a/server/device_api_handler.go b/server/device_api_handler.go index d356f86..e0bbc38 100644 --- a/server/device_api_handler.go +++ b/server/device_api_handler.go @@ -83,11 +83,21 @@ func (r *DeviceApiHandler) handleCreateIntegration(context echo.Context) error { func (r *DeviceApiHandler) handleGetIntegration(context echo.Context) error { id := context.Param("id") - integration, error := r.deviceManager.GetIntegration(id) + exists, err := r.deviceManager.IntegrationIdExists(id) + if err != nil { + SendError(500, context, fmt.Sprintf("Failed to get integration: %s", err)) + return err + } + if !exists { + SendError(404, context, fmt.Sprintf("integration with id %s does not exist", id)) + return err + } - if error != nil { - SendError(500, context, fmt.Sprintf("Failed to get integration: %s", error)) - return error + integration, err := r.deviceManager.GetIntegration(id) + + if err != nil { + SendError(500, context, fmt.Sprintf("Failed to get integration: %s", err)) + return err } integrationData := struct {