|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Book; |
|
6
|
|
|
use App\Repository\BookRepository; |
|
7
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
|
14
|
|
|
|
|
15
|
|
|
final class LibraryController extends AbstractController |
|
16
|
|
|
{ |
|
17
|
1 |
|
#[Route("/library", name: 'library')] |
|
18
|
|
|
public function index(): Response |
|
19
|
|
|
{ |
|
20
|
1 |
|
return $this->redirectToRoute('show_all'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
1 |
|
#[Route("/library/create", name: "add_book_get", methods: ['GET'])] |
|
24
|
|
|
public function addBook(): Response |
|
25
|
|
|
{ |
|
26
|
1 |
|
return $this->render('library/create.html.twig'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
#[Route("/library/create", name: "add_book_post", methods: ['POST'])] |
|
30
|
|
|
public function addBookCallback( |
|
31
|
|
|
Request $request, |
|
32
|
|
|
ManagerRegistry $doctrine |
|
33
|
|
|
): Response { |
|
34
|
1 |
|
$entityManager = $doctrine->getManager(); |
|
35
|
|
|
|
|
36
|
1 |
|
$book = new Book(); |
|
37
|
1 |
|
$book->setTitle((string) $request->request->get('title')); |
|
38
|
1 |
|
$book->setIsbn((string) $request->request->get('isbn')); |
|
39
|
1 |
|
$book->setAuthor((string) $request->request->get('author')); |
|
40
|
1 |
|
$book->setImage((string) $request->request->get('image')); |
|
41
|
|
|
|
|
42
|
1 |
|
$entityManager->persist($book); |
|
43
|
|
|
|
|
44
|
1 |
|
$entityManager->flush(); |
|
45
|
|
|
|
|
46
|
1 |
|
return $this->redirectToRoute('show_all'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
#[Route("/library/show/{id}", name: "show_one", methods: ['GET'])] |
|
50
|
|
|
public function showBookById( |
|
51
|
|
|
BookRepository $bookRepository, |
|
52
|
|
|
int $id |
|
53
|
|
|
): Response { |
|
54
|
1 |
|
$book = $bookRepository->find($id); |
|
55
|
|
|
|
|
56
|
1 |
|
if (!$book) { |
|
57
|
|
|
throw new Exception("No book found for id"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
return $this->render( |
|
61
|
1 |
|
'library/display_one.html.twig', |
|
62
|
1 |
|
array('book' => $book) |
|
63
|
1 |
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
#[Route("/library/show", name: "show_all", methods: ['GET'])] |
|
67
|
|
|
public function showAllBooks( |
|
68
|
|
|
BookRepository $bookRepository |
|
69
|
|
|
): Response { |
|
70
|
1 |
|
$books = $bookRepository->findAll(); |
|
71
|
|
|
|
|
72
|
1 |
|
return $this->render( |
|
73
|
1 |
|
'library/display_all.html.twig', |
|
74
|
1 |
|
array('books' => $books) |
|
75
|
1 |
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
#[Route("/library/update/{id}", name: "update_book_get", methods: ['GET'])] |
|
79
|
|
|
public function updateBook( |
|
80
|
|
|
BookRepository $bookRepository, |
|
81
|
|
|
int $id |
|
82
|
|
|
): Response { |
|
83
|
1 |
|
$book = $bookRepository->find($id); |
|
84
|
|
|
|
|
85
|
1 |
|
if (!$book) { |
|
86
|
|
|
throw new Exception("No book found for id"); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
return $this->render( |
|
90
|
1 |
|
'library/update.html.twig', |
|
91
|
1 |
|
array('book' => $book) |
|
92
|
1 |
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
#[Route("/library/update/{id}", name: "update_book_post", methods: ['POST'])] |
|
96
|
|
|
public function updateBookCallback( |
|
97
|
|
|
BookRepository $bookRepository, |
|
98
|
|
|
Request $request, |
|
99
|
|
|
ManagerRegistry $doctrine, |
|
100
|
|
|
int $id |
|
101
|
|
|
): Response { |
|
102
|
1 |
|
$entityManager = $doctrine->getManager(); |
|
103
|
1 |
|
$book = $bookRepository->find($id); |
|
104
|
|
|
|
|
105
|
1 |
|
if (!$book) { |
|
106
|
|
|
throw new Exception("No book found for id"); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
$book->setTitle((string) $request->request->get('title')); |
|
110
|
1 |
|
$book->setIsbn((string) $request->request->get('isbn')); |
|
111
|
1 |
|
$book->setAuthor((string) $request->request->get('author')); |
|
112
|
1 |
|
$book->setImage((string) $request->request->get('image')); |
|
113
|
|
|
|
|
114
|
1 |
|
$entityManager->flush(); |
|
115
|
|
|
|
|
116
|
1 |
|
return $this->redirectToRoute('show_all'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
1 |
|
#[Route("/library/delete/{id}", name: "delete_book", methods: ['GET', 'POST'])] |
|
120
|
|
|
public function deleteBook( |
|
121
|
|
|
BookRepository $bookRepository, |
|
122
|
|
|
ManagerRegistry $doctrine, |
|
123
|
|
|
int $id |
|
124
|
|
|
): Response { |
|
125
|
1 |
|
$entityManager = $doctrine->getManager(); |
|
126
|
1 |
|
$book = $bookRepository->find($id); |
|
127
|
|
|
|
|
128
|
1 |
|
if (!$book) { |
|
129
|
|
|
throw new Exception("No book found for id"); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
$title = $book->getTitle(); |
|
133
|
|
|
|
|
134
|
1 |
|
$entityManager->remove($book); |
|
135
|
1 |
|
$entityManager->flush(); |
|
136
|
|
|
|
|
137
|
1 |
|
$this->addFlash( |
|
138
|
1 |
|
'notice', |
|
139
|
1 |
|
$title . ' har tagits bort' |
|
140
|
1 |
|
); |
|
141
|
|
|
|
|
142
|
1 |
|
return $this->redirectToRoute('show_all'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
#[Route("/api/library/books", name: 'json-books', methods: ['GET'], defaults: ['description' => 'Returnerar en JSON-struktur med samtliga böcker i biblioteket.'])] |
|
146
|
|
|
public function jsonBooks( |
|
147
|
|
|
BookRepository $bookRepository |
|
148
|
|
|
): Response { |
|
149
|
1 |
|
$books = $bookRepository->findAll(); |
|
150
|
|
|
|
|
151
|
1 |
|
if (empty($books)) { |
|
152
|
|
|
throw $this->createNotFoundException( |
|
153
|
|
|
'No books found' |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
1 |
|
$response = $this->json($books); |
|
158
|
1 |
|
$response->setEncodingOptions( |
|
159
|
1 |
|
$response->getEncodingOptions() | JSON_PRETTY_PRINT |
|
160
|
1 |
|
); |
|
161
|
1 |
|
return $response; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
1 |
|
#[Route("/api/library/book/{isbn<\d+>}", name: 'json-isbn', methods: ['GET'], defaults: ['description' => 'Returnerar en JSON-struktur för valt ISBN-nummer.', 'isbn' => '9789129691771'])] |
|
165
|
|
|
public function jsonIsbn( |
|
166
|
|
|
BookRepository $bookRepository, |
|
167
|
|
|
ManagerRegistry $doctrine, |
|
|
|
|
|
|
168
|
|
|
string $isbn |
|
169
|
|
|
): Response { |
|
170
|
1 |
|
$book = $bookRepository->findOneBy(['isbn' => $isbn]); |
|
171
|
|
|
|
|
172
|
1 |
|
if (!$book) { |
|
173
|
|
|
throw $this->createNotFoundException( |
|
174
|
|
|
'No book found for ISBN: '.$isbn |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
1 |
|
$response = $this->json($book); |
|
179
|
1 |
|
$response->setEncodingOptions( |
|
180
|
1 |
|
$response->getEncodingOptions() | JSON_PRETTY_PRINT |
|
181
|
1 |
|
); |
|
182
|
1 |
|
return $response; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.