mirror of
https://github.com/sakaljurgis/kodi-api-server.git
synced 2026-07-08 20:37:41 +00:00
add alias edit
This commit is contained in:
@@ -8,6 +8,11 @@
|
|||||||
"include": "KodiApiInterface/View/",
|
"include": "KodiApiInterface/View/",
|
||||||
"outDir": "dist/src",
|
"outDir": "dist/src",
|
||||||
"watchAssets": true
|
"watchAssets": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"include": "AliasesModule/View/",
|
||||||
|
"outDir": "dist/src",
|
||||||
|
"watchAssets": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,109 @@
|
|||||||
|
<html>
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<link rel="stylesheet"
|
||||||
|
href="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/css/jsoneditor.min.css" />
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css"
|
||||||
|
integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
|
||||||
|
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<div>
|
||||||
|
<div id="editor"></div>
|
||||||
|
</div>
|
||||||
|
<hr />
|
||||||
|
<div id="button">
|
||||||
|
<button class="btn btn-dark" onclick="handleClick()">OK</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"
|
||||||
|
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
|
||||||
|
crossorigin="anonymous"></script>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"
|
||||||
|
integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct"
|
||||||
|
crossorigin="anonymous"></script>
|
||||||
|
<script>
|
||||||
|
const element = document.getElementById("editor");
|
||||||
|
|
||||||
|
function tuplesToObj(arrOfTuples) {
|
||||||
|
const ret = [];
|
||||||
|
arrOfTuples.forEach(element => {
|
||||||
|
ret.push({ alias: element[0], title: element[1] });
|
||||||
|
});
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
function objToTuples(arrOfObjs) {
|
||||||
|
const ret = [];
|
||||||
|
arrOfObjs.forEach(element => {
|
||||||
|
ret.push([element.alias, element.title]);
|
||||||
|
});
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
const editor = new JSONEditor(element, {
|
||||||
|
ajax: true,
|
||||||
|
schema: { "$ref": "/aliases/schema" },
|
||||||
|
theme: "bootstrap4"
|
||||||
|
});
|
||||||
|
|
||||||
|
editor.on("ready", () => {
|
||||||
|
editor.validate();
|
||||||
|
fetch("/aliases/data").then((response) => {
|
||||||
|
return response.json();
|
||||||
|
}).then((data) => {
|
||||||
|
editor.setValue(tuplesToObj(data));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function handleClick() {
|
||||||
|
const tuples = objToTuples(editor.getValue());
|
||||||
|
console.log(tuples);
|
||||||
|
fetch("", {
|
||||||
|
body: JSON.stringify(tuples),
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then((response) => {
|
||||||
|
return response.json();
|
||||||
|
}).then((data) => {
|
||||||
|
if (data.response && data.response.statusCode) {
|
||||||
|
// workaround for success message actually being failure see HttpExceptionFilter
|
||||||
|
addAlert(false, data.notification ?? data.response.message);
|
||||||
|
} else {
|
||||||
|
editor.setValue(tuplesToObj(data));
|
||||||
|
addAlert(true);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
addAlert(false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function addAlert(success = true, msg = "") {
|
||||||
|
const html = success ?
|
||||||
|
`<div class="alert alert-success alert-dismissible" role="alert">
|
||||||
|
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||||
|
<strong>Success!</strong> saved. ${msg}
|
||||||
|
</div>
|
||||||
|
` :
|
||||||
|
`
|
||||||
|
<div class="alert alert-danger alert-dismissible" role="alert">
|
||||||
|
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||||
|
<strong>Failed!</strong> not saved. ${msg}
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
$("#button").append(html);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import { Body, Controller, Get, Post, Res } from '@nestjs/common';
|
||||||
|
import { JsonFileDataStorageService } from '../DataStorage/json-file-data-storage.service';
|
||||||
|
import { createReadStream } from 'fs';
|
||||||
|
import { join } from 'path';
|
||||||
|
import { Response } from 'express';
|
||||||
|
|
||||||
|
@Controller('aliases')
|
||||||
|
export class AliasesController {
|
||||||
|
constructor(private readonly jsonDs: JsonFileDataStorageService) {}
|
||||||
|
|
||||||
|
@Get('')
|
||||||
|
async main(@Res() res: Response) {
|
||||||
|
//todo - make the views directory global so no need to edit nest-cli.json every time
|
||||||
|
const readable = createReadStream(join(__dirname, './View/index.html'));
|
||||||
|
readable.pipe(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get('data')
|
||||||
|
async getData() {
|
||||||
|
return this.jsonDs.get('aliases', []);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post('')
|
||||||
|
async setData(@Body() data: any) {
|
||||||
|
await this.jsonDs.set('aliases', data);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get('schema')
|
||||||
|
getSchema() {
|
||||||
|
return {
|
||||||
|
title: 'Aliases',
|
||||||
|
type: 'array',
|
||||||
|
format: 'table',
|
||||||
|
uniqueItems: true,
|
||||||
|
items: {
|
||||||
|
type: 'object',
|
||||||
|
title: 'Alias',
|
||||||
|
properties: {
|
||||||
|
alias: {
|
||||||
|
type: 'string',
|
||||||
|
title: 'alias',
|
||||||
|
},
|
||||||
|
title: {
|
||||||
|
type: 'string',
|
||||||
|
title: 'title',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { AliasesController } from './aliases.cotroller';
|
||||||
|
import { DataStorageModule } from '../DataStorage/data-storage.module';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Playground module for playing around
|
||||||
|
*/
|
||||||
|
@Module({
|
||||||
|
imports: [DataStorageModule],
|
||||||
|
controllers: [AliasesController],
|
||||||
|
providers: [],
|
||||||
|
})
|
||||||
|
export class AliasesModule {}
|
||||||
@@ -11,12 +11,15 @@ import { RequestLogMiddleware } from './RequestLog/request-log.middleware';
|
|||||||
import { TryModule } from './TryOutModule/try.module';
|
import { TryModule } from './TryOutModule/try.module';
|
||||||
import { configService } from './config/config.service';
|
import { configService } from './config/config.service';
|
||||||
import { KodiApiInterfaceModule } from './KodiApiInterface/kodi-api-interface.module';
|
import { KodiApiInterfaceModule } from './KodiApiInterface/kodi-api-interface.module';
|
||||||
|
import { AliasesModule } from './AliasesModule/aliases.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
TryModule,
|
TryModule,
|
||||||
|
AliasesModule,
|
||||||
KodiApiModule,
|
KodiApiModule,
|
||||||
KodiApiInterfaceModule,
|
KodiApiInterfaceModule,
|
||||||
|
/** all routes must be before static module to be reachable */
|
||||||
StaticModule,
|
StaticModule,
|
||||||
TypeOrmModule.forRoot(configService.getTypeOrmConfig()),
|
TypeOrmModule.forRoot(configService.getTypeOrmConfig()),
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user