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) {
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)

View File

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

View File

@ -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"`

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 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,
};
})();

View File

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

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 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) {
<Show when={listType() === INTEGRATION_LIST_TYPE}>
<List
onListItemClick={() => {}}
items={[].map((integration) => ({
items={(integrations() || []).map((integration) => ({
id: {
html: <span class="font-monospace">{integration.getId()}</span>,
},