diff --git a/assets/app.js b/assets/app.js index 0f055a2..6b810b6 100644 --- a/assets/app.js +++ b/assets/app.js @@ -9,5 +9,4 @@ import './scss/app.scss'; // start the Stimulus application -import './bootstrap'; import * as mdb from 'mdb-ui-kit' diff --git a/assets/scss/app.scss b/assets/scss/app.scss index e9a2eba..45f0625 100644 --- a/assets/scss/app.scss +++ b/assets/scss/app.scss @@ -1,2 +1 @@ -@import "~bootstrap/scss/bootstrap"; -@import '~mdb-ui-kit/src/scss/mdb.core'; +@import '~mdb-ui-kit/src/scss/mdb.free'; diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index 29241b2..01cdf43 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -2,8 +2,12 @@ namespace App\Controller; +use App\Entity\Comment; use App\Entity\Post; +use App\Form\CommentType; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; @@ -11,14 +15,35 @@ class PostController extends AbstractController { /** * @Route("/post/{slug}", name="post") - * @param string $slug + * @param Request $request + * @param string $slug * @return Response */ - public function index(string $slug): Response + public function index(Request $request, string $slug): Response { + $post = $this->getDoctrine()->getRepository(Post::class)->findOneBy(array('slug' => $slug)); + $form = $this->formGenerator($post); + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $comment = $form->getData(); + $comment->setCreatedAt(new \DateTime()); + $comment->setValid(true); + $manager = $this->getDoctrine()->getManager(); + $manager->persist($comment); + $manager->flush(); + $form = $this->formGenerator($post); + } return $this->render('post/index.html.twig', [ 'controller_name' => 'PostController', - 'post' => $this->getDoctrine()->getRepository(Post::class)->findOneBy(array('slug' => $slug)) + 'post' => $post, + 'form' => $form->createView() ]); } + + private function formGenerator(Post $post) : FormInterface + { + $comment = new Comment(); + $comment->setPost($post); + return $this->createForm(CommentType::class, $comment); + } } diff --git a/src/Form/CommentType.php b/src/Form/CommentType.php new file mode 100644 index 0000000..00a6761 --- /dev/null +++ b/src/Form/CommentType.php @@ -0,0 +1,30 @@ +add('username', TextType::class, ['attr' => ['class' => 'form-control']]) + ->add('content', TextareaType::class, ['attr' => ['class' => 'form-control']]) + ->add('comment', SubmitType::class, ['attr' => ['class' => 'btn btn-primary btn-block']]) + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => Comment::class, + ]); + } +} diff --git a/templates/base.html.twig b/templates/base.html.twig index 4f9afb3..3b2ae04 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -12,7 +12,7 @@