From 1caac72771cbd5b0cbb872bd07497c1c1322ab37 Mon Sep 17 00:00:00 2001 From: Fritz Heiden Date: Thu, 20 Mar 2025 19:22:20 +0100 Subject: [PATCH] feat: add delete integration endpoint --- data/device_database.go | 5 +++ management/device_manager.go | 83 +++++++++++++++++++----------------- server/device_api_handler.go | 14 ++++++ 3 files changed, 63 insertions(+), 39 deletions(-) diff --git a/data/device_database.go b/data/device_database.go index 3faae0a..4d562b4 100644 --- a/data/device_database.go +++ b/data/device_database.go @@ -154,6 +154,11 @@ func (db *DeviceDatabase) GetIntegrations() ([]Integration, error) { return integrations, nil } +func (db *DeviceDatabase) DeleteIntegration(id string) error { + _, err := db.Connection.Exec("DELETE FROM integrations WHERE id = ?", id) + return err +} + func (db *DeviceDatabase) IntegrationNameExists(name string) (bool, error) { var exists bool err := db.Connection.QueryRow("SELECT EXISTS(SELECT 1 FROM integrations WHERE name = ?)", name).Scan(&exists) diff --git a/management/device_manager.go b/management/device_manager.go index 6059d0c..ab50636 100644 --- a/management/device_manager.go +++ b/management/device_manager.go @@ -13,35 +13,35 @@ type DeviceManager struct { registrationCodes []string } -func (um *DeviceManager) Initialize(deviceDatabase *d.DeviceDatabase) error { - um.deviceDatabase = deviceDatabase +func (dm *DeviceManager) Initialize(deviceDatabase *d.DeviceDatabase) error { + dm.deviceDatabase = deviceDatabase return nil } -func (um *DeviceManager) CreateDevice(device *d.PlaybackDevice) (string, error) { - id, error := um.deviceDatabase.CreateDevice(device.Name, device.Description) +func (dm *DeviceManager) CreateDevice(device *d.PlaybackDevice) (string, error) { + id, error := dm.deviceDatabase.CreateDevice(device.Name, device.Description) if error != nil { return "", error } return id, nil } -func (um *DeviceManager) DeviceIdExists(id string) (bool, error) { - exists, error := um.deviceDatabase.DeviceIdExists(id) +func (dm *DeviceManager) DeviceIdExists(id string) (bool, error) { + exists, error := dm.deviceDatabase.DeviceIdExists(id) if error != nil { return false, error } return exists, nil } -//func (um *DeviceManager) Register(code string) (string, error) { -// device, error := um.GetDeviceByCode(code) +//func (dm *DeviceManager) Register(code string) (string, error) { +// device, error := dm.GetDeviceByCode(code) // if error != nil { // return "", error // } // // expiryDate := time.Now().AddDate(0, 0, 30) -// token, error := um.deviceDatabase.CreateSession(device.ID, expiryDate) +// token, error := dm.deviceDatabase.CreateSession(device.ID, expiryDate) // if error != nil { // return "", error // } @@ -49,52 +49,52 @@ func (um *DeviceManager) DeviceIdExists(id string) (bool, error) { // return token, nil //} -func (um *DeviceManager) GetSession(sessionToken string) (*d.DeviceSession, error) { - session, error := um.deviceDatabase.GetSession(sessionToken) +func (dm *DeviceManager) GetSession(sessionToken string) (*d.DeviceSession, error) { + session, error := dm.deviceDatabase.GetSession(sessionToken) if error != nil { return nil, error } return session, nil } -func (um *DeviceManager) GetDeviceById(id string) (*d.PlaybackDevice, error) { - device, error := um.deviceDatabase.GetDeviceById(id) +func (dm *DeviceManager) GetDeviceById(id string) (*d.PlaybackDevice, error) { + device, error := dm.deviceDatabase.GetDeviceById(id) if error != nil { return nil, error } return device, nil } -func (um *DeviceManager) UpdateDevice(device *d.PlaybackDevice) error { - error := um.deviceDatabase.UpdateDevice(device) +func (dm *DeviceManager) UpdateDevice(device *d.PlaybackDevice) error { + error := dm.deviceDatabase.UpdateDevice(device) return error } -func (um *DeviceManager) DeleteSession(token string) error { - error := um.deviceDatabase.DeleteSessionByToken(token) +func (dm *DeviceManager) DeleteSession(token string) error { + error := dm.deviceDatabase.DeleteSessionByToken(token) if error != nil { return error } return nil } -func (um *DeviceManager) GetDevices() (*[]d.PlaybackDevice, error) { - users, error := um.deviceDatabase.GetDevices() +func (dm *DeviceManager) GetDevices() (*[]d.PlaybackDevice, error) { + users, error := dm.deviceDatabase.GetDevices() return users, error } -func (um *DeviceManager) DeleteDevice(ID string) error { - error := um.deviceDatabase.DeleteDevice(ID) +func (dm *DeviceManager) DeleteDevice(ID string) error { + error := dm.deviceDatabase.DeleteDevice(ID) return error } -func (um *DeviceManager) GetRegistrationCode() (string, error) { - code := um.createCode() +func (dm *DeviceManager) GetRegistrationCode() (string, error) { + code := dm.createCode() return code, nil } -func (um *DeviceManager) CreateIntegration(name, code string) (d.Integration, error) { - exists, err := um.deviceDatabase.IntegrationNameExists(name) +func (dm *DeviceManager) CreateIntegration(name, code string) (d.Integration, error) { + exists, err := dm.deviceDatabase.IntegrationNameExists(name) if err != nil { return d.Integration{}, err } @@ -102,7 +102,7 @@ func (um *DeviceManager) CreateIntegration(name, code string) (d.Integration, er return d.Integration{}, fmt.Errorf("Integration name already exists") } - if err := um.redeemCode(code); err != nil { + if err := dm.redeemCode(code); err != nil { return d.Integration{}, err } @@ -111,7 +111,7 @@ func (um *DeviceManager) CreateIntegration(name, code string) (d.Integration, er return d.Integration{}, err } - id, err := um.deviceDatabase.CreateIntegration(name, token) + id, err := dm.deviceDatabase.CreateIntegration(name, token) if err != nil { return d.Integration{}, err } @@ -125,24 +125,29 @@ func (um *DeviceManager) CreateIntegration(name, code string) (d.Integration, er return integration, nil } -func (um *DeviceManager) GetIntegration(id string) (*d.Integration, error) { - integration, err := um.deviceDatabase.GetIntegration(id) +func (dm *DeviceManager) GetIntegration(id string) (*d.Integration, error) { + integration, err := dm.deviceDatabase.GetIntegration(id) if err != nil { return nil, err } return integration, nil } -func (um *DeviceManager) GetIntegrations() ([]d.Integration, error) { - integrations, err := um.deviceDatabase.GetIntegrations() +func (dm *DeviceManager) GetIntegrations() ([]d.Integration, error) { + integrations, err := dm.deviceDatabase.GetIntegrations() if err != nil { return nil, err } return integrations, nil } -func (um *DeviceManager) checkCode(code string) bool { - for _, c := range um.registrationCodes { +func (dm *DeviceManager) DeleteIntegration(id string) error { + error := dm.deviceDatabase.DeleteIntegration(id) + return error +} + +func (dm *DeviceManager) checkCode(code string) bool { + for _, c := range dm.registrationCodes { if c == code { return true } @@ -150,20 +155,20 @@ func (um *DeviceManager) checkCode(code string) bool { return false } -func (um *DeviceManager) createCode() string { +func (dm *DeviceManager) createCode() string { code := gonanoid.MustGenerate("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 6) - um.registrationCodes = append(um.registrationCodes, code) + dm.registrationCodes = append(dm.registrationCodes, code) return code } -func (um *DeviceManager) redeemCode(code string) error { - if !um.checkCode(code) { +func (dm *DeviceManager) redeemCode(code string) error { + if !dm.checkCode(code) { return fmt.Errorf("Invalid registration code") } - for i, c := range um.registrationCodes { + for i, c := range dm.registrationCodes { if c == code { - um.registrationCodes = slices.Delete(um.registrationCodes, i, i+1) + dm.registrationCodes = slices.Delete(dm.registrationCodes, i, i+1) break } } diff --git a/server/device_api_handler.go b/server/device_api_handler.go index 20a7b7f..d356f86 100644 --- a/server/device_api_handler.go +++ b/server/device_api_handler.go @@ -28,6 +28,7 @@ func (r *DeviceApiHandler) Initialize(authenticator *Authenticator) { integrationsApi.POST("", r.handleCreateIntegration) integrationsApi.GET("", r.handleGetIntegrations) integrationsApi.GET("/:id", r.handleGetIntegration) + integrationsApi.DELETE("/:id", r.handleDeleteIntegration) } func (r *DeviceApiHandler) handleIntegrationRegistration(context echo.Context) error { @@ -111,6 +112,19 @@ func (r *DeviceApiHandler) handleGetIntegrations(context echo.Context) error { return context.JSON(200, integrations) } +func (r *DeviceApiHandler) handleDeleteIntegration(context echo.Context) error { + id := context.Param("id") + + error := r.deviceManager.DeleteIntegration(id) + + if error != nil { + SendError(500, context, fmt.Sprintf("Failed to delete integration: %s", error)) + return error + } + + return context.JSON(200, "") +} + //func (r DeviceApiHandler) handleRegister(context echo.Context) error { // var registrationData struct { // Code string `json:"code"`