From f5581d2490e78b68010b2c86407b253a4d81b8b1 Mon Sep 17 00:00:00 2001 From: Eirik Th S Date: Fri, 6 Aug 2021 00:24:44 +0200 Subject: [PATCH] Draft of API, including a DOCS-page --- www/api/v1/.htaccess | 16 ++++++ www/api/v1/api-init.php | 41 ++++++++++++++ www/api/v1/docs.php | 123 ++++++++++++++++++++++++++++++++++++++++ www/api/v1/index.php | 47 +++++++++++++++ www/api/v1/recipes.php | 42 ++++++++++++++ 5 files changed, 269 insertions(+) create mode 100644 www/api/v1/.htaccess create mode 100644 www/api/v1/api-init.php create mode 100644 www/api/v1/docs.php create mode 100644 www/api/v1/index.php create mode 100644 www/api/v1/recipes.php diff --git a/www/api/v1/.htaccess b/www/api/v1/.htaccess new file mode 100644 index 0000000..6e5e71e --- /dev/null +++ b/www/api/v1/.htaccess @@ -0,0 +1,16 @@ +#RewriteBase / + +RewriteEngine On + +#RewriteRule ^/v1/?([*a-zA-Z0-9_-]+)$ /api/index.php?slug1=$1 [L] +RewriteRule ^([*a-zA-Z0-9_-]+)(?:\/|)$ index.php?fi=$1 [L] +#RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?slug=$1 [L] + +#RewriteRule ^project/$ projects/index.php [L] +#RewriteRule ^projects/$ projects/index.php [L] +#RewriteRule ^v1/?([a-zA-Z0-9_-]+)$ index.php?project=$1 [L] +#RewriteRule ^project/?([a-zA-Z0-9_-]+)/$ projects/index.php?project=$1 [L] +#RewriteRule ^project/?([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ projects/index.php?project=$1&item=$2 [L] +#RewriteRule ^project/?([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ projects/index.php?project=$1&item=$2 [L] +#RewriteRule ^project/?([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ projects/index.php?project=$1&item=$2&ID=$3 [L] +#RewriteRule ^project/?([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ projects/index.php?project=$1&item=$2&ID=$3 [L] diff --git a/www/api/v1/api-init.php b/www/api/v1/api-init.php new file mode 100644 index 0000000..e188fa4 --- /dev/null +++ b/www/api/v1/api-init.php @@ -0,0 +1,41 @@ + $value){ + if(($data[$key] = filter($value)) === false){ + print_r($value); + echo "Failed to sanitize: `".$key."`: ".$value." \t-\t type: ".gettype($value)."\n"; + } + } + } + } + + return $data; +} diff --git a/www/api/v1/docs.php b/www/api/v1/docs.php new file mode 100644 index 0000000..678400d --- /dev/null +++ b/www/api/v1/docs.php @@ -0,0 +1,123 @@ +'GET', "href"=>"/", "name"=>"Nothing", "body"=>"{}"); +$docs['recipe'][] = array("method"=>'GET', "href"=>"/recipe", "name"=>"Get all available recipes", "body"=>"{}"); +$docs['recipe'][] = array("method"=>'POST', "href"=>"/recipe", "name"=>"Create recipe", "body"=>"{\n \"name\": \"string\",\n \"portions\": \"integer\",\n \"public\": \"boolean\" \n}"); + + + +function capitalizeFirst($input){ + $input[0] = strtoupper($input[0]); + return $input; +} +?> + + + + + + + + + + + + + + + + PaperBag API Docs + + + +
+

PaperBag API Docs

+ +
+ + $rows){ + echo "

".capitalizeFirst($group)."

"; + foreach ($rows as $k => $row){ + $i = $count; + $count++; + + switch ($row['method']){ + case 'GET': + $methodClass = "bg-primary"; + break; + case 'POST': + $methodClass = "bg-success"; + break; + default: + $methodClass = "bg-warning"; + } + ?> +
+

+ +

+
+
+ +
+
+
+ + + +
+
+
+
+
+
+
+ + +
+
+ + + + diff --git a/www/api/v1/index.php b/www/api/v1/index.php new file mode 100644 index 0000000..a2522dc --- /dev/null +++ b/www/api/v1/index.php @@ -0,0 +1,47 @@ +error, 1); + } + } + } + else { + returns(Recipes::getAll()); + } + + break; + + case "plan": + returns("Coming soon..."); + break; + + default: + returns('Nothing to do'); +} +returns('Failed.',2); + + +$docs['recipe'][] = array("method"=>'POST', "href"=>"/recipe", "name"=>"Create recipe", "body"=>"{\n \"name\": \"string\",\n \"public\": \"boolean\" \n}"); \ No newline at end of file diff --git a/www/api/v1/recipes.php b/www/api/v1/recipes.php new file mode 100644 index 0000000..1dac1ce --- /dev/null +++ b/www/api/v1/recipes.php @@ -0,0 +1,42 @@ +query($sql); + while($row = $res->fetch_assoc()){ + $return[$row['recipe_id']] = $row; + + +// $return[$row['recipe_id']]['items'] = ; + } + + if(empty($return)){ + $return[] = "No recipes found"; + } + + return $return; + } + + static function createRecipe($name, $portions = 1, $public = 0) { + global $user_id, $db; + + if($portions == null){ + $portions = 1; + } + + if(strlen($name) <= 200 && is_numeric($public)){ + $createRecipeSQL = "INSERT INTO `recipe` (name, author, portions, public) VALUES ('$name', $user_id, $portions, $public);"; + if($db->query($createRecipeSQL)){ + return $db->insert_id; + } + } + return false; + } +} +