REST data from web site working. Admin panel area in plugin disabled for now. SQL schema added.

This commit is contained in:
Scott Duensing 2022-01-12 19:06:59 -06:00
parent cb34b340ae
commit 41a45260c4
11 changed files with 298 additions and 155 deletions

3
get-sql.sh Executable file
View file

@ -0,0 +1,3 @@
#!/bin/bash
mysqldump -d -h 192.168.0.3 -u dosThing -p dosThing > schema.sql

View file

@ -0,0 +1,15 @@
{
"name": "kangaroopunch/download-kirbytag",
"type": "kirby-plugin",
"version": "1.0",
"license": "GPL3",
"authors": [
{
"name": "Scott Duensing",
"email": "scott@kangaroopunch.com"
}
],
"require": {
"getkirby/composer-installer": "^1.2"
}
}

View file

@ -0,0 +1,15 @@
{
"name": "kangaroopunch/image-kirbytag",
"type": "kirby-plugin",
"version": "1.0",
"license": "GPL3",
"authors": [
{
"name": "Scott Duensing",
"email": "scott@kangaroopunch.com"
}
],
"require": {
"getkirby/composer-installer": "^1.2"
}
}

View file

@ -1,21 +1,21 @@
<?php
function kpApiConfigGetConfig(&$response) {
$response['config'] = collection('kwconfig')->filterBy('type', 'config');
$response['payload'] = collection('kwconfig');
$response['result'] = 'true';
$response['reason'] = 'Configuration entries returned.';
}
function kpApiConfigGetNumbers(&$response) {
$response['config'] = collection('kwconfig')->filterBy('type', 'number');
$response['payload'] = collection('kwnumbers');
$response['result'] = 'true';
$response['reason'] = 'Number entries returned.';
}
function kpApiConfigGetStrings(&$response) {
$response['config'] = collection('kwconfig')->filterBy('type', 'string');
$response['payload'] = collection('kwstrings');
$response['result'] = 'true';
$response['reason'] = 'String entries returned.';
}

View file

