First question route
parent
1d70d79704
commit
48251a0c9d
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Question;
|
||||
use App\Form\QuestionType;
|
||||
use App\Repository\QuestionRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
|
||||
/**
|
||||
* @Route("/")
|
||||
*/
|
||||
class QuestionApiController extends Controller
|
||||
{
|
||||
/**
|
||||
* @Route("/questions/first", name="api_questions_first")
|
||||
*/
|
||||
public function getFirst(QuestionRepository $questionRepository)
|
||||
{
|
||||
$data = $questionRepository->findFirst();
|
||||
|
||||
return JsonResponse::create($data, 200, ['Content-Type' => 'application/json']);
|
||||
}
|
||||
}
|
|
@ -7,11 +7,15 @@ use Doctrine\Common\Collections\Collection;
|
|||
use Doctrine\ORM\Mapping as ORM;
|
||||
use ApiPlatform\Core\Annotation\ApiResource;
|
||||
use ApiPlatform\Core\Annotation\ApiSubresource;
|
||||
use App\Service\QuestionAPI;
|
||||
|
||||
/**
|
||||
* @ApiResource(
|
||||
* collectionOperations={"get"={"method"="GET"}},
|
||||
* itemOperations={"get"={"method"="GET"}}
|
||||
* itemOperations={
|
||||
* "get"={"method"="GET"},
|
||||
* "special"={"route_name"="api_questions_first", "requirements"={}}
|
||||
* }
|
||||
* )
|
||||
* @ORM\Table(name="question")
|
||||
* @ORM\Entity(repositoryClass="App\Repository\QuestionRepository")
|
||||
|
@ -53,13 +57,6 @@ class Question
|
|||
$this->date = new \DateTime();
|
||||
}
|
||||
|
||||
/*public function __invoke(Question $question): Question
|
||||
{
|
||||
$this->testSerializer->test();
|
||||
|
||||
return $question;
|
||||
}*/
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->getText();
|
||||
|
|
|
@ -22,14 +22,13 @@ class QuestionRepository extends ServiceEntityRepository
|
|||
/**
|
||||
* @return Question[] Returns an array of Question objects
|
||||
*/
|
||||
public function findByToken($token)
|
||||
public function findFirst()
|
||||
{
|
||||
return $this->createQueryBuilder('q')
|
||||
->andWhere('q.token = :token')
|
||||
->setParameter('token', $token)
|
||||
->orderBy('q.id', 'ASC')
|
||||
->setMaxResults(1)
|
||||
->getQuery()
|
||||
->getResult()
|
||||
->getArrayResult()
|
||||
;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: sundowndev
|
||||
* Date: 30/05/18
|
||||
* Time: 17:20
|
||||
*/
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Response;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
|
||||
class ResponseService
|
||||
{
|
||||
//private $object;
|
||||
private $response;
|
||||
|
||||
public function __construct(Response $response)
|
||||
{
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
public function persistResponses(array $data, $question)
|
||||
{
|
||||
$entityManager = $this->getDoctrine()->getManager();
|
||||
|
||||
foreach ($data as $text) {
|
||||
$entity = new Response();
|
||||
$entity->setText($text);
|
||||
$entity->setQuestion($question);
|
||||
|
||||
// tell Doctrine you want to (eventually) save the Product (no queries yet)
|
||||
$entityManager->persist($entity);
|
||||
|
||||
// actually executes the queries (i.e. the INSERT query)
|
||||
$entityManager->flush();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: sundowndev
|
||||
* Date: 30/05/18
|
||||
* Time: 16:31
|
||||
*/
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
||||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
||||
|
||||
|
||||
class TestSerializer
|
||||
{
|
||||
public function test ($data, Serializer $serializer)
|
||||
{
|
||||
return $serializer->denormalize($data);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue