Commit 4ac68c6c authored by Mat's avatar Mat

message, vérification d'une seconde route de l'API/v2 en POST avec postman

parent 152f04f0
......@@ -5,12 +5,14 @@ namespace Api2Bundle\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use SocketBundle\Entity\Message;
use SocketBundle\Form\MessageType;
use Symfony\Component\HttpFoundation\Request;
class MessageController extends FOSRestController
{
/**
* @Rest\Get("/message")
* @Rest\Get(path="/message")
* @Rest\View(serializerGroups={"getMessage"})
*/
public function getAction()
......@@ -19,4 +21,26 @@ class MessageController extends FOSRestController
->getRepository(Message::class)
->findAll();
}
/**
* @param Request $request
* @Rest\Post(path="/message")
* @Rest\View()
* @return \Symfony\Component\Form\FormInterface
*/
public function postAction(Request $request)
{
$message = new Message();
$form = $this->createForm(MessageType::class, $message);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em = $this->getDoctrine()->getManager();
$message->setDate(new \DateTime()); // c'est mieux de mettre un prepersist dans l'entité
$em->persist($message);
$em->flush();
}
return $form;
}
}
......@@ -4,6 +4,7 @@ namespace SocketBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Message
......@@ -44,6 +45,7 @@ class Message
* @var string
* @ORM\Column(name="content", type="text", nullable=false, unique=false)
* @Serializer\Groups({"getMessage"})
* @Assert\Length(min="2", minMessage="test")
*/
private $content;
......
<?php
namespace SocketBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class MessageType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('content')
->add('author')
->add('receiver')
;
}/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'SocketBundle\Entity\Message',
'csrf_protection' => false
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return ''; // pas nécessaire selon Khun, sauf si forms imbriqués
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment