wip: added rest api endpoint shiai

This commit is contained in:
marko
2023-12-26 20:24:34 +01:00
parent 49978a48df
commit f2cf7ff764
12 changed files with 245 additions and 44 deletions

View File

@@ -1 +1,3 @@
local/*.php
local/*.php
node_modules/*
testApi.config.yaml

View File

@@ -1,7 +1,16 @@
{
"cSpell.language": "en,de-de",
"cSpell.words": [
"CURDATE",
"cwsv",
"cwsvjudo",
"participo",
"retval",
"shiai"
"shiai",
"vormundschaft",
"wettkampfkalender"
],
"prettier.documentSelectors": [
"**/*.{js,jsx,ts,tsx,vue,html,css,scss,less,json,md,mdx,graphql,yaml,yml,php}"
]
}

View File

@@ -8,7 +8,9 @@
"ignore": [
".vscode",
"*.py",
"*.config.yaml"
"*.config.yaml",
"package.json", "package-lock.json"
, "node-modules"
],
"uploadOnSave": true
}

View File

@@ -0,0 +1 @@
Deny from all

View File

@@ -0,0 +1,31 @@
<?php
/// @file common settings and includes for the participo api
/// - set locale to german
setlocale(LC_ALL, "de_DE@euro", "de_DE", "de", "ge");
/// - extend the include search path for
set_include_path(implode(
PATH_SEPARATOR,
[
get_include_path(),
/// - user defined libs (e.g. participo)
"../lib",
/// - config files (we reuse the participo-wide configuration)
".."
]
));
/// - participo configuration
require_once("config/participo.php");
/// - data base credentials
require_once("local/cwsvJudo.php");
/// - since this is a rest api implementation we can assume each endpoint needs dbAccess
require_once("participoLib/dbConnector.php");
/// - initialize the database connection
dbConnector::connect(
$cwsvJudoConfig["db"]["host"],
$cwsvJudoConfig["db"]["name"],
$cwsvJudoConfig["db"]["user"],
$cwsvJudoConfig["db"]["password"]
);

View File

@@ -0,0 +1,14 @@
<?php
require_once("inc/bootstrap.php");
require_once("participoLib/shiai.php");
// Sending Response
// - setting header
// - we send a json-formatted response
header("Content-Type: application/json");
// - sending body payload
echo json_encode(
Shiai::dbSelect()
);
?>

View File

