1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace VSV\GVQ_API\Question\Controllers; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use VSV\GVQ_API\Question\Models\Question; |
8
|
|
|
use VSV\GVQ_API\Question\Repositories\QuestionRepository; |
9
|
|
|
use VSV\GVQ_API\Question\Serializers\QuestionSerializer; |
10
|
|
|
use VSV\GVQ_API\Question\Serializers\QuestionsSerializer; |
11
|
|
|
|
12
|
|
|
class QuestionController |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var QuestionRepository |
16
|
|
|
*/ |
17
|
|
|
private $questionRepository; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var QuestionSerializer |
21
|
|
|
*/ |
22
|
|
|
private $questionSerializer; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var QuestionsSerializer |
26
|
|
|
*/ |
27
|
|
|
private $questionsSerializer; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param QuestionRepository $questionRepository |
31
|
|
|
* @param QuestionSerializer $questionSerializer |
32
|
|
|
* @param QuestionsSerializer $questionsSerializer |
33
|
|
|
*/ |
34
|
|
|
public function __construct( |
35
|
|
|
QuestionRepository $questionRepository, |
36
|
|
|
QuestionSerializer $questionSerializer, |
37
|
|
|
QuestionsSerializer $questionsSerializer |
38
|
|
|
) { |
39
|
|
|
$this->questionRepository = $questionRepository; |
40
|
|
|
$this->questionSerializer = $questionSerializer; |
41
|
|
|
$this->questionsSerializer = $questionsSerializer; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Request $request |
46
|
|
|
* @return Response |
47
|
|
|
*/ |
48
|
|
|
public function save(Request $request): Response |
49
|
|
|
{ |
50
|
|
|
$json = $request->getContent(); |
51
|
|
|
/** @var Question $question */ |
52
|
|
|
$question = $this->questionSerializer->deserialize($json, Question::class, 'json'); |
53
|
|
|
$this->questionRepository->save($question); |
54
|
|
|
|
55
|
|
|
$response = new Response('{"id":"'.$question->getId()->toString().'"}'); |
56
|
|
|
$response->headers->set('Content-Type', 'application/json'); |
57
|
|
|
|
58
|
|
|
return $response; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return Response |
63
|
|
|
*/ |
64
|
|
|
public function getAll(): Response |
65
|
|
|
{ |
66
|
|
|
$questions = $this->questionRepository->getAll(); |
67
|
|
|
$questionsAsJson = $this->questionsSerializer->serialize($questions, 'json'); |
68
|
|
|
|
69
|
|
|
$response = new Response($questionsAsJson); |
70
|
|
|
$response->headers->set('Content-Type', 'application/json'); |
71
|
|
|
|
72
|
|
|
return $response; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|