|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
6
|
|
|
use App\Entity\Main\Book; |
|
7
|
|
|
use App\Repository\BookRepository; |
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
12
|
|
|
|
|
13
|
|
|
class LibraryControllerJson extends AbstractController |
|
14
|
|
|
{ |
|
15
|
|
|
#[Route("/api/library/", name: "json_library")] |
|
16
|
|
|
public function index(BookRepository $bookRepository): Response |
|
17
|
|
|
{ |
|
18
|
|
|
$books = $bookRepository->findAll(); |
|
19
|
|
|
|
|
20
|
|
|
return $this->render('library/json/index.html.twig', ['books' => $books]); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
#[Route("/api/library/books", name: "json_books")] |
|
24
|
|
|
public function showAll(BookRepository $bookRepository): JsonResponse |
|
25
|
|
|
{ |
|
26
|
|
|
$books = $bookRepository->findAll(); |
|
27
|
|
|
|
|
28
|
|
|
if (empty($books)) { |
|
29
|
|
|
throw $this->createNotFoundException( |
|
30
|
|
|
'No books found' |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$response = $this->json($books); |
|
35
|
|
|
$response->setEncodingOptions( |
|
36
|
|
|
$response->getEncodingOptions() | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT, |
|
37
|
|
|
); |
|
38
|
|
|
return $response; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
#[Route("/api/library/book/{isbn}", name: "json_book")] |
|
42
|
|
|
public function showByIsbn(Book $book): JsonResponse |
|
43
|
|
|
{ |
|
44
|
|
|
$response = $this->json($book); |
|
45
|
|
|
$response->setEncodingOptions( |
|
46
|
|
|
$response->getEncodingOptions() | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT, |
|
47
|
|
|
); |
|
48
|
|
|
return $response; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|