@@ -1,70 +1,81 @@
<?php
setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge');
set_include_path(get_include_path() . PATH_SEPARATOR . '../lib/'. PATH_SEPARATOR. '..');
setlocale(LC_ALL, "de_DE@euro", "de_DE", "de", "ge");
set_include_path(
get_include_path() . PATH_SEPARATOR . "../lib/" . PATH_SEPARATOR . ".."
);
require_once 'config/participo.php';
require_once 'local/cwsvJudo.php';
require_once "config/participo.php";
require_once "local/cwsvJudo.php";
require_once 'participoLib/participo.php';
require_once "participoLib/participo.php";
$db = dbConnector::connect(
$cwsvJudoConfig['db']['host'],
$cwsvJudoConfig['db']['name'],
$cwsvJudoConfig['db']['user'],
$cwsvJudoConfig['db']['password']
);
function init($config)
{
dbConnector::connect(
$config["db"]["host"],
$config["db"]["name"],
$config["db"]["user"],
$config["db"]["password"]
);
}
function authorize(){
if(array_key_exists("HTTP_AUTHORIZATION", $_SERVER)){
if(!empty($_SERVER["HTTP_AUTHORIZATION"])){
function authorize()
{
if (array_key_exists("HTTP_AUTHORIZATION", $_SERVER)) {
if (!empty($_SERVER["HTTP_AUTHORIZATION"])) {
$auth = explode(" ", $_SERVER["HTTP_AUTHORIZATION"]);
if($auth[0]="Basic"){
if ($auth[0] = "Basic") {
$allowKey = ApiKey::loadFromDb($auth[1]);
}
}
}
if (!$allowKey || !$allowKey->isValidFor('api')) {
die(json_encode(['success' => false]));
if (!$allowKey || !$allowKey->isValidFor("api")) {
die(
json_encode([
"success" => false,
"reason" => "apiKey not sufficient or no api key provided",
])
);
}
}
function get(){
$wkSqlQuery = "SELECT DISTINCT"
." `wkParticipo_Users`.* "
." FROM `wkParticipo_Users`"
." JOIN `vormundschaft`"
." ON `wkParticipo_Users`.`id` =`vormundschaft`.`userId`"
." JOIN `wkParticipo_user<=>userAttributes`"
." ON `wkParticipo_user<=>userAttributes`.`userId` = `vormundschaft`.`kidId`"
." WHERE `wkParticipo_user<=>userAttributes`.`attributeId` = 4"
." ORDER BY `wkParticipo_Users`.`id` ASC;";
function get()
{
$wkSqlQuery =
"SELECT DISTINCT" .
" `wkParticipo_Users`.* " .
" FROM `wkParticipo_Users`" .
" JOIN `vormundschaft`" .
" ON `wkParticipo_Users`.`id` =`vormundschaft`.`userId`" .
" JOIN `wkParticipo_user<=>userAttributes`" .
" ON `wkParticipo_user<=>userAttributes`.`userId` = `vormundschaft`.`kidId`" .
" WHERE `wkParticipo_user<=>userAttributes`.`attributeId` = 4" .
" ORDER BY `wkParticipo_Users`.`id` ASC;";
$wkSqlResponse = dbConnector::query($wkSqlQuery);
// Postprocessing
// - convert the comma separated list into an array
foreach( $wkSqlResponse as &$user){
$user['eMail'] = explode(",", $user['eMail']);
foreach( $user['eMail'] as &$email){
foreach ($wkSqlResponse as &$user) {
$user["eMail"] = explode(",", $user["eMail"]);
foreach ($user["eMail"] as &$email) {
$email = trim($email);
}
}
return $wkSqlResponse;
}
init($cwsvJudoConfig);
authorize();
$wkSqlResponse = get();
// Sending Response
// - setting header
header('Content-Type: application/json');
header("Content-Type: application/json");
// - sending body payload
// @todo die() seems to be more a error handling function. But echo+exit doesn't seem to close the connection (?). What leads to pythons requests.get() always wait for the complete timeout.
die(
json_encode($wkSqlResponse)
);
echo json_encode($wkSqlResponse);
// @todo Should not be necessary. But until the problem with the timeout time of the requesting client is solved, this explicit exit() stands!
exit(0);
?>
// exit(0);
?>

View File

@@ -132,7 +132,7 @@ class ApiKey
// @todo differentiate between inserting and updating if the id is set it should only be updated (e.g. prolonging)
}
/** create an Api key from the return of an sql select * */
/** create an Api key from the return of an sql select */
private static function fromDbArray(array $apiKey)
{
return new ApiKey(

View File

@@ -28,7 +28,7 @@ class dbConnector
public static function query($aQueryString, $aBindArray = [], $someOptions = [])
{
// var_dump($aQueryString, $aBindArray);
// Standardbelegungen
// Standardbelegungen
if (empty($someOptions['dbCharset'])) {
$someOptions['dbCharset'] = 'ISO-8859-1';
}
@@ -65,7 +65,7 @@ class dbConnector
);
}
$pdoResult = $pdoStatement->execute();
if (!$ignoreErrors && !$pdoResult) {
if (!$ignoreErrors && !$pdoResult) {
echo("Error during dbQuery!\n");
echo("DB-Error:\n");
var_dump(self::$db->errorInfo());
@@ -128,6 +128,7 @@ class dbConnector
} else {
self::$db = null;
}
return $success;
}
public static function debugEchoQuery($query, $params)

View File

@@ -15,6 +15,12 @@ class Shiai
private $galleryUrl = null; //< url of the gallery to a gallery of the shiai
private $promoImgUrl = null; //< promotional image for the shiai (as url)
/** name of the table in the db holding the Shiai-s
*
* @var string
*/
private static $tableName = "wettkampfkalender";
public function __construct($id, $date, $name, $ageclasses, $place, $announcementUrl, $routeUrl, $galleryUrl, $promoImgUrl)
{
//! @todo input validation and sanitation
@@ -83,6 +89,17 @@ class Shiai
return self::fromDbArray($response[0]);
}
/** select shiai from the database
*
* - by default, only coming events will be returned
*/
public static function dbSelect(){
$query = "SELECT `".self::$tableName."`.* FROM `".self::$tableName."` WHERE `Datum` >= CURDATE();";
$response = dbConnector::query($query);
return $response;
}
public static function fromDbArray($member)
{
return new shiai(

103
homepage/participo/package-lock.json generated Normal file
View File

@@ -0,0 +1,103 @@
{
"name": "participo",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"devDependencies": {
"@prettier/plugin-php": "^0.22.1",
"prettier": "^3.1.1"
}
},
"node_modules/@prettier/plugin-php": {
"version": "0.22.1",
"resolved": "https://registry.npmjs.org/@prettier/plugin-php/-/plugin-php-0.22.1.tgz",
"integrity": "sha512-TN7tzC2/jCM1/H/mlUjqPos8lIV+vm8Qwp83KofuZclGlG9PoUWHU7m0yqskjAoCy+R4ZCV0hxdBLPBkU69S2Q==",
"dev": true,
"dependencies": {
"linguist-languages": "^7.27.0",
"mem": "^9.0.2",
"php-parser": "^3.1.5"
},
"peerDependencies": {
"prettier": "^3.0.0"
}
},
"node_modules/linguist-languages": {
"version": "7.27.0",
"resolved": "https://registry.npmjs.org/linguist-languages/-/linguist-languages-7.27.0.tgz",
"integrity": "sha512-Wzx/22c5Jsv2ag+uKy+ITanGA5hzvBZngrNGDXLTC7ZjGM6FLCYGgomauTkxNJeP9of353OM0pWqngYA180xgw==",
"dev": true
},
"node_modules/map-age-cleaner": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/map-age-cleaner/-/map-age-cleaner-0.1.3.tgz",
"integrity": "sha512-bJzx6nMoP6PDLPBFmg7+xRKeFZvFboMrGlxmNj9ClvX53KrmvM5bXFXEWjbz4cz1AFn+jWJ9z/DJSz7hrs0w3w==",
"dev": true,
"dependencies": {
"p-defer": "^1.0.0"
},
"engines": {
"node": ">=6"
}
},
"node_modules/mem": {
"version": "9.0.2",
"resolved": "https://registry.npmjs.org/mem/-/mem-9.0.2.tgz",
"integrity": "sha512-F2t4YIv9XQUBHt6AOJ0y7lSmP1+cY7Fm1DRh9GClTGzKST7UWLMx6ly9WZdLH/G/ppM5RL4MlQfRT71ri9t19A==",
"dev": true,
"dependencies": {
"map-age-cleaner": "^0.1.3",
"mimic-fn": "^4.0.0"
},
"engines": {
"node": ">=12.20"
},
"funding": {
"url": "https://github.com/sindresorhus/mem?sponsor=1"
}
},
"node_modules/mimic-fn": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-4.0.0.tgz",
"integrity": "sha512-vqiC06CuhBTUdZH+RYl8sFrL096vA45Ok5ISO6sE/Mr1jRbGH4Csnhi8f3wKVl7x8mO4Au7Ir9D3Oyv1VYMFJw==",
"dev": true,
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/p-defer": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/p-defer/-/p-defer-1.0.0.tgz",
"integrity": "sha512-wB3wfAxZpk2AzOfUMJNL+d36xothRSyj8EXOa4f6GMqYDN9BJaaSISbsk+wS9abmnebVw95C2Kb5t85UmpCxuw==",
"dev": true,
"engines": {
"node": ">=4"
}
},
"node_modules/php-parser": {
"version": "3.1.5",
"resolved": "https://registry.npmjs.org/php-parser/-/php-parser-3.1.5.tgz",
"integrity": "sha512-jEY2DcbgCm5aclzBdfW86GM6VEIWcSlhTBSHN1qhJguVePlYe28GhwS0yoeLYXpM2K8y6wzLwrbq814n2PHSoQ==",
"dev": true
},
"node_modules/prettier": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-3.1.1.tgz",
"integrity": "sha512-22UbSzg8luF4UuZtzgiUOfcGM8s4tjBv6dJRT7j275NXsy2jb4aJa4NNveul5x4eqlF1wuhuR2RElK71RvmVaw==",
"dev": true,
"bin": {
"prettier": "bin/prettier.cjs"
},
"engines": {
"node": ">=14"
},
"funding": {
"url": "https://github.com/prettier/prettier?sponsor=1"
}
}
}
}

View File

@@ -0,0 +1,10 @@
{
"devDependencies": {
"@prettier/plugin-php": "^0.22.1",
"prettier": "^3.1.1"
},
"prettier":{
"parser": "php",
"plugins": ["@prettier/plugin-php"]
}
}