From 2077ce8709a798bc3f75d6f95577b69acfae695d Mon Sep 17 00:00:00 2001 From: Mateus Zimmermann Date: Tue, 12 Sep 2017 22:36:29 -0300 Subject: [PATCH 1/2] First commit --- .gitignore | 1 + README.md | 48 +-- composer.json | 11 + composer.lock | 648 +++++++++++++++++++++++++++++++++ data/dump.sql | 37 ++ public/index.php | 110 ++++++ src/Entity/Entity.php | 182 +++++++++ src/Entity/EntityInterface.php | 7 + src/User/User.php | 22 ++ 9 files changed, 1032 insertions(+), 34 deletions(-) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 data/dump.sql create mode 100644 public/index.php create mode 100644 src/Entity/Entity.php create mode 100644 src/Entity/EntityInterface.php create mode 100644 src/User/User.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..49ce3c19 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor \ No newline at end of file diff --git a/README.md b/README.md index 8fb16649..fbdad5c6 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,14 @@ -# A tarefa -Sua tarefa consiste em desenvolver uma API RESTful para manipular um determinado recurso. Deverá ser utilizado o framework Silex. - -# Requisitos -A escolha do recurso deverá ser feita pelo desenvolvedor, atendendo apenas os requisitos mínimos abaixo: - -* Deverá conter um ID -* Deverá conter pelo menos quatro propriedades (exemplos: nome, email, etc.) -* Deverá conter campos que armazenem as datas de criação e alteração do recurso - -A API deverá atender às seguintes exigências: - -* Listagem de todos os recursos -* Busca de um recurso pelo ID -* Criação de um novo recurso -* Alteração de um recurso existente -* Exclusão de um recurso -* Aceitar e retornar apenas JSON -* Deverá possuir algum método de autenticação para utilização de seus endpoints - -# Ferramentas -* PHP -* Banco de dados MySQL -* Framework Silex - -# Fluxo de desenvolvimento -1. Faça um fork deste repositório -2. Crie uma nova branch e nomeie-a com seu usuário do Github -3. Quando o desenvolvimento estiver concluído, faça um pull request - -# Padrões de nomenclatura -1. Código fonte, nome do banco de dados, tabelas e campos devem estar em inglês - -**Inclua no seu commit todos os arquivos necessários para que possamos testar o código.** +# Database +Import database: data/dump.sql +Configure connection in public/index.php + +# Initialize +cd public +php -S localhost:8000 + +# JSON POST/PUT +{ + "name": "Teste Nome", + "email": "testeemail@email.com", + "password": "123456" +} \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..390b32da --- /dev/null +++ b/composer.json @@ -0,0 +1,11 @@ +{ + "name": "root/teste", + "require": { + "silex/silex": "~2.0" + }, + "autoload": { + "psr-4": { + "Test\\": "src/" + } + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 00000000..ec9e6e48 --- /dev/null +++ b/composer.lock @@ -0,0 +1,648 @@ +{ + "_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#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "content-hash": "4c3ac9b93b857826bf43dc864ed4e940", + "packages": [ + { + "name": "pimple/pimple", + "version": "v3.2.2", + "source": { + "type": "git", + "url": "https://github.com/silexphp/Pimple.git", + "reference": "4d45fb62d96418396ec58ba76e6f065bca16e10a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/silexphp/Pimple/zipball/4d45fb62d96418396ec58ba76e6f065bca16e10a", + "reference": "4d45fb62d96418396ec58ba76e6f065bca16e10a", + "shasum": "" + }, + "require": { + "php": ">=5.3.0", + "psr/container": "^1.0" + }, + "require-dev": { + "symfony/phpunit-bridge": "^3.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.2.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": "http://pimple.sensiolabs.org", + "keywords": [ + "container", + "dependency injection" + ], + "time": "2017-07-23T07:32:15+00:00" + }, + { + "name": "psr/container", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/container.git", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Container\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://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" + ], + "time": "2017-02-14T16:28:37+00:00" + }, + { + "name": "psr/log", + "version": "1.0.2", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "4ebe3a8bf773a19edfe0a84b6585ba3d401b724d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/4ebe3a8bf773a19edfe0a84b6585ba3d401b724d", + "reference": "4ebe3a8bf773a19edfe0a84b6585ba3d401b724d", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "Psr/Log/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "time": "2016-10-10T12:19:37+00:00" + }, + { + "name": "silex/silex", + "version": "v2.2.0", + "source": { + "type": "git", + "url": "https://github.com/silexphp/Silex.git", + "reference": "ec7d5b5334465414952d4b2e935e73bd085dbbbb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/silexphp/Silex/zipball/ec7d5b5334465414952d4b2e935e73bd085dbbbb", + "reference": "ec7d5b5334465414952d4b2e935e73bd085dbbbb", + "shasum": "" + }, + "require": { + "php": ">=5.5.9", + "pimple/pimple": "~3.0", + "symfony/event-dispatcher": "~2.8|^3.0", + "symfony/http-foundation": "~2.8|^3.0", + "symfony/http-kernel": "~2.8|^3.0", + "symfony/routing": "~2.8|^3.0" + }, + "conflict": { + "phpunit/phpunit": "<4.8.35 || >= 5.0, <5.4.3" + }, + "replace": { + "silex/api": "self.version", + "silex/providers": "self.version" + }, + "require-dev": { + "doctrine/dbal": "~2.2", + "monolog/monolog": "^1.4.1", + "swiftmailer/swiftmailer": "~5", + "symfony/asset": "~2.8|^3.0", + "symfony/browser-kit": "~2.8|^3.0", + "symfony/config": "~2.8|^3.0", + "symfony/css-selector": "~2.8|^3.0", + "symfony/debug": "~2.8|^3.0", + "symfony/doctrine-bridge": "~2.8|^3.0", + "symfony/dom-crawler": "~2.8|^3.0", + "symfony/expression-language": "~2.8|^3.0", + "symfony/finder": "~2.8|^3.0", + "symfony/form": "~2.8|^3.0", + "symfony/intl": "~2.8|^3.0", + "symfony/monolog-bridge": "~2.8|^3.0", + "symfony/options-resolver": "~2.8|^3.0", + "symfony/phpunit-bridge": "^3.2", + "symfony/process": "~2.8|^3.0", + "symfony/security": "~2.8|^3.0", + "symfony/serializer": "~2.8|^3.0", + "symfony/translation": "~2.8|^3.0", + "symfony/twig-bridge": "~2.8|^3.0", + "symfony/validator": "~2.8|^3.0", + "symfony/var-dumper": "~2.8|^3.0", + "symfony/web-link": "^3.3", + "twig/twig": "~1.28|~2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.2.x-dev" + } + }, + "autoload": { + "psr-4": { + "Silex\\": "src/Silex" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Igor Wiedler", + "email": "igor@wiedler.ch" + } + ], + "description": "The PHP micro-framework based on the Symfony Components", + "homepage": "http://silex.sensiolabs.org", + "keywords": [ + "microframework" + ], + "time": "2017-07-23T07:40:14+00:00" + }, + { + "name": "symfony/debug", + "version": "v3.3.9", + "source": { + "type": "git", + "url": "https://github.com/symfony/debug.git", + "reference": "8beb24eec70b345c313640962df933499373a944" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/debug/zipball/8beb24eec70b345c313640962df933499373a944", + "reference": "8beb24eec70b345c313640962df933499373a944", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8", + "psr/log": "~1.0" + }, + "conflict": { + "symfony/http-kernel": ">=2.3,<2.3.24|~2.4.0|>=2.5,<2.5.9|>=2.6,<2.6.2" + }, + "require-dev": { + "symfony/http-kernel": "~2.8|~3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Debug\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Debug Component", + "homepage": "https://symfony.com", + "time": "2017-09-01T13:23:39+00:00" + }, + { + "name": "symfony/event-dispatcher", + "version": "v3.3.9", + "source": { + "type": "git", + "url": "https://github.com/symfony/event-dispatcher.git", + "reference": "54ca9520a00386f83bca145819ad3b619aaa2485" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/54ca9520a00386f83bca145819ad3b619aaa2485", + "reference": "54ca9520a00386f83bca145819ad3b619aaa2485", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8" + }, + "conflict": { + "symfony/dependency-injection": "<3.3" + }, + "require-dev": { + "psr/log": "~1.0", + "symfony/config": "~2.8|~3.0", + "symfony/dependency-injection": "~3.3", + "symfony/expression-language": "~2.8|~3.0", + "symfony/stopwatch": "~2.8|~3.0" + }, + "suggest": { + "symfony/dependency-injection": "", + "symfony/http-kernel": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\EventDispatcher\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony EventDispatcher Component", + "homepage": "https://symfony.com", + "time": "2017-07-29T21:54:42+00:00" + }, + { + "name": "symfony/http-foundation", + "version": "v3.3.9", + "source": { + "type": "git", + "url": "https://github.com/symfony/http-foundation.git", + "reference": "2cdc7de1921d1a1c805a13dc05e44a2cd58f5ad3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/http-foundation/zipball/2cdc7de1921d1a1c805a13dc05e44a2cd58f5ad3", + "reference": "2cdc7de1921d1a1c805a13dc05e44a2cd58f5ad3", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8", + "symfony/polyfill-mbstring": "~1.1" + }, + "require-dev": { + "symfony/expression-language": "~2.8|~3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\HttpFoundation\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony HttpFoundation Component", + "homepage": "https://symfony.com", + "time": "2017-09-06T17:07:39+00:00" + }, + { + "name": "symfony/http-kernel", + "version": "v3.3.9", + "source": { + "type": "git", + "url": "https://github.com/symfony/http-kernel.git", + "reference": "70f5bb3cdd737624249953b61023411e26be5db7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/http-kernel/zipball/70f5bb3cdd737624249953b61023411e26be5db7", + "reference": "70f5bb3cdd737624249953b61023411e26be5db7", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8", + "psr/log": "~1.0", + "symfony/debug": "~2.8|~3.0", + "symfony/event-dispatcher": "~2.8|~3.0", + "symfony/http-foundation": "~3.3" + }, + "conflict": { + "symfony/config": "<2.8", + "symfony/dependency-injection": "<3.3", + "symfony/var-dumper": "<3.3", + "twig/twig": "<1.34|<2.4,>=2" + }, + "require-dev": { + "psr/cache": "~1.0", + "symfony/browser-kit": "~2.8|~3.0", + "symfony/class-loader": "~2.8|~3.0", + "symfony/config": "~2.8|~3.0", + "symfony/console": "~2.8|~3.0", + "symfony/css-selector": "~2.8|~3.0", + "symfony/dependency-injection": "~3.3", + "symfony/dom-crawler": "~2.8|~3.0", + "symfony/expression-language": "~2.8|~3.0", + "symfony/finder": "~2.8|~3.0", + "symfony/process": "~2.8|~3.0", + "symfony/routing": "~2.8|~3.0", + "symfony/stopwatch": "~2.8|~3.0", + "symfony/templating": "~2.8|~3.0", + "symfony/translation": "~2.8|~3.0", + "symfony/var-dumper": "~3.3" + }, + "suggest": { + "symfony/browser-kit": "", + "symfony/class-loader": "", + "symfony/config": "", + "symfony/console": "", + "symfony/dependency-injection": "", + "symfony/finder": "", + "symfony/var-dumper": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\HttpKernel\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony HttpKernel Component", + "homepage": "https://symfony.com", + "time": "2017-09-11T16:13:23+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.5.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "7c8fae0ac1d216eb54349e6a8baa57d515fe8803" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/7c8fae0ac1d216eb54349e6a8baa57d515fe8803", + "reference": "7c8fae0ac1d216eb54349e6a8baa57d515fe8803", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.5-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "time": "2017-06-14T15:44:48+00:00" + }, + { + "name": "symfony/routing", + "version": "v3.3.9", + "source": { + "type": "git", + "url": "https://github.com/symfony/routing.git", + "reference": "970326dcd04522e1cd1fe128abaee54c225e27f9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/routing/zipball/970326dcd04522e1cd1fe128abaee54c225e27f9", + "reference": "970326dcd04522e1cd1fe128abaee54c225e27f9", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8" + }, + "conflict": { + "symfony/config": "<2.8", + "symfony/dependency-injection": "<3.3", + "symfony/yaml": "<3.3" + }, + "require-dev": { + "doctrine/annotations": "~1.0", + "doctrine/common": "~2.2", + "psr/log": "~1.0", + "symfony/config": "~2.8|~3.0", + "symfony/dependency-injection": "~3.3", + "symfony/expression-language": "~2.8|~3.0", + "symfony/http-foundation": "~2.8|~3.0", + "symfony/yaml": "~3.3" + }, + "suggest": { + "doctrine/annotations": "For using the annotation loader", + "symfony/config": "For using the all-in-one router or any loader", + "symfony/dependency-injection": "For loading routes from a service", + "symfony/expression-language": "For using expression matching", + "symfony/http-foundation": "For using a Symfony Request object", + "symfony/yaml": "For using the YAML loader" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Routing\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Routing Component", + "homepage": "https://symfony.com", + "keywords": [ + "router", + "routing", + "uri", + "url" + ], + "time": "2017-07-29T21:54:42+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/data/dump.sql b/data/dump.sql new file mode 100644 index 00000000..58087f3f --- /dev/null +++ b/data/dump.sql @@ -0,0 +1,37 @@ +-- MySQL dump 10.13 Distrib 5.7.17, for Win64 (x86_64) +-- +-- Host: 127.0.0.1 Database: test_rest +-- ------------------------------------------------------ +-- Server version 5.7.19-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 utf8 */; +/*!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 */; + +CREATE DATABASE `test`; +USE `test`; + +-- +-- 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, + `name` varchar(50) NOT NULL, + `email` varchar(50) NOT NULL, + `password` varchar(20) NOT NULL, + `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated` datetime DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; \ No newline at end of file diff --git a/public/index.php b/public/index.php new file mode 100644 index 00000000..10ae336f --- /dev/null +++ b/public/index.php @@ -0,0 +1,110 @@ +get('/', function(Application $app){ + return $app->redirect('/api/v1'); +}); + +$app->get('/api/v1', function(Request $request) use ($app) { + + return 'API Working'; + +}); + +$app->get('/api/v1/users', function(Application $app){ + $user = new User($app['entity']); + $users = $user->getEntity()->getAll(); + + return $app->json($users); +}); + +$app->get('/api/v1/users/{id}', function(Application $app, $id){ + + $user = new User($app['entity']); + $user = $user->getEntity()->where(array('id' => $id)); + + return $app->json($user); +}) +->convert('id', function($id){ return (int) $id; }); + +$app->post('/api/v1/users', function(Application $app, Request $request){ + $data = json_decode($request->getContent(), true); + + $user = array( + 'name' => (string) $data['name'], + 'email' => (string) $data['email'], + 'password' => (string) $data['password'], + 'created' => $app['currentDateTime'] + ); + + $save = new User($app['entity']); + $save = $save->getEntity()->save($user); + + $return = array('status' => true); + + if(!$save) $return = array('status' => false); + + return $app->json($return); + +}); + +$app->put('/api/v1/users/{id}', function(Application $app, Request $request, $id){ + + $data = json_decode($request->getContent(), true); + + $user = array( + 'name' => (string) $data['name'], + 'email' => (string) $data['email'], + 'password' => (string) $data['password'], + 'updated' => $app['currentDateTime'] + ); + + $update = new User($app['entity']); + $update = $update->getEntity()->update($id, $user); + + + $return = array('status' => true); + + if(!$update) $return = array('status' => false); + + return $app->json($return); + +}); + +$app->delete('/api/v1/users/{id}', function(Application $app, $id){ + + $user = new User($app['entity']); + $userDeleted = $user->getEntity()->delete($id); + + + $return = array('status' => true); + + if(!$userDeleted) $return = array('status' => false); + + return $app->json($return); +}); + +$app->run(); \ No newline at end of file diff --git a/src/Entity/Entity.php b/src/Entity/Entity.php new file mode 100644 index 00000000..9c470069 --- /dev/null +++ b/src/Entity/Entity.php @@ -0,0 +1,182 @@ +conn = $conn; + } + /** + * Set Table + */ + public function setTable($table) + { + $this->table = $table; + } + + /** + * Save data in database (Insert Or Update) + * @return bool + */ + public function save(array $data) + { + return $this->insert($data); + } + + /** + * Insert data in database + * @return bool + */ + private function insert(array $data = array()) + { + foreach($data as $key => $value) { + $fields[] = $key; + $bind[] = ':' . $key; + } + + $fields = implode(', ', $fields); + $bind = implode(', ', $bind); + + $sql = "INSERT INTO " . $this->table . "(" . $fields . ") + VALUES(" . $bind . ")"; + + try{ + $insert = $this->conn->prepare($sql); + + foreach($data as $key => $value) { + + $insert->bindValue(":" . $key, $value, !is_int($value)? \PDO::PARAM_STR : \PDO::PARAM_INT); + } + + if($insert->execute()) { + return true; + } + + return false; + + } catch(PDOexception $e) { + return 'Error to insert data in database'; + } + } + + /** + * Update data in database + * @return bool + */ + public function update($id, array $datas) + { + $datasSql = ''; + + foreach($datas as $key => $a) { + $datasSql .= $key . ' = ' . ':' . $key . ', '; + } + + $datasSql = substr($datasSql, 0, -2); + + $sql = "UPDATE " . $this->table . " SET " . $datasSql . " WHERE id = :id"; + + try{ + $update = $this->conn->prepare($sql); + + $update->bindValue(':id', $id, \PDO::PARAM_INT); + + foreach($datas as $key => $value) { + $update->bindValue(":" . $key, $value, !is_int($value)? \PDO::PARAM_STR : \PDO::PARAM_INT); + } + + return $update->execute(); + + } catch(PDOexception $e) { + return false; + } + } + + /** + * Get all datas in bd + * @return array with datas + */ + public function getAll($fields = '*') + { + $sql = "SELECT " . $fields . " FROM " . $this->table; + + try { + $select = $this->conn->prepare($sql); + $select->execute(); + + } catch(PDOexception $e) { + return false; + } + + return $select->fetchAll(\PDO::FETCH_ASSOC); + } + + /** + * Get especific data + * + */ + public function where($where, $fields = '*', $more = 'AND') + { + foreach($where as $key => $w) { + $whereSql = $key . ' = :' . $key; + } + + $sql = "SELECT " . $fields . " FROM " . $this->table . " WHERE " . $whereSql; + + try { + $select = $this->conn->prepare($sql); + foreach($where as $key => $value) { + $select->bindValue(":" . $key, $value, is_int($value)? \PDO::PARAM_STR : \PDO::PARAM_INT); + } + $select->execute(); + + } catch(PDOexception $e) { + return false; + } + + return $select->fetchAll(\PDO::FETCH_ASSOC); + } + + /** + * Delete data + * @param [id] [data's id] + */ + public function delete($id) + { + $sql = "DELETE FROM " . $this->table . " WHERE id = :id"; + + try { + $delete = $this->conn->prepare($sql); + $delete->bindValue(':id', $id, \PDO::PARAM_INT); + + if($delete->execute()) { + return true; + } + + } catch(PDOexception $e) { + return false; + } + } + /** + * Count total itens in especific table + * @return int total of registries + */ + public function total() + { + return count($this->getAll()); + } +} diff --git a/src/Entity/EntityInterface.php b/src/Entity/EntityInterface.php new file mode 100644 index 00000000..9376ff92 --- /dev/null +++ b/src/Entity/EntityInterface.php @@ -0,0 +1,7 @@ +entity = $entity; + + $this->entity->setTable('users'); + } + + public function getEntity() + { + return $this->entity; + } +} \ No newline at end of file From 71166957d9be90c97befefba51f172686c88db8e Mon Sep 17 00:00:00 2001 From: Mateus Zimmermann Date: Fri, 15 Sep 2017 10:36:32 -0300 Subject: [PATCH 2/2] Adjust readme --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index fbdad5c6..b2e8d922 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,20 @@ # Database Import database: data/dump.sql + Configure connection in public/index.php # Initialize cd public + php -S localhost:8000 # JSON POST/PUT { + "name": "Teste Nome", + "email": "testeemail@email.com", + "password": "123456" + } \ No newline at end of file