@ -2,6 +2,10 @@
namespace KangarooPunch;
use \PDO;
class KPunch {
public static function databaseGet() {

View file

@ -2,139 +2,87 @@
namespace KangarooPunch;
use Kirby\Data\Data;
use KangarooPunch\KPunch;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Toolkit\V;
class KwConfig
{
/**
* Creates a new entry with the given $input
* data and adds it to the json file
*
* @return bool
*/
public static function create(array $input): bool
{
// reuse the update method to create a new
// entry. If you need different logic
// here, you can easily extend it
return static::update($input);
class KwConfig {
public static function create(array $input): bool {
validate($input);
$db = KPunch::databaseGet();
KPunch::databaseQueryToJSON($db,
'INSERT INTO config (name, data, description) VALUES (?, ?, ?)',
array(
$input['name'],
$input['data'],
$input['description']
)
);
return true;
}
/**
* Deletes a entry by product id
*
* @return bool
*/
public static function delete(string $id): bool
{
// get all entry
$kwconfig = static::list();
// remove the entry from the list
unset($kwconfig[$id]);
// write the update list to the file
return Data::write(static::file(), $kwconfig);
public static function delete(string $id): bool {
$db = KPunch::databaseGet();
KPunch::databaseQueryToJSON($db, 'DELETE FROM config WHERE id = ?', array($id));
return true;
}
/**
* Returns the absolute path to the settings.json
* This is the place to modify if you don't want to
* store the entries in your plugin folder
* which you probably really don't want to do.
*
* @return string
*/
public static function file(): string
{
return __DIR__ . '/../settings.json';
}
/**
* Finds an entry by id and throws an exception
* if the entry cannot be found
*
* @param string $id
* @return array
*/
public static function find(string $id): array
{
$kwconfig = static::list()[$id] ?? null;
if (empty($kwconfig) === true) {
public static function find(string $id): array {
$db = KPunch::databaseGet();
$result = KPunch::databaseQueryToJSON($db, 'SELECT * FROM config WHERE id = ?', array($id));
if ($result == null) {
throw new NotFoundException('The entry could not be found');
}
return $kwconfig;
return json_decode($result);
}
/**
* Lists all entries from the settings.json
*
* @return array
*/
public static function list(): array
{
return Data::read(static::file());
public static function list(): array {
$db = KPunch::databaseGet();
return json_decode(KPunch::databaseQueryToJSON($db, 'SELECT * FROM config'));
}
/**
* Lists all available entry types
*
* @return array
*/
public static function types(): array
{
return [
'config',
'number',
'string'
];
public static function update(array $input): bool {
validate($input);
$db = KPunch::databaseGet();
KPunch::databaseQueryToJSON($db,
'UPDATE config SET name = ?, data = ?, description = ? WHERE id = ?',
array(
$input['name'],
$input['data'],
$input['description'],
$input['id']
)
);
return true;
}
/**
* Updates a entries by id with the given input
* It throws an exception in case of validation issues
*
* @param string $id
* @param array $input
* @return boolean
*/
public static function update(array $input): bool
{
$kwconfig = [
'title' => $input['title'] ?? null,
'type' => $input['type'] ?? null,
'description' => $input['description'] ?? null,
'data' => $input['data'] ?? null
];
// require a title
if (V::minlength($kwconfig['title'], 1) === false) {
throw new InvalidArgumentException('The title must not be empty');
private static function validate($input) {
if (V::minlength($input['name'], 1) === false) {
throw new InvalidArgumentException('The name must not be empty');
}
if (V::maxlength($input['name'], 32) === false) {
throw new InvalidArgumentException('The name must not be longer than 32 characters');
}
// make sure the title is not longer than expected
if (V::maxlength($kwconfig['title'], 255) === false) {
throw new InvalidArgumentException('The title must not be longer than 255 characters');
if (V::minlength($input['data'], 1) === false) {
throw new InvalidArgumentException('The data must not be empty');
}
if (V::maxlength($input['data'], 4096) === false) {
throw new InvalidArgumentException('The data must not be longer than 4096 characters');
}
// validate the category
if (V::in($kwconfig['type'], static::types()) === false) {
throw new InvalidArgumentException('Please select a valid category');
if (V::maxlength($input['description'], 255) === false) {
throw new InvalidArgumentException('The description must not be longer than 255 characters');
}
// load all products
$kwconfig = static::list();
// set/overwrite the data
$kwconfig[$id] = $kwconfig;
return Data::write(static::file(), $kwconfig);
}
}

View file

@ -0,0 +1,83 @@
<?php
namespace KangarooPunch;
use KangarooPunch\KPunch;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Toolkit\V;
class KwNumbers {
public static function create(array $input): bool {
validate($input);
$db = KPunch::databaseGet();
KPunch::databaseQueryToJSON($db,
'INSERT INTO numbers (name, data, description) VALUES (?, ?, ?)',
array(
$input['name'],
$input['data'],
$input['description']
)
);
return true;
}
public static function delete(string $id): bool {
$db = KPunch::databaseGet();
KPunch::databaseQueryToJSON($db, 'DELETE FROM numbers WHERE id = ?', array($id));
return true;
}
public static function find(string $id): array {
$db = KPunch::databaseGet();
$result = KPunch::databaseQueryToJSON($db, 'SELECT * FROM numbers WHERE id = ?', array($id));
if ($result == null) {
throw new NotFoundException('The entry could not be found');
}
return json_decode($result);
}
public static function list(): array {
$db = KPunch::databaseGet();
return json_decode(KPunch::databaseQueryToJSON($db, 'SELECT * FROM numbers'));
}
public static function update(array $input): bool {
validate($input);
$db = KPunch::databaseGet();
KPunch::databaseQueryToJSON($db,
'UPDATE numbers SET name = ?, data = ?, description = ? WHERE id = ?',
array(
$input['name'],
$input['data'],
$input['description'],
$input['id']
)
);
return true;
}
private static function validate($input) {
if (V::minlength($input['name'], 1) === false) {
throw new InvalidArgumentException('The name must not be empty');
}
if (V::maxlength($input['name'], 32) === false) {
throw new InvalidArgumentException('The name must not be longer than 32 characters');
}
if (V::maxlength($input['description'], 255) === false) {
throw new InvalidArgumentException('The description must not be longer than 255 characters');
}
}
}
?>

View file

@ -1,6 +1,7 @@
<?php
use KangarooPunch\KwConfig;
use KangarooPunch\KwNumbers;
use KangarooPunch\KwStrings;
return [
@ -8,6 +9,10 @@ return [
return new Collection(KwConfig::list());
},
'kwnumbers' => function($site) {
return new Collection(KwNumbers::list());
},
'kwstrings' => function($site) {
return new Collection(KwStrings::list());
}

View file

@ -1,17 +1,10 @@
<?php
/*
load([
'KangarooPunch\KPunch ' => __DIR__ . '/classes/KPunch.php',
'KangarooPunch\KwConfig' => __DIR__ . '/classes/KwConfig.php',
'KangarooPunch\KwStrings' => __DIR__ . '/classes/KwStrings.php'
]);
*/
load([
'KangarooPunch\KPunch ' => 'KPunch.php',
'KangarooPunch\KwConfig' => 'KwConfig.php',
'KangarooPunch\KwStrings' => 'KwStrings.php'
'KangarooPunch\KwNumbers' => 'KwNumbers.php',
'KangarooPunch\KwStrings' => 'KwStrings.php',
'KangarooPunch\KPunch' => 'KPunch.php'
], __DIR__ . '/classes');

View file

@ -1,20 +0,0 @@
{
"configItem": {
"name": "configItem",
"type": "config",
"data": "2048",
"description": "Sample CONFIG item."
},
"numberItem": {
"name": "numberItem",
"type": "number",
"data": "16550",
"description": "Sample NUMBER item."
},
"stringItem": {
"name": "stringItem",
"type": "string",
"data": "aValue",
"description": "Sample STRING item."
}
}

97
schema.sql Normal file
View file

@ -0,0 +1,97 @@
-- MySQL dump 10.19 Distrib 10.3.32-MariaDB, for debian-linux-gnu (x86_64)
--
-- Host: 192.168.0.3 Database: dosThing
-- ------------------------------------------------------
-- Server version 10.5.13-MariaDB-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `config`
--
DROP TABLE IF EXISTS `config`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `config` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`data` varchar(4096) NOT NULL,
`description` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `nameIDX` (`name`),
KEY `descriptionIDX` (`description`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `numbers`
--
DROP TABLE IF EXISTS `numbers`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `numbers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`data` int(11) NOT NULL,
`description` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `nameIDX` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `strings`
--
DROP TABLE IF EXISTS `strings`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `strings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`data` varchar(4096) NOT NULL,
`description` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `nameIDX` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `users`
--
DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` varchar(256) NOT NULL,
`pass` varchar(256) NOT NULL,
`email` varchar(256) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `emailIDX` (`email`),
UNIQUE KEY `userIDX` (`user`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2022-01-12 19:05:55