From f2cf7ff7640511afbd8a3325858e719e19fa9963 Mon Sep 17 00:00:00 2001 From: marko Date: Tue, 26 Dec 2023 20:24:34 +0100 Subject: [PATCH] wip: added rest api endpoint shiai --- homepage/participo/.gitignore | 4 +- homepage/participo/.vscode/settings.json | 11 +- homepage/participo/.vscode/sftp.json | 4 +- homepage/participo/api/inc/.htaccess | 1 + homepage/participo/api/inc/bootstrap.php | 31 ++++++ homepage/participo/api/shiai.php | 14 +++ homepage/participo/api/users.php | 87 ++++++++------- .../participo/lib/participoLib/apiKey.php | 2 +- .../lib/participoLib/dbConnector.php | 5 +- homepage/participo/lib/participoLib/shiai.php | 17 +++ homepage/participo/package-lock.json | 103 ++++++++++++++++++ homepage/participo/package.json | 10 ++ 12 files changed, 245 insertions(+), 44 deletions(-) create mode 100644 homepage/participo/api/inc/.htaccess create mode 100644 homepage/participo/api/inc/bootstrap.php create mode 100644 homepage/participo/api/shiai.php create mode 100644 homepage/participo/package-lock.json create mode 100644 homepage/participo/package.json diff --git a/homepage/participo/.gitignore b/homepage/participo/.gitignore index be8da08..e047986 100644 --- a/homepage/participo/.gitignore +++ b/homepage/participo/.gitignore @@ -1 +1,3 @@ -local/*.php \ No newline at end of file +local/*.php +node_modules/* +testApi.config.yaml diff --git a/homepage/participo/.vscode/settings.json b/homepage/participo/.vscode/settings.json index 1da7818..d248b80 100644 --- a/homepage/participo/.vscode/settings.json +++ b/homepage/participo/.vscode/settings.json @@ -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}" ] } \ No newline at end of file diff --git a/homepage/participo/.vscode/sftp.json b/homepage/participo/.vscode/sftp.json index 39cd462..fbe7d1e 100644 --- a/homepage/participo/.vscode/sftp.json +++ b/homepage/participo/.vscode/sftp.json @@ -8,7 +8,9 @@ "ignore": [ ".vscode", "*.py", - "*.config.yaml" + "*.config.yaml", + "package.json", "package-lock.json" + , "node-modules" ], "uploadOnSave": true } \ No newline at end of file diff --git a/homepage/participo/api/inc/.htaccess b/homepage/participo/api/inc/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/homepage/participo/api/inc/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/homepage/participo/api/inc/bootstrap.php b/homepage/participo/api/inc/bootstrap.php new file mode 100644 index 0000000..3ce6c3c --- /dev/null +++ b/homepage/participo/api/inc/bootstrap.php @@ -0,0 +1,31 @@ + diff --git a/homepage/participo/api/users.php b/homepage/participo/api/users.php index 2e50205..93820b4 100644 --- a/homepage/participo/api/users.php +++ b/homepage/participo/api/users.php @@ -1,70 +1,81 @@ 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); -?> \ No newline at end of file +// exit(0); + +?> diff --git a/homepage/participo/lib/participoLib/apiKey.php b/homepage/participo/lib/participoLib/apiKey.php index 77bfba1..5b31871 100644 --- a/homepage/participo/lib/participoLib/apiKey.php +++ b/homepage/participo/lib/participoLib/apiKey.php @@ -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( diff --git a/homepage/participo/lib/participoLib/dbConnector.php b/homepage/participo/lib/participoLib/dbConnector.php index f0ea0f0..1af8c20 100644 --- a/homepage/participo/lib/participoLib/dbConnector.php +++ b/homepage/participo/lib/participoLib/dbConnector.php @@ -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) diff --git a/homepage/participo/lib/participoLib/shiai.php b/homepage/participo/lib/participoLib/shiai.php index 150a1da..ab59543 100644 --- a/homepage/participo/lib/participoLib/shiai.php +++ b/homepage/participo/lib/participoLib/shiai.php @@ -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( diff --git a/homepage/participo/package-lock.json b/homepage/participo/package-lock.json new file mode 100644 index 0000000..c92a28a --- /dev/null +++ b/homepage/participo/package-lock.json @@ -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" + } + } + } +} diff --git a/homepage/participo/package.json b/homepage/participo/package.json new file mode 100644 index 0000000..06ec2e3 --- /dev/null +++ b/homepage/participo/package.json @@ -0,0 +1,10 @@ +{ + "devDependencies": { + "@prettier/plugin-php": "^0.22.1", + "prettier": "^3.1.1" + }, + "prettier":{ + "parser": "php", + "plugins": ["@prettier/plugin-php"] + } +}