1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
8
|
|
|
use App\Entity\Book; |
9
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
10
|
|
|
use App\Repository\BookRepository; |
11
|
|
|
use Exception; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
14
|
|
|
|
15
|
|
|
final class BookController extends AbstractController |
16
|
|
|
{ |
17
|
|
|
#[Route('/library', name: 'library_home')] |
18
|
|
|
public function index(): Response |
19
|
|
|
{ |
20
|
|
|
return $this->render('book/home.html.twig', [ |
21
|
|
|
'app_env' => $_ENV['APP_ENV'] ?? null, |
22
|
|
|
'database_url' => $_ENV['DATABASE_URL'] ?? null, |
23
|
|
|
'controller_name' => 'BookController', |
24
|
|
|
]); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
#[Route('/library/create', name: 'library_create_get', methods: ['GET'])] |
28
|
|
|
public function init(): Response |
29
|
|
|
{ |
30
|
|
|
return $this->render('book/create.html.twig'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
#[Route('/library/create', name: 'library_create_post', methods: ['POST'])] |
34
|
|
|
public function createBook( |
35
|
|
|
Request $request, |
36
|
|
|
SessionInterface $session, |
|
|
|
|
37
|
|
|
ManagerRegistry $doctrine |
38
|
|
|
): Response { |
39
|
|
|
$entityManager = $doctrine->getManager(); |
40
|
|
|
$title = (string)$request->request->get('title'); |
41
|
|
|
$isbn = (string)$request->request->get('isbn'); |
42
|
|
|
$author = (string)$request->request->get('author'); |
43
|
|
|
$image = (string)$request->request->get('image'); |
44
|
|
|
|
45
|
|
|
$book = new Book(); |
46
|
|
|
$book->setTitle($title); |
47
|
|
|
$book->setIsbn($isbn); |
48
|
|
|
$book->setAuthor($author); |
49
|
|
|
$book->setimage($image); |
50
|
|
|
|
51
|
|
|
// tell Doctrine you want to (eventually) save the Book |
52
|
|
|
// (no queries yet) |
53
|
|
|
$entityManager->persist($book); |
54
|
|
|
|
55
|
|
|
// actually executes the queries (i.e. the INSERT query) |
56
|
|
|
$entityManager->flush(); |
57
|
|
|
$this->addFlash( |
58
|
|
|
'notice', |
59
|
|
|
'Boken "' . $title . ' " är inlagd i biblioteket!' |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
return $this->redirectToRoute('library_show_all'); |
63
|
|
|
} |
64
|
|
|
#[Route('/library/show', name: 'library_show_all', methods:['GET'])] |
65
|
|
|
public function showAllBook( |
66
|
|
|
BookRepository $bookRepository |
67
|
|
|
): Response { |
68
|
|
|
$books = $bookRepository->findAll(); |
69
|
|
|
$data = [ |
70
|
|
|
"books" => $books |
71
|
|
|
]; |
72
|
|
|
return $this->render('book/view.html.twig', $data); |
73
|
|
|
} |
74
|
|
|
#[Route('/library/show/{id}', name: 'library_by_id', methods:['GET'])] |
75
|
|
|
public function showBookById( |
76
|
|
|
BookRepository $bookRepository, |
77
|
|
|
int $id |
78
|
|
|
): Response { |
79
|
|
|
$book = $bookRepository->find($id); |
80
|
|
|
$data = [ |
81
|
|
|
"book" => $book |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
return $this->render('book/single-view.html.twig', $data); |
85
|
|
|
} |
86
|
|
|
#[Route('/library/delete', name: 'library_delete', methods:['POST'])] |
87
|
|
|
public function deleteBookById( |
88
|
|
|
ManagerRegistry $doctrine, |
89
|
|
|
Request $request |
90
|
|
|
): Response { |
91
|
|
|
$entityManager = $doctrine->getManager(); |
92
|
|
|
$id = (int) $request->request->get('id'); |
93
|
|
|
$book = $entityManager->getRepository(Book::class)->find($id); |
94
|
|
|
|
95
|
|
|
if (!$book) { |
96
|
|
|
throw $this->createNotFoundException( |
97
|
|
|
'No book found with id '.$id |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$entityManager->remove($book); |
102
|
|
|
$entityManager->flush(); |
103
|
|
|
$this->addFlash( |
104
|
|
|
'notice', |
105
|
|
|
'Boken "' . $book->getTitle() . ' " borttagen från biblioteket!' |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
return $this->redirectToRoute('library_show_all'); |
109
|
|
|
} |
110
|
|
|
#[Route('/library/update/{id}', name: 'library_update_get', methods:['GET'])] |
111
|
|
|
public function updateBookById( |
112
|
|
|
BookRepository $bookRepository, |
113
|
|
|
int $id |
114
|
|
|
): Response { |
115
|
|
|
$book = $bookRepository->find($id); |
116
|
|
|
$data = [ |
117
|
|
|
"book" => $book |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
return $this->render('book/update.html.twig', $data); |
121
|
|
|
} |
122
|
|
|
#[Route('/library/update', name: 'library_update_post', methods:['POST'])] |
123
|
|
|
public function updateBookPost( |
124
|
|
|
ManagerRegistry $doctrine, |
125
|
|
|
Request $request |
126
|
|
|
): Response { |
127
|
|
|
$entityManager = $doctrine->getManager(); |
128
|
|
|
$id = (int) $request->request->get('id'); |
129
|
|
|
$title = (string) $request->request->get('title'); |
130
|
|
|
$isbn = (string) $request->request->get('isbn'); |
131
|
|
|
$author = (string) $request->request->get('author'); |
132
|
|
|
$image = (string) $request->request->get('image'); |
133
|
|
|
$book = $entityManager->getRepository(Book::class)->find($id); |
134
|
|
|
if (!$book) { |
135
|
|
|
throw $this->createNotFoundException('No book found with id ' . $id); |
136
|
|
|
} |
137
|
|
|
$book->setTitle($title); |
138
|
|
|
$book->setIsbn($isbn); |
139
|
|
|
$book->setAuthor($author); |
140
|
|
|
$book->setimage($image); |
141
|
|
|
|
142
|
|
|
$entityManager->persist($book); |
143
|
|
|
|
144
|
|
|
$entityManager->flush(); |
145
|
|
|
$this->addFlash( |
146
|
|
|
'notice', |
147
|
|
|
'Boken "' . $title . ' " är uppdaterad.' |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
return $this->redirectToRoute('library_show_all'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
#[Route('/library/reset', name: 'library_reset_really', methods: ['GET'])] |
154
|
|
|
public function reallyReset(): Response |
155
|
|
|
{ |
156
|
|
|
return $this->render('book/reset.html.twig'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
#[Route('/library/reset', name: 'library_reset_database', methods:['POST'])] |
160
|
|
|
public function resetDatabase( |
161
|
|
|
BookRepository $bookRepository |
162
|
|
|
): Response { |
163
|
|
|
$bookRepository->reset(); |
164
|
|
|
|
165
|
|
|
$this->addFlash( |
166
|
|
|
'notice', |
167
|
|
|
'Databasen är återställd!' |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
return $this->redirectToRoute('library_show_all'); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.