diff --git a/app/AppKernel.php b/app/AppKernel.php index ef068e0..13acec1 100644 --- a/app/AppKernel.php +++ b/app/AppKernel.php @@ -15,6 +15,7 @@ public function registerBundles() new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(), new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(), new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), + new \FOS\RestBundle\FOSRestBundle(), new AppBundle\AppBundle(), ]; diff --git a/app/Resources/views/test/list.html.twig b/app/Resources/views/test/list.html.twig deleted file mode 100644 index 7f3f97e..0000000 --- a/app/Resources/views/test/list.html.twig +++ /dev/null @@ -1,2 +0,0 @@ -

Welcome

-liste des articles \ No newline at end of file diff --git a/app/Resources/views/test/liste.html.twig b/app/Resources/views/test/liste.html.twig deleted file mode 100644 index ba75b3e..0000000 --- a/app/Resources/views/test/liste.html.twig +++ /dev/null @@ -1,7 +0,0 @@ -

liste des articles

- -accueil \ No newline at end of file diff --git a/app/Resources/views/test/page.html.twig b/app/Resources/views/test/page.html.twig deleted file mode 100644 index 338a7c3..0000000 --- a/app/Resources/views/test/page.html.twig +++ /dev/null @@ -1,3 +0,0 @@ -

You are on page {{ page }}

