post form + controller

pull/11/head^2
remrem 5 months ago
parent 82ca68fef0
commit a07327dbc5

@ -23,10 +23,10 @@ APP_SECRET=5e7ed9de1fd633f917d0e87e2e05f923
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=8.0.32&charset=utf8mb4"
# DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=10.11.2-MariaDB&charset=utf8mb4"
DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=16&charset=utf8"
# DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=16&charset=utf8"
###< doctrine/doctrine-bundle ###
###> symfony/messenger ###

@ -11,6 +11,9 @@ services:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
Symfony\Component\Serializer\Serializer:
autowire: true
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:

@ -7,9 +7,11 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Post;
use App\Form\Type\PostType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Serializer\Serializer;
class PostController {
class PostController extends AbstractController {
private EntityManagerInterface $em;
private Serializer $serializer;
@ -20,7 +22,7 @@ class PostController {
$this->serializer = $serializer;
}
#[Route('/post/{id}', name: 'display post', methods: ['GET'])]
#[Route('/post/{id}', name: 'display post', methods: ['POST'])]
public function getPost(int $id): Response
{
$post = $this->em->getRepository(Post::class)->find($id);
@ -33,22 +35,28 @@ class PostController {
return new Response();
}
#[Route('/post/', name: 'add_post', methods: ['POST'])]
#[Route('/post/new/', name: 'add_post', methods: ['GET','POST'])]
public function addPost(Request $request) :Response
{
$data = json_decode($request->getContent(), true);
try {
$post = $this->serializer->deserialize($data, Post::class, 'json');
} catch (\Exception) {
return new Response("Invalid JSON data", Response::HTTP_BAD_REQUEST);
$post = new Post();
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$form = $form->getData();
$this->em->persist($post);
$this->em->flush();
}
return $this->render('post/new.html.twig', [
'form' => $form,
]);
# Handle error on data
$this->em->persist($post);
$this->em->flush();
return new Response();
}
#[Route('/post/{id}', name: 'remove_post', methods: ['DELETE'])]

@ -2,14 +2,12 @@
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\TagsRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: TagsRepository::class)]
#[ApiResource]
class Tags
{
#[ORM\Id]

@ -0,0 +1,27 @@
<?php
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class PostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('title', TextType::class)
->add('text', TextareaType::class)
->add('dream', CheckboxType::class)
// ->add('tags', ChoiceType::class, [
// "multiple" => true
// ])
->add('submit', SubmitType::class)
;
}
}

@ -0,0 +1 @@
{{ form(form) }}
Loading…
Cancel
Save