feat: add get integration list

This commit is contained in:
Fritz Heiden 2025-03-19 14:40:36 +01:00
parent 54a2b7418a
commit e526bb8122
7 changed files with 104 additions and 3 deletions

View File

@ -105,7 +105,7 @@ func (db *DeviceDatabase) DeleteDevice(ID string) error {
} }
func (db *DeviceDatabase) CreateIntegration(name, token string) (string, error) { func (db *DeviceDatabase) CreateIntegration(name, token string) (string, error) {
id, err := gonanoid.Nanoid(10) id, err := gonanoid.Nanoid(8)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -122,6 +122,29 @@ func (db *DeviceDatabase) CreateIntegration(name, token string) (string, error)
return id, nil 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) { func (db *DeviceDatabase) IntegrationNameExists(name string) (bool, error) {
var exists bool var exists bool
err := db.Connection.QueryRow("SELECT EXISTS(SELECT 1 FROM integrations WHERE name = ?)", name).Scan(&exists) err := db.Connection.QueryRow("SELECT EXISTS(SELECT 1 FROM integrations WHERE name = ?)", name).Scan(&exists)

View File

@ -125,6 +125,14 @@ func (um *DeviceManager) CreateIntegration(name, code string) (d.Integration, er
return integration, nil 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 { func (um *DeviceManager) checkCode(code string) bool {
for _, c := range um.registrationCodes { for _, c := range um.registrationCodes {
if c == code { if c == code {

View File

@ -26,6 +26,7 @@ func (r *DeviceApiHandler) Initialize(authenticator *Authenticator) {
integrationsApi := r.router.Group("/api/integrations") integrationsApi := r.router.Group("/api/integrations")
integrationsApi.GET("/register", r.handleIntegrationRegistration) integrationsApi.GET("/register", r.handleIntegrationRegistration)
integrationsApi.POST("", r.handleCreateIntegration) integrationsApi.POST("", r.handleCreateIntegration)
integrationsApi.GET("", r.handleGetIntegrations)
} }
func (r *DeviceApiHandler) handleIntegrationRegistration(context echo.Context) error { 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) 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 { //func (r DeviceApiHandler) handleRegister(context echo.Context) error {
// var registrationData struct { // var registrationData struct {
// Code string `json:"code"` // Code string `json:"code"`

View File

@ -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;

View File

@ -1,5 +1,6 @@
import PlaybackDevice from "./playback-device.js"; import PlaybackDevice from "./playback-device.js";
import User from "./user.js"; import User from "./user.js";
import Integration from "./integration.js";
const Serializer = (function () { const Serializer = (function () {
function deserializeUser(object) { function deserializeUser(object) {
@ -19,11 +20,21 @@ const Serializer = (function () {
return objects.map((object) => deserializeDevice(object)); return objects.map((object) => deserializeDevice(object));
} }
function deserializeIntegration(object) {
return new Integration(object);
}
function deserializeIntegrations(objects) {
return objects.map((object) => deserializeIntegration(object));
}
return { return {
deserializeUser, deserializeUser,
deserializeUsers, deserializeUsers,
deserializeDevice, deserializeDevice,
deserializeDevices, deserializeDevices,
deserializeIntegration,
deserializeIntegrations,
}; };
})(); })();

View File

@ -78,12 +78,29 @@ function DeviceService() {
return codeObject.code; 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 { return {
getDevices, getDevices,
createDevice, createDevice,
updateDevice, updateDevice,
deleteDevice, deleteDevice,
getRegistrationCode, getRegistrationCode,
getIntegrations,
}; };
} }

View File

@ -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 List from "../modules/list";
import CreateDeviceModal from "../modals/create-device-modal"; import CreateDeviceModal from "../modals/create-device-modal";
@ -11,6 +11,7 @@ function DevicesView(props) {
const [listType, setListType] = createSignal(DEVICES_LIST_TYPE); const [listType, setListType] = createSignal(DEVICES_LIST_TYPE);
const [devices, setDevices] = createSignal([]); const [devices, setDevices] = createSignal([]);
const [integrations] = createResource(DeviceService.getIntegrations);
CreateDeviceModal.onDeviceCreated(() => { CreateDeviceModal.onDeviceCreated(() => {
handleRefreshDevices(); handleRefreshDevices();
@ -119,7 +120,7 @@ function DevicesView(props) {
<Show when={listType() === INTEGRATION_LIST_TYPE}> <Show when={listType() === INTEGRATION_LIST_TYPE}>
<List <List
onListItemClick={() => {}} onListItemClick={() => {}}
items={[].map((integration) => ({ items={(integrations() || []).map((integration) => ({
id: { id: {
html: <span class="font-monospace">{integration.getId()}</span>, html: <span class="font-monospace">{integration.getId()}</span>,
}, },