-accueil -retour \ No newline at end of file diff --git a/app/config/config.yml b/app/config/config.yml index 1a5c147..1b49bce 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -18,7 +18,7 @@ framework: form: ~ csrf_protection: ~ validation: { enable_annotations: true } - #serializer: { enable_annotations: true } + serializer: { enable_annotations: true } templating: engines: ['twig'] default_locale: '%locale%' @@ -32,7 +32,6 @@ framework: assets: ~ php_errors: log: true - # Twig Configuration twig: debug: '%kernel.debug%' @@ -67,3 +66,13 @@ swiftmailer: username: '%mailer_user%' password: '%mailer_password%' spool: { type: memory } + +fos_rest: + routing_loader: + include_format: false + view: + view_response_listener: true + format_listener: + rules: + - { path: '^/api', priorities: ['json'], fallback_format: 'json', prefer_extension: false } + - { path: '^/', priorities: ['html'], fallback_format: 'html', prefer_extension: false } diff --git a/app/config/routing.yml b/app/config/routing.yml index 2ec067f..2c57352 100644 --- a/app/config/routing.yml +++ b/app/config/routing.yml @@ -1,3 +1,18 @@ app: resource: '@AppBundle/Controller/' - type: annotation + type: annotation + +articles: + type: rest + prefix: /api + resource: AppBundle\Controller\ArticleController + +users: + type: rest + prefix: /api + resource: AppBundle\Controller\UserController + +comments: + type: rest + prefix: /api + resource: AppBundle\Controller\CommentController \ No newline at end of file diff --git a/composer.json b/composer.json index 166b3ec..10f5249 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,8 @@ "php": "^7.0", "doctrine/doctrine-bundle": "^1.6", "doctrine/orm": "^2.5", + "friendsofsymfony/rest-bundle": "^2.1", + "guzzlehttp/guzzle": "~6.0", "incenteev/composer-parameter-handler": "^2.0", "sensio/distribution-bundle": "^5.0.19", "sensio/framework-extra-bundle": "^3.0.2", diff --git a/composer.lock b/composer.lock index d12271a..97e40bb 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "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": "f237bbf90831cd096588626bf276fd09", + "content-hash": "b0ca17316325a16faa61f274c044bceb", "packages": [ { "name": "composer/ca-bundle", @@ -887,6 +887,288 @@ ], "time": "2016-10-17T18:31:11+00:00" }, + { + "name": "friendsofsymfony/rest-bundle", + "version": "2.2.0", + "source": { + "type": "git", + "url": "https://github.com/FriendsOfSymfony/FOSRestBundle.git", + "reference": "d62a6c0f4bc699f899865d7e7bc7a4186aef9a86" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/FriendsOfSymfony/FOSRestBundle/zipball/d62a6c0f4bc699f899865d7e7bc7a4186aef9a86", + "reference": "d62a6c0f4bc699f899865d7e7bc7a4186aef9a86", + "shasum": "" + }, + "require": { + "doctrine/inflector": "^1.0", + "php": "^5.5.9|~7.0", + "psr/log": "^1.0", + "symfony/config": "^2.7|^3.0", + "symfony/debug": "^2.7|^3.0", + "symfony/dependency-injection": "^2.7|^3.0", + "symfony/event-dispatcher": "^2.7|^3.0", + "symfony/finder": "^2.7|^3.0", + "symfony/framework-bundle": "^2.7|^3.0", + "symfony/http-foundation": "^2.7|^3.0", + "symfony/http-kernel": "^2.7|^3.0", + "symfony/routing": "^2.7|^3.0", + "symfony/security-core": "^2.7|^3.0", + "symfony/templating": "^2.7|^3.0", + "willdurand/jsonp-callback-validator": "^1.0", + "willdurand/negotiation": "^2.0" + }, + "conflict": { + "jms/serializer": "1.3.0", + "sensio/framework-extra-bundle": "<3.0.13" + }, + "require-dev": { + "jms/serializer-bundle": "^1.0", + "phpoption/phpoption": "^1.1", + "psr/http-message": "^1.0", + "sensio/framework-extra-bundle": "^3.0.13", + "symfony/asset": "^2.7|^3.0", + "symfony/browser-kit": "^2.7|^3.0", + "symfony/css-selector": "^2.7|^3.0", + "symfony/dependency-injection": "^2.7|^3.0", + "symfony/expression-language": "~2.7|^3.0", + "symfony/form": "^2.7|^3.0", + "symfony/phpunit-bridge": "^3.2", + "symfony/security-bundle": "^2.7|^3.0", + "symfony/serializer": "^2.7.11|^3.0.4", + "symfony/twig-bundle": "^2.7|^3.0", + "symfony/validator": "^2.7|^3.0", + "symfony/web-profiler-bundle": "^2.7|^3.0", + "symfony/yaml": "^2.7|^3.0" + }, + "suggest": { + "jms/serializer-bundle": "Add support for advanced serialization capabilities, recommended, requires ^1.0", + "sensio/framework-extra-bundle": "Add support for route annotations and the view response listener, requires ^3.0", + "symfony/expression-language": "Add support for using the expression language in the routing, requires ^2.7|^3.0", + "symfony/serializer": "Add support for basic serialization capabilities and xml decoding, requires ^2.7|^3.0", + "symfony/validator": "Add support for validation capabilities in the ParamFetcher, requires ^2.7|^3.0" + }, + "type": "symfony-bundle", + "extra": { + "branch-alias": { + "dev-master": "2.2-dev" + } + }, + "autoload": { + "psr-4": { + "FOS\\RestBundle\\": "" + }, + "exclude-from-classmap": [ + "Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Lukas Kahwe Smith", + "email": "smith@pooteeweet.org" + }, + { + "name": "FriendsOfSymfony Community", + "homepage": "https://github.com/friendsofsymfony/FOSRestBundle/contributors" + }, + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com" + } + ], + "description": "This Bundle provides various tools to rapidly develop RESTful API's with Symfony", + "homepage": "http://friendsofsymfony.github.com", + "keywords": [ + "rest" + ], + "time": "2017-04-06T12:55:03+00:00" + }, + { + "name": "guzzlehttp/guzzle", + "version": "6.3.0", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "f4db5a78a5ea468d4831de7f0bf9d9415e348699" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/f4db5a78a5ea468d4831de7f0bf9d9415e348699", + "reference": "f4db5a78a5ea468d4831de7f0bf9d9415e348699", + "shasum": "" + }, + "require": { + "guzzlehttp/promises": "^1.0", + "guzzlehttp/psr7": "^1.4", + "php": ">=5.5" + }, + "require-dev": { + "ext-curl": "*", + "phpunit/phpunit": "^4.0 || ^5.0", + "psr/log": "^1.0" + }, + "suggest": { + "psr/log": "Required for using the Log middleware" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.2-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2017-06-22T18:50:49+00:00" + }, + { + "name": "guzzlehttp/promises", + "version": "v1.3.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "time": "2016-12-20T10:07:11+00:00" + }, + { + "name": "guzzlehttp/psr7", + "version": "1.4.2", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/f5b8a8512e2b58b0071a7280e39f14f72e05d87c", + "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c", + "shasum": "" + }, + "require": { + "php": ">=5.4.0", + "psr/http-message": "~1.0" + }, + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Schultze", + "homepage": "https://github.com/Tobion" + } + ], + "description": "PSR-7 message implementation that also provides common utility methods", + "keywords": [ + "http", + "message", + "request", + "response", + "stream", + "uri", + "url" + ], + "time": "2017-03-20T17:10:46+00:00" + }, { "name": "incenteev/composer-parameter-handler", "version": "v2.1.2", @@ -1209,6 +1491,56 @@ ], "time": "2017-02-14T16:28:37+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" + ], + "time": "2016-08-06T14:39:51+00:00" + }, { "name": "psr/link", "version": "1.0.0", @@ -2247,6 +2579,98 @@ "templating" ], "time": "2017-07-04T13:19:31+00:00" + }, + { + "name": "willdurand/jsonp-callback-validator", + "version": "v1.1.0", + "source": { + "type": "git", + "url": "https://github.com/willdurand/JsonpCallbackValidator.git", + "reference": "1a7d388bb521959e612ef50c5c7b1691b097e909" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/willdurand/JsonpCallbackValidator/zipball/1a7d388bb521959e612ef50c5c7b1691b097e909", + "reference": "1a7d388bb521959e612ef50c5c7b1691b097e909", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "~3.7" + }, + "type": "library", + "autoload": { + "psr-0": { + "JsonpCallbackValidator": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "William Durand", + "email": "william.durand1@gmail.com", + "homepage": "http://www.willdurand.fr" + } + ], + "description": "JSONP callback validator.", + "time": "2014-01-20T22:35:06+00:00" + }, + { + "name": "willdurand/negotiation", + "version": "v2.3.1", + "source": { + "type": "git", + "url": "https://github.com/willdurand/Negotiation.git", + "reference": "03436ededa67c6e83b9b12defac15384cb399dc9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/willdurand/Negotiation/zipball/03436ededa67c6e83b9b12defac15384cb399dc9", + "reference": "03436ededa67c6e83b9b12defac15384cb399dc9", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.3-dev" + } + }, + "autoload": { + "psr-4": { + "Negotiation\\": "src/Negotiation" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "William Durand", + "email": "will+git@drnd.me" + } + ], + "description": "Content Negotiation tools for PHP provided as a standalone library.", + "homepage": "http://williamdurand.fr/Negotiation/", + "keywords": [ + "accept", + "content", + "format", + "header", + "negotiation" + ], + "time": "2017-05-14T17:21:12+00:00" } ], "packages-dev": [ @@ -2373,10 +2797,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=5.5.9" + "php": "^7.0" }, - "platform-dev": [], - "platform-overrides": { - "php": "5.5.9" - } + "platform-dev": [] } diff --git a/docker-compose.override.dist.yml b/docker-compose.override.dist.yml index 4e9c5c5..d61993c 100644 --- a/docker-compose.override.dist.yml +++ b/docker-compose.override.dist.yml @@ -5,7 +5,3 @@ front: db: ports: - 3306:3306 - -mailcatcher: - ports: - - 1080:1080 \ No newline at end of file diff --git a/src/AppBundle/Controller/ArticleController.php b/src/AppBundle/Controller/ArticleController.php new file mode 100644 index 0000000..83b7ebb --- /dev/null +++ b/src/AppBundle/Controller/ArticleController.php @@ -0,0 +1,141 @@ +getDoctrine() + ->getRepository(Article::class) + ->findAllOrderedByTitle(); + + /*$formatted = []; + + foreach($articles as $article) { + $formatted[] = [ + 'id' => $article->getId(), + 'title' => $article->getTitle(), + 'content' => $article->getContent(), + 'author' => $article->getAuthor() + ]; + }*/ + + return $articles; + } + + + /** + * @Rest\View(serializerGroups = {"article"}) + * + * @param Request $request + * @param $id + * @return Article|null|object|JsonResponse + */ + public function getArticleAction($id, Request $request) + { + $article = $this->getDoctrine() + ->getRepository(Article::class) + ->find($request->get('id')); + + if (empty($article)) { + return new JsonResponse(['message' => 'Article not found'], Response::HTTP_NOT_FOUND); + } + + return $article; + } + + /** + * @Rest\View(statusCode = Response::HTTP_CREATED, serializerGroups = {"article"}) + * @Rest\Post("/articles", name = "new_article", options = {"method_prefix" = false} ) + * + * @param Request $request + * @return Article + */ + public function postArticlesAction(Request $request) + { + $article = new Article(); + $form = $this->createForm(ArticleType::class, $article); + + + if ($form->isValid()) { + //$form->submit($request->request->all()); + $form->handleRequest($request); + + if ($form->isValid()) { + $article = $form->getData(); + $em = $this->getDoctrine()->getManager(); + $em->persist($article); + $em->flush(); + return $article; + } + + return $form; + } + } + + /** + * @Rest\View(statusCode = Response::HTTP_NO_CONTENT, serializerGroups = {"article"}) + * + * @param Request $request + */ + public + function deleteArticleAction($id, Request $request) + { + $em = $this->getDoctrine()->getManager(); + $article = $em->getRepository(Article::class) + ->find($request->get('id')); + + if ($article) { + $em->remove($article); + $em->flush(); + } + } + + /** + * @Rest\View(serializerGroups = {"article"}) + * + * @param $id + * @param Request $request + * @return Article|null|object|\Symfony\Component\Form\Form|JsonResponse + */ + public + function putArticleAction($id, Request $request) + { + $article = $this->getDoctrine() + ->getRepository(Article::class) + ->find($request->get('id')); + + if (empty($article)) { + return new JsonResponse(['message' => 'Article not found'], Response::HTTP_NOT_FOUND); + } + + $form = $this->createForm(ArticleType::class, $article); + $form->submit($request->request->all()); + + if ($form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->flush(); + return $article; + } else { + return $form; + } + } + } \ No newline at end of file diff --git a/src/AppBundle/Controller/BlogController.php b/src/AppBundle/Controller/BlogController.php deleted file mode 100644 index 5d9cd83..0000000 --- a/src/AppBundle/Controller/BlogController.php +++ /dev/null @@ -1,59 +0,0 @@ - $articles, - ]; - } - - /** - * @Route("/test/specific/{page}", name="specific", requirements={"page": "\d+"}) - * - * @param Request $request - * @param int $page - * - * @return \Symfony\Component\HttpFoundation\Response - */ - public function pageAction(Request $request, $page) - { - return $this->render('/test/page.html.twig', array( - 'page' => $page, - )); - } -} \ No newline at end of file diff --git a/src/AppBundle/Controller/CommentController.php b/src/AppBundle/Controller/CommentController.php new file mode 100644 index 0000000..cb18e56 --- /dev/null +++ b/src/AppBundle/Controller/CommentController.php @@ -0,0 +1,89 @@ +getDoctrine() + ->getRepository(Article::class) + ->find($request->get('id')); + + if(empty($article)) { + return $this->placeNotFound(); + } + + return $article->getComments(); + } + + /** + * @Rest\View(serializerGroups = {"comment"}) + * @Rest\Post("/articles/{id}/comments") + * + * @param Request $request + * @return View + */ + public function postCommentsAction(Request $request) + { + $article = $this->getDoctrine() + ->getRepository(Article::class) + ->find($request->get('id')); + + if (empty($article)) { + return $this->placeNotFound(); + } + + $comment = new Comment(); + $comment->setArticle($article); + $form = $this->createForm(CommentType::class, $comment); + $form->submit($request->request->all()); + + if ($form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($comment); + $em->flush(); + return $comment; + } + + return $form; + } + + /** + * @return View + */ + private function placeNotFound() + { + return View::create(['message' => 'Place not found'], Response::HTTP_NOT_FOUND); + } +} \ No newline at end of file diff --git a/src/AppBundle/Controller/DefaultController.php b/src/AppBundle/Controller/DefaultController.php index cecd334..79f95fa 100644 --- a/src/AppBundle/Controller/DefaultController.php +++ b/src/AppBundle/Controller/DefaultController.php @@ -2,15 +2,20 @@ namespace AppBundle\Controller; +use AppBundle\Form\Type\ArticleType; +use GuzzleHttp\Client; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; +use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; class DefaultController extends Controller { + /** * @Route("/", name="homepage") - */ + public function indexAction(Request $request) { // replace this example code with whatever you need @@ -18,4 +23,44 @@ public function indexAction(Request $request) 'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR, ]); } + * */ + + + /** + * @Route("/new_article", name = "new_article_form") + * + * @Template + * + * @param Request $request + * @return array + */ + public function newArticleAction(Request $request) + { + $form = $this->createForm(ArticleType::class); + return ['form' => $form->createView()]; + } + + /** + * @Route("/show_articles", name = "show_articles") + * + * @Template + * + * @param Request $request + * @return array + */ + public function showAll(Request $request) + { + $client = new Client(); + $res = $client->request( + 'GET', + 'db'.$this->generateUrl('get_articles', [], UrlGeneratorInterface::ABSOLUTE_PATH) + ); + + $articles = $res->getBody(); + + return [ + 'articles' => $articles, + 'count' => count($articles) + ]; + } } diff --git a/src/AppBundle/Controller/UserController.php b/src/AppBundle/Controller/UserController.php new file mode 100644 index 0000000..db107aa --- /dev/null +++ b/src/AppBundle/Controller/UserController.php @@ -0,0 +1,166 @@ +getDoctrine() + ->getRepository(User::class) + ->findAll(); + + /*$formatted = []; + + foreach($users as $user) { + $formatted[] = [ + 'id' => $user->getId(), + 'nom' => $user->getNom(), + 'prenom' => $user->getPrenom(), + ]; + }*/ + + return $users; + } + + /** + * @Rest\View + * + * @param $id + * @param Request $request + * @return User|object|JsonResponse + */ + public function getUserAction($id, Request $request) + { + $user = $this->getDoctrine() + ->getRepository(User::class) + ->find($request->get('id')); + + if(empty($user)) { + return new JsonResponse(['message' => 'User not found'], Response::HTTP_NOT_FOUND); + } + + return $user; + } + + /** + * @Rest\View(statusCode = Response::HTTP_CREATED) + * + * @param Request $request + * @return User|\Symfony\Component\Form\Form + */ + public function postUsersAction(Request $request) + { + $user = new User(); + $form = $this->createForm(UserType::class, $user); + + $form->submit($request->request->all()); + + if($form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($user); + $em->flush(); + return $user; + } + + return $form; + } + + /** + * @Rest\View(statusCode = Response::HTTP_NO_CONTENT) + * @Rest\Put("/users/{id}") + * + * @param $id + * @param Request $request + */ + public function deleteUserAction($id, Request $request) + { + $em = $this->getDoctrine()->getManager(); + $user = $em->getRepository(User::class) + ->find($request->get('id')); + + if($user) { + $em->remove($user); + $em->flush(); + } + } + + /** + * @Rest\View + * @Rest\Put("/users/{id}") + * + * @param Request $request + * @return User|object|\Symfony\Component\Form\Form|JsonResponse + */ + public function putUserAction(Request $request) + { + return $this->updateUser($request, true); + } + + /** + * @Rest\View + * @Rest\Patch("/users/{id}") + * + * @param Request $request + * @return User|object|\Symfony\Component\Form\Form|JsonResponse + */ + public function patchUserAction(Request $request) + { + return $this->updateUser($request, false); + } + + /** + * @param Request $request + * @param bool $clearMissing + * + * @return User|object|\Symfony\Component\Form\Form|JsonResponse + */ + public function updateUser(Request $request, $clearMissing) + { + $user = $this->getDoctrine() + ->getRepository(User::class) + ->find($request->get('id')); + + if (empty($user)) { + return new JsonResponse(['message' => 'User not found'], Response::HTTP_NOT_FOUND); + } + + $form = $this->createForm(UserType::class, $user); + $form->submit($request->request->all(), $clearMissing); + + if ($form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($user); + $em->flush(); + return $user; + } + + return $form; + } +} \ No newline at end of file diff --git a/src/AppBundle/Entity/Article.php b/src/AppBundle/Entity/Article.php new file mode 100644 index 0000000..016da67 --- /dev/null +++ b/src/AppBundle/Entity/Article.php @@ -0,0 +1,145 @@ + + * + * @ORM\Entity(repositoryClass = "AppBundle\Repository\ArticleRepository") + * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name = "articles_title_unique", columns = {"title"})}) + */ +class Article +{ + /** + * @var int + * + * @ORM\Id + * @ORM\GeneratedValue + * @ORM\Column(type = "integer") + * + * @Groups({"comment", "article"}) + */ + protected $id; + + /** + * @var string + * + * @ORM\Column( + * type = "string", + * length = 100 + * ) + * + * @Assert\NotBlank + * + * @Groups({"comment", "article"}) + */ + protected $title; + + /** + * @var string + * + * @ORM\Column(type = "string") + * + * @Assert\NotBlank + * + * @Groups({"comment", "article"}) + */ + protected $content; + + /** + * @var string + * + * @ORM\Column( + * type = "string", + * length = 100 + * ) + * + * @Assert\NotBlank + * + * @Groups({"comment", "article"}) + */ + protected $author; + + /** + * @var Comment[] + * + * @ORM\OneToMany(targetEntity = "Comment", mappedBy = "article") + * + * @Groups({"article"}) + */ + protected $comments; + + public function __construct() + { + $this->comments = new ArrayCollection(); + } + + /** + * @return int + */ + public function getId() + { + return $this->id; + } + + /** + * @return string + */ + public function getTitle() + { + return $this->title; + } + + /** + * @param string $title + */ + public function setTitle($title) + { + $this->title = $title; + } + + /** + * @return string + */ + public function getContent() + { + return $this->content; + } + + /** + * @param string $content + */ + public function setContent($content) + { + $this->content = $content; + } + + /** + * @return string + */ + public function getAuthor() + { + return $this->author; + } + + /** + * @param string $author + */ + public function setAuthor($author) + { + $this->author = $author; + } + + /** + * @return Comment[] + */ + public function getComments() + { + return $this->comments; + } +} \ No newline at end of file diff --git a/src/AppBundle/Entity/Comment.php b/src/AppBundle/Entity/Comment.php new file mode 100644 index 0000000..7a10592 --- /dev/null +++ b/src/AppBundle/Entity/Comment.php @@ -0,0 +1,111 @@ +id; + } + + /** + * @return string + */ + public function getAuthor() + { + return $this->author; + } + + /** + * @param string $author + */ + public function setAuthor($author) + { + $this->author = $author; + } + + /** + * @return string + */ + public function getContent() + { + return $this->content; + } + + /** + * @param string $content + */ + public function setContent($content) + { + $this->content = $content; + } + + /** + * @return Article + */ + public function getArticle() + { + return $this->article; + } + + /** + * @param Article $article + */ + public function setArticle($article) + { + $this->article = $article; + } +} \ No newline at end of file diff --git a/src/AppBundle/Entity/User.php b/src/AppBundle/Entity/User.php new file mode 100644 index 0000000..dfb7460 --- /dev/null +++ b/src/AppBundle/Entity/User.php @@ -0,0 +1,88 @@ + + * + * @ORM\Entity + */ +class User +{ + /** + * @var int + * + * @ORM\Id + * @ORM\GeneratedValue + * @ORM\Column(type = "integer") + */ + protected $id; + + /** + * @var string + * + * @ORM\Column( + * type = "string", + * length = 100 + * ) + */ + protected $nom; + + /** + * @var string + * + * @ORM\Column( + * type = "string", + * length = 100 + * ) + */ + protected $prenom; + + /** + * @return int + */ + public function getId() + { + return $this->id; + } + + /** + * @return string + */ + public function getNom() + { + return $this->nom; + } + + /** + * @param string $nom + */ + public function setNom($nom) + { + $this->nom = $nom; + } + + /** + * @return string + */ + public function getPrenom() + { + return $this->prenom; + } + + /** + * @param string $prenom + */ + public function setPrenom($prenom) + { + $this->prenom = $prenom; + } +} \ No newline at end of file diff --git a/src/AppBundle/Form/Type/ArticleType.php b/src/AppBundle/Form/Type/ArticleType.php new file mode 100644 index 0000000..91de753 --- /dev/null +++ b/src/AppBundle/Form/Type/ArticleType.php @@ -0,0 +1,28 @@ +add('title') + ->add('content') + ->add('author'); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults( + [ + 'data_class' => 'AppBundle\Entity\Article', + 'csrf_protection' => false + ] + ); + } +} \ No newline at end of file diff --git a/src/AppBundle/Form/Type/CommentType.php b/src/AppBundle/Form/Type/CommentType.php new file mode 100644 index 0000000..2f35d98 --- /dev/null +++ b/src/AppBundle/Form/Type/CommentType.php @@ -0,0 +1,34 @@ +add('content') + ->add('author'); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults( + [ + 'data_class' => 'AppBundle\Entity\Comment', + 'csrf_protection' => false + ] + ); + } +} \ No newline at end of file diff --git a/src/AppBundle/Form/Type/UserType.php b/src/AppBundle/Form/Type/UserType.php new file mode 100644 index 0000000..1590ddd --- /dev/null +++ b/src/AppBundle/Form/Type/UserType.php @@ -0,0 +1,25 @@ +add('nom'); + $builder->add('prenom'); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => 'AppBundle\Entity\User', + 'csrf_protection' => false + ]); + } +} \ No newline at end of file diff --git a/src/AppBundle/Repository/ArticleRepository.php b/src/AppBundle/Repository/ArticleRepository.php new file mode 100644 index 0000000..9774c6a --- /dev/null +++ b/src/AppBundle/Repository/ArticleRepository.php @@ -0,0 +1,16 @@ +createQueryBuilder('article') + ->orderBy('article.title', 'ASC') + ->getQuery() + ->getResult(); + } +} \ No newline at end of file diff --git a/src/AppBundle/Resources/views/Blog/list.html.twig b/src/AppBundle/Resources/views/Blog/list.html.twig deleted file mode 100644 index 7f3f97e..0000000 --- a/src/AppBundle/Resources/views/Blog/list.html.twig +++ /dev/null @@ -1,2 +0,0 @@ -

