diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5657f6e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor \ No newline at end of file diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000..11b28de --- /dev/null +++ b/.htaccess @@ -0,0 +1,4 @@ +RewriteEngine On +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule ^ index.php [QSA,L] \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..da3fb5a --- /dev/null +++ b/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "slim/slim": "^3.12" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..150de6a --- /dev/null +++ b/composer.lock @@ -0,0 +1,300 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "a200524ba4252697eaecc09aadda13fd", + "packages": [ + { + "name": "nikic/fast-route", + "version": "v1.3.0", + "source": { + "type": "git", + "url": "https://github.com/nikic/FastRoute.git", + "reference": "181d480e08d9476e61381e04a71b34dc0432e812" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nikic/FastRoute/zipball/181d480e08d9476e61381e04a71b34dc0432e812", + "reference": "181d480e08d9476e61381e04a71b34dc0432e812", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35|~5.7" + }, + "type": "library", + "autoload": { + "psr-4": { + "FastRoute\\": "src/" + }, + "files": [ + "src/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Nikita Popov", + "email": "nikic@php.net" + } + ], + "description": "Fast request router for PHP", + "keywords": [ + "router", + "routing" + ], + "support": { + "issues": "https://github.com/nikic/FastRoute/issues", + "source": "https://github.com/nikic/FastRoute/tree/master" + }, + "time": "2018-02-13T20:26:39+00:00" + }, + { + "name": "pimple/pimple", + "version": "v3.5.0", + "source": { + "type": "git", + "url": "https://github.com/silexphp/Pimple.git", + "reference": "a94b3a4db7fb774b3d78dad2315ddc07629e1bed" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/silexphp/Pimple/zipball/a94b3a4db7fb774b3d78dad2315ddc07629e1bed", + "reference": "a94b3a4db7fb774b3d78dad2315ddc07629e1bed", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "psr/container": "^1.1 || ^2.0" + }, + "require-dev": { + "symfony/phpunit-bridge": "^5.4@dev" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.4.x-dev" + } + }, + "autoload": { + "psr-0": { + "Pimple": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Pimple, a simple Dependency Injection Container", + "homepage": "https://pimple.symfony.com", + "keywords": [ + "container", + "dependency injection" + ], + "support": { + "source": "https://github.com/silexphp/Pimple/tree/v3.5.0" + }, + "time": "2021-10-28T11:13:42+00:00" + }, + { + "name": "psr/container", + "version": "1.1.2", + "source": { + "type": "git", + "url": "https://github.com/php-fig/container.git", + "reference": "513e0666f7216c7459170d56df27dfcefe1689ea" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/container/zipball/513e0666f7216c7459170d56df27dfcefe1689ea", + "reference": "513e0666f7216c7459170d56df27dfcefe1689ea", + "shasum": "" + }, + "require": { + "php": ">=7.4.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Psr\\Container\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common Container Interface (PHP FIG PSR-11)", + "homepage": "https://github.com/php-fig/container", + "keywords": [ + "PSR-11", + "container", + "container-interface", + "container-interop", + "psr" + ], + "support": { + "issues": "https://github.com/php-fig/container/issues", + "source": "https://github.com/php-fig/container/tree/1.1.2" + }, + "time": "2021-11-05T16:50:12+00:00" + }, + { + "name": "psr/http-message", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "support": { + "source": "https://github.com/php-fig/http-message/tree/master" + }, + "time": "2016-08-06T14:39:51+00:00" + }, + { + "name": "slim/slim", + "version": "3.12.3", + "source": { + "type": "git", + "url": "https://github.com/slimphp/Slim.git", + "reference": "1c9318a84ffb890900901136d620b4f03a59da38" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/slimphp/Slim/zipball/1c9318a84ffb890900901136d620b4f03a59da38", + "reference": "1c9318a84ffb890900901136d620b4f03a59da38", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-libxml": "*", + "ext-simplexml": "*", + "nikic/fast-route": "^1.0", + "php": ">=5.5.0", + "pimple/pimple": "^3.0", + "psr/container": "^1.0", + "psr/http-message": "^1.0" + }, + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0", + "squizlabs/php_codesniffer": "^2.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Slim\\": "Slim" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Josh Lockhart", + "email": "hello@joshlockhart.com", + "homepage": "https://joshlockhart.com" + }, + { + "name": "Andrew Smith", + "email": "a.smith@silentworks.co.uk", + "homepage": "http://silentworks.co.uk" + }, + { + "name": "Rob Allen", + "email": "rob@akrabat.com", + "homepage": "http://akrabat.com" + }, + { + "name": "Gabriel Manricks", + "email": "gmanricks@me.com", + "homepage": "http://gabrielmanricks.com" + } + ], + "description": "Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs", + "homepage": "https://slimframework.com", + "keywords": [ + "api", + "framework", + "micro", + "router" + ], + "support": { + "issues": "https://github.com/slimphp/Slim/issues", + "source": "https://github.com/slimphp/Slim/tree/3.x" + }, + "time": "2019-11-28T17:40:33+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.2.0" +} diff --git a/index.php b/index.php new file mode 100644 index 0000000..9224107 --- /dev/null +++ b/index.php @@ -0,0 +1,10 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); + return $pdo; + } + } \ No newline at end of file diff --git a/src/routes/products.php b/src/routes/products.php new file mode 100644 index 0000000..4f8cd87 --- /dev/null +++ b/src/routes/products.php @@ -0,0 +1,97 @@ +get('/', function (Request $request, Response $response, array $args) { + echo 'homepage found'; + }); + + // SHOW ALL + $app->get('/api/product', function(Request $request, Response $response){ + $sql = "SELECT * FROM product"; + + try { + $db = new db(); + $pdo = $db->connect(); + $stmt = $pdo->query($sql); + $product = $stmt->fetchAll(PDO::FETCH_OBJ); + echo json_encode($product); + } catch (\PDOException $e) { + echo '{"msg": {"resp": '.$e->getMessage().'}}'; + } + }); + + // SHOW BY ID + $app->get('/api/product/{id}', function(Request $request, Response $response, array $args){ + $id = $request->getAttribute('id'); + $sql = "SELECT * FROM product WHERE id = $id"; + + try { + $db = new db(); + $pdo = $db->connect(); + $stmt = $pdo->query($sql); + $product = $stmt->fetchAll(PDO::FETCH_OBJ); + $pdo = null; + echo json_encode($product); + } catch (\PDOException $e) { + echo '{"msg": {"resp": '.$e->getMessage().'}}'; + } + }); + + // CREATE + $app->post('/api/product/add', function(Request $request, Response $response, array $args){ + $name = $request->getParam('name'); + $price = $request->getParam('price'); + try { + $db = new db(); + $pdo = $db->connect(); + + $sql = "INSERT INTO product (name, price) VALUES (?,?)"; + $pdo->prepare($sql)->execute([$name, $price]); + echo '{"notice": {"text": "Product '.$name.' has been added to the list"}}'; + $pdo = null; + } catch (\PDOException $e) { + echo '{"msg": {"resp": '.$e->getMessage().'}}'; + } + }); + + // UPDATE + $app->put('/api/product/update/{id}', function(Request $request, Response $response, array $args){ + $id = $request->getAttribute('id'); + $name = $request->getParam('name'); + $price = $request->getParam('price'); + try { + $db = new db(); + $pdo = $db->connect(); + + $sql = "UPDATE product SET name =?, price =? WHERE id=?"; + + $pdo->prepare($sql)->execute([$name, $price, $id]); + echo '{"notice": {"text": "Product '.$name.' has been just updated now"}}'; + $pdo = null; + } catch (\PDOException $e) { + echo '{"msg": {"resp": '.$e->getMessage().'}}'; + } + }); + + // DELETE + $app->delete('/api/product/delete/{id}', function(Request $request, Response $response, array $args){ + $id = $request->getAttribute('id'); + try { + $db = new db(); + $pdo = $db->connect(); + + $sql = "DELETE FROM product WHERE id=?"; + + $pdo->prepare($sql)->execute([$id]); + $pdo = null; + echo '{"notice": {"text": "Product with '.$id.' has been just deleted now"}}'; + } catch (\PDOException $e) { + echo '{"msg": {"resp": '.$e->getMessage().'}}'; + } + }); + +$app->run(); \ No newline at end of file