From e526bb812224a1bd5fa1b3384c21e2fe67b75f60 Mon Sep 17 00:00:00 2001 From: Fritz Heiden Date: Wed, 19 Mar 2025 14:40:36 +0100 Subject: [PATCH] feat: add get integration list --- data/device_database.go | 25 ++++++++++++++++++++++++- management/device_manager.go | 8 ++++++++ server/device_api_handler.go | 12 ++++++++++++ www/src/data/integration.js | 29 +++++++++++++++++++++++++++++ www/src/data/serializer.js | 11 +++++++++++ www/src/services/device-service.js | 17 +++++++++++++++++ www/src/views/devices-view.jsx | 5 +++-- 7 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 www/src/data/integration.js diff --git a/data/device_database.go b/data/device_database.go index a46596b..940f713 100644 --- a/data/device_database.go +++ b/data/device_database.go @@ -105,7 +105,7 @@ func (db *DeviceDatabase) DeleteDevice(ID string) error { } func (db *DeviceDatabase) CreateIntegration(name, token string) (string, error) { - id, err := gonanoid.Nanoid(10) + id, err := gonanoid.Nanoid(8) if err != nil { return "", err } @@ -122,6 +122,29 @@ func (db *DeviceDatabase) CreateIntegration(name, token string) (string, error) return id, nil } +func (db *DeviceDatabase) GetIntegrations() ([]Integration, error) { + var integrations []Integration + + rows, err := db.Connection.Query("SELECT id, name FROM integrations") + if err != nil { + return nil, err + } + defer rows.Close() + + for rows.Next() { + var integration Integration + err := rows.Scan(&integration.ID, &integration.Name) + if err != nil { + if err == sql.ErrNoRows { + return nil, nil + } + return nil, err + } + integrations = append(integrations, integration) + } + return integrations, nil +} + 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 55890cd..49b2d18 100644 --- a/management/device_manager.go +++ b/management/device_manager.go @@ -125,6 +125,14 @@ func (um *DeviceManager) CreateIntegration(name, code string) (d.Integration, er return integration, nil } +func (um *DeviceManager) GetIntegrations() ([]d.Integration, error) { + integrations, err := um.deviceDatabase.GetIntegrations() + if err != nil { + return nil, err + } + return integrations, nil +} + func (um *DeviceManager) checkCode(code string) bool { for _, c := range um.registrationCodes { if c == code { diff --git a/server/device_api_handler.go b/server/device_api_handler.go index f685201..aa259ed 100644 --- a/server/device_api_handler.go +++ b/server/device_api_handler.go @@ -26,6 +26,7 @@ func (r *DeviceApiHandler) Initialize(authenticator *Authenticator) { integrationsApi := r.router.Group("/api/integrations") integrationsApi.GET("/register", r.handleIntegrationRegistration) integrationsApi.POST("", r.handleCreateIntegration) + integrationsApi.GET("", r.handleGetIntegrations) } func (r *DeviceApiHandler) handleIntegrationRegistration(context echo.Context) error { @@ -67,6 +68,17 @@ func (r *DeviceApiHandler) handleCreateIntegration(context echo.Context) error { return context.JSON(200, integration) } +func (r *DeviceApiHandler) handleGetIntegrations(context echo.Context) error { + integrations, error := r.deviceManager.GetIntegrations() + + if error != nil { + SendError(500, context, fmt.Sprintf("Failed to get integrations: %s", error)) + return error + } + + return context.JSON(200, integrations) +} + //func (r DeviceApiHandler) handleRegister(context echo.Context) error { // var registrationData struct { // Code string `json:"code"` diff --git a/www/src/data/integration.js b/www/src/data/integration.js new file mode 100644 index 0000000..c5345a8 --- /dev/null +++ b/www/src/data/integration.js @@ -0,0 +1,29 @@ +function Integration({ id, name }) { + let _id = id; + let _name = name; + + function getId() { + return _id; + } + + function setId(id) { + _id = id; + } + + function getName() { + return _name; + } + + function setName(name) { + _name = name; + } + + return { + getId, + setId, + getName, + setName, + }; +} + +export default Integration; \ No newline at end of file diff --git a/www/src/data/serializer.js b/www/src/data/serializer.js index b33e776..85dafe3 100644 --- a/www/src/data/serializer.js +++ b/www/src/data/serializer.js @@ -1,5 +1,6 @@ import PlaybackDevice from "./playback-device.js"; import User from "./user.js"; +import Integration from "./integration.js"; const Serializer = (function () { function deserializeUser(object) { @@ -19,11 +20,21 @@ const Serializer = (function () { return objects.map((object) => deserializeDevice(object)); } + function deserializeIntegration(object) { + return new Integration(object); + } + + function deserializeIntegrations(objects) { + return objects.map((object) => deserializeIntegration(object)); + } + return { deserializeUser, deserializeUsers, deserializeDevice, deserializeDevices, + deserializeIntegration, + deserializeIntegrations, }; })(); diff --git a/www/src/services/device-service.js b/www/src/services/device-service.js index c6b64d5..fdfbae1 100644 --- a/www/src/services/device-service.js +++ b/www/src/services/device-service.js @@ -78,12 +78,29 @@ function DeviceService() { return codeObject.code; } + async function getIntegrations() { + let response = await Net.sendRequest({ + method: "GET", + url: "/api/integrations", + }); + + if (response.status !== 200) { + let responseData = JSON.parse(response.data); + throw new Error(responseData.error); + } + + let integrations = JSON.parse(response.data); + integrations = Serializer.deserializeIntegrations(integrations); + return integrations; + } + return { getDevices, createDevice, updateDevice, deleteDevice, getRegistrationCode, + getIntegrations, }; } diff --git a/www/src/views/devices-view.jsx b/www/src/views/devices-view.jsx index 0f7b108..0e7c45e 100644 --- a/www/src/views/devices-view.jsx +++ b/www/src/views/devices-view.jsx @@ -1,4 +1,4 @@ -import { createSignal, onMount, Show } from "solid-js"; +import { createResource, createSignal, onMount, Show } from "solid-js"; import List from "../modules/list"; import CreateDeviceModal from "../modals/create-device-modal"; @@ -11,6 +11,7 @@ function DevicesView(props) { const [listType, setListType] = createSignal(DEVICES_LIST_TYPE); const [devices, setDevices] = createSignal([]); + const [integrations] = createResource(DeviceService.getIntegrations); CreateDeviceModal.onDeviceCreated(() => { handleRefreshDevices(); @@ -119,7 +120,7 @@ function DevicesView(props) { {}} - items={[].map((integration) => ({ + items={(integrations() || []).map((integration) => ({ id: { html: {integration.getId()}, },