Welcome

-liste des articles \ No newline at end of file diff --git a/src/AppBundle/Resources/views/Blog/liste.html.twig b/src/AppBundle/Resources/views/Blog/liste.html.twig deleted file mode 100644 index ec9d8d9..0000000 --- a/src/AppBundle/Resources/views/Blog/liste.html.twig +++ /dev/null @@ -1,7 +0,0 @@ -

liste des articles

- -accueil \ No newline at end of file diff --git a/src/AppBundle/Resources/views/Blog/new.html.twig b/src/AppBundle/Resources/views/Blog/new.html.twig new file mode 100644 index 0000000..8e4e629 --- /dev/null +++ b/src/AppBundle/Resources/views/Blog/new.html.twig @@ -0,0 +1,4 @@ +{{ form_start(form) }} +{{ form_widget(form) }} + +{{ form_end(form) }} diff --git a/src/AppBundle/Resources/views/Blog/page.html.twig b/src/AppBundle/Resources/views/Blog/page.html.twig new file mode 100644 index 0000000..e8c74a2 --- /dev/null +++ b/src/AppBundle/Resources/views/Blog/page.html.twig @@ -0,0 +1,5 @@ +

{{ article.title }}

+

{{ article.content }}

+

by {{ article.author }}

+accueil +retour \ No newline at end of file diff --git a/src/AppBundle/Resources/views/Blog/success.html.twig b/src/AppBundle/Resources/views/Blog/success.html.twig new file mode 100644 index 0000000..8ad72c3 --- /dev/null +++ b/src/AppBundle/Resources/views/Blog/success.html.twig @@ -0,0 +1 @@ +

GG WP

\ No newline at end of file diff --git a/src/AppBundle/Resources/views/Default/index.html.twig b/src/AppBundle/Resources/views/Default/index.html.twig new file mode 100644 index 0000000..e69de29 diff --git a/src/AppBundle/Resources/views/Default/newArticle.html.twig b/src/AppBundle/Resources/views/Default/newArticle.html.twig new file mode 100644 index 0000000..2ce1fce --- /dev/null +++ b/src/AppBundle/Resources/views/Default/newArticle.html.twig @@ -0,0 +1,4 @@ +{{ form_start(form, {'action': path('new_article'), 'method': 'POST'}) }} +{{ form_widget(form) }} + +{{ form_end(form) }} \ No newline at end of file diff --git a/src/AppBundle/Resources/views/Default/showAll.html.twig b/src/AppBundle/Resources/views/Default/showAll.html.twig new file mode 100644 index 0000000..366b97d --- /dev/null +++ b/src/AppBundle/Resources/views/Default/showAll.html.twig @@ -0,0 +1,6 @@ +

Liste des articles ({{ count }})

+ \ No newline at end of file