Issues (10)

src/Controller/LibraryController.php (2 issues)

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
    #[Route("/library", name: 'library')]
18
    public function index(): Response
19
    {
20
        return $this->redirectToRoute('show_all');
21
    }
22
23
    #[Route("/library/create", name: "add_book_get", methods: ['GET'])]
24
    public function addBook(): Response
25
    {
26
        return $this->render('library/create.html.twig');
27
    }
28
29
    #[Route("/library/create", name: "add_book_post", methods: ['POST'])]
30
    public function addBookCallback(
31
        Request $request,
32
        ManagerRegistry $doctrine
33
    ): Response {
34
        $entityManager = $doctrine->getManager();
35
36
        $book = new Book();
37
        $book->setTitle((string) $request->request->get('title'));
38
        $book->setIsbn((string) $request->request->get('isbn'));
39
        $book->setAuthor((string) $request->request->get('author'));
40
        $book->setImage((string) $request->request->get('image'));
41
42
        $entityManager->persist($book);
43
44
        $entityManager->flush();
45
46
        return $this->redirectToRoute('show_all');
47
    }
48
49
    #[Route("/library/show/{id}", name: "show_one", methods: ['GET'])]
50
    public function showBookById(
51
        BookRepository $bookRepository,
52
        int $id
53
    ): Response {
54
        $book = $bookRepository->find($id);
55
56
        if (!$book) {
57
            throw new Exception("No book found for id");
58
        }
59
60
        return $this->render(
61
            'library/display_one.html.twig',
62
            array('book' => $book)
63
        );
64
    }
65
66
    #[Route("/library/show", name: "show_all", methods: ['GET'])]
67
    public function showAllBooks(
68
        BookRepository $bookRepository
69
    ): Response {
70
        $books = $bookRepository->findAll();
71
72
        return $this->render(
73
            'library/display_all.html.twig',
74
            array('books' => $books)
75
        );
76
    }
77
78
    #[Route("/library/update/{id}", name: "update_book_get", methods: ['GET'])]
79
    public function updateBook(
80
        BookRepository $bookRepository,
81
        int $id
82
    ): Response {
83
        $book = $bookRepository->find($id);
84
85
        if (!$book) {
86
            throw new Exception("No book found for id");
87
        }
88
89
        return $this->render(
90
            'library/update.html.twig',
91
            array('book' => $book)
92
        );
93
    }
94
95
    #[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
        $entityManager = $doctrine->getManager();
103
        $book = $bookRepository->find($id);
104
105
        if (!$book) {
106
            throw new Exception("No book found for id");
107
        }
108
109
        $book->setTitle((string) $request->request->get('title'));
110
        $book->setIsbn((string) $request->request->get('isbn'));
111
        $book->setAuthor((string) $request->request->get('author'));
112
        $book->setImage((string) $request->request->get('image'));
113
114
        $entityManager->flush();
115
116
        return $this->redirectToRoute('show_all');
117
    }
118
119
    #[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
        $entityManager = $doctrine->getManager();
126
        $book = $bookRepository->find($id);
127
128
        if (!$book) {
129
            throw new Exception("No book found for id");
130
        }
131
132
        $title = $book->getTitle();
133
134
        $entityManager->remove($book);
135
        $entityManager->flush();
136
137
        $this->addFlash(
138
            'notice',
139
            $title . ' har tagits bort'
140
        );
141
142
        return $this->redirectToRoute('show_all');
143
    }
144
145
    #[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
        $books = $bookRepository->findAll();
150
151
        if (!$books) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $books of type array<mixed,mixed> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
152
            throw $this->createNotFoundException(
153
                'No books found'
154
            );
155
        }
156
157
        $response = $this->json($books);
158
        $response->setEncodingOptions(
159
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
160
        );
161
        return $response;
162
    }
163
164
    #[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
        $entityManager = $doctrine->getManager();
0 ignored issues
show
The assignment to $entityManager is dead and can be removed.
Loading history...
171
        $book = $bookRepository->findOneBy(['isbn' => $isbn]);
172
173
        if (!$book) {
174
            throw $this->createNotFoundException(
175
                'No book found for ISBN: '.$isbn
176
            );
177
        }
178
179
        $response = $this->json($book);
180
        $response->setEncodingOptions(
181
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
182
        );
183
        return $response;
184
    }
185
}
186