1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace VSV\GVQ_API\Question\Controllers; |
4
|
|
|
|
5
|
|
|
use Ramsey\Uuid\UuidFactoryInterface; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use VSV\GVQ_API\Common\ValueObjects\Languages; |
10
|
|
|
use VSV\GVQ_API\Image\Controllers\ImageController; |
11
|
|
|
use VSV\GVQ_API\Question\Forms\QuestionFormDTO; |
12
|
|
|
use VSV\GVQ_API\Question\Forms\QuestionFormType; |
13
|
|
|
use VSV\GVQ_API\Question\Repositories\CategoryRepository; |
14
|
|
|
use VSV\GVQ_API\Question\Repositories\QuestionRepository; |
15
|
|
|
|
16
|
|
|
class QuestionViewController extends AbstractController |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var UuidFactoryInterface |
20
|
|
|
*/ |
21
|
|
|
private $uuidFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var QuestionRepository |
25
|
|
|
*/ |
26
|
|
|
private $questionRepository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var CategoryRepository |
30
|
|
|
*/ |
31
|
|
|
private $categoryRepository; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ImageController |
35
|
|
|
*/ |
36
|
|
|
private $imageController; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param UuidFactoryInterface $uuidFactory |
40
|
|
|
* @param QuestionRepository $questionRepository |
41
|
|
|
* @param CategoryRepository $categoryRepository |
42
|
|
|
* @param ImageController $imageController |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
UuidFactoryInterface $uuidFactory, |
46
|
|
|
QuestionRepository $questionRepository, |
47
|
|
|
CategoryRepository $categoryRepository, |
48
|
|
|
ImageController $imageController |
49
|
|
|
) { |
50
|
|
|
$this->uuidFactory = $uuidFactory; |
51
|
|
|
$this->questionRepository = $questionRepository; |
52
|
|
|
$this->categoryRepository = $categoryRepository; |
53
|
|
|
$this->imageController = $imageController; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Response |
58
|
|
|
*/ |
59
|
|
|
public function index(): Response |
60
|
|
|
{ |
61
|
|
|
$questions = $this->questionRepository->getAll(); |
62
|
|
|
|
63
|
|
|
return $this->render( |
64
|
|
|
'questions/index.html.twig', |
65
|
|
|
[ |
66
|
|
|
'questions' => $questions ? $questions->toArray() : [], |
67
|
|
|
] |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Request $request |
73
|
|
|
* @return Response |
74
|
|
|
* @throws \League\Flysystem\FileExistsException |
75
|
|
|
*/ |
76
|
|
|
public function add(Request $request): Response |
77
|
|
|
{ |
78
|
|
|
$categories = $this->categoryRepository->getAll(); |
79
|
|
|
$questionFormDTO = new QuestionFormDTO(); |
80
|
|
|
|
81
|
|
|
$form = $this->createForm( |
82
|
|
|
QuestionFormType::class, |
83
|
|
|
$questionFormDTO, |
84
|
|
|
[ |
85
|
|
|
'languages' => new Languages(), |
86
|
|
|
'categories' => $categories, |
87
|
|
|
] |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$form->handleRequest($request); |
91
|
|
|
|
92
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
93
|
|
|
$fileName = $this->imageController->handleImage($questionFormDTO->image); |
94
|
|
|
|
95
|
|
|
$question = $questionFormDTO->toQuestion( |
96
|
|
|
$this->uuidFactory, |
97
|
|
|
$fileName |
98
|
|
|
); |
99
|
|
|
$this->questionRepository->save($question); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->render( |
103
|
|
|
'questions/add.html.twig', |
104
|
|
|
[ |
105
|
|
|
'categories' => $categories ? $categories->toArray() : [], |
106
|
|
|
'form' => $form->createView() |
107
|
|
|
] |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param Request $request |
113
|
|
|
* @param string $id |
114
|
|
|
* @return Response |
115
|
|
|
*/ |
116
|
|
|
public function delete(Request $request, string $id): Response |
117
|
|
|
{ |
118
|
|
|
if ($request->getMethod() === 'POST') { |
119
|
|
|
$this->questionRepository->delete( |
120
|
|
|
$this->uuidFactory->fromString($id) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
return $this->redirectToRoute('questions_view_index'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this->render( |
127
|
|
|
'questions/delete.html.twig', |
128
|
|
|
[ |
129
|
|
|
'id' => $id, |
130
|
|
|
] |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|