Merge pull request #21 from SundownDEV/firstQuestion

First question route
devFront
Raphael Cerveaux 2018-06-06 11:25:21 +02:00 committed by GitHub
commit 478c77776b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 75 deletions

View File

@ -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']);
}
}

View File

@ -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();

View File

@ -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()
;
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}