Issues (6)

src/Controller/LibraryController.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace App\Controller;
4
5
use App\Controller\LibraryHelpers;
6
use App\Entity\Book;
7
use App\Repository\BookRepository;
8
use Doctrine\Persistence\ManagerRegistry;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Routing\Attribute\Route;
13
14
final class LibraryController extends AbstractController
15
{
16
    use LibraryHelpers;
0 ignored issues
show
The trait App\Controller\LibraryHelpers requires the property $request which is not provided by App\Controller\LibraryController.
Loading history...
17
18
    #[Route('/library', name: 'app_library')]
19
    public function index(BookRepository $bookRepository): Response
20
    {
21
        return $this->render('library/index.html.twig', [
22
            'books' => $this->getAllBooks($bookRepository),
23
        ]);
24
    }
25
26
    #[Route('/library/create', name: 'show-create-form', methods: ['GET'])]
27
    public function showCreateForm(): Response
28
    {
29
        return $this->render('library/create.html.twig');
30
    }
31
32
    #[Route('/library/create', name: 'library_create', methods: ['POST'])]
33
    public function createBook(
34
        Request $request,
35
        ManagerRegistry $doctrine
36
    ): Response {
37
        $entityManager = $doctrine->getManager();
38
39
        $book = new Book();
40
41
        $this->fillBookData($book, $request);
42
43
        $entityManager->persist($book);
44
        $entityManager->flush();
45
46
        return $this->redirectToRoute('app_library');
47
    }
48
49
    #[Route('/library/find', name: 'find_book_form')]
50
    public function findBookForm(): Response
51
    {
52
        return $this->render('library/find-book.html.twig');
53
    }
54
55
    #[Route('/library/find-id', name: 'find_book_by_id', methods: ['GET'])]
56
    public function findBookById(Request $request): Response
57
    {
58
        $id = $request->query->get('id');
59
60
        return $this->redirectToRoute('show_book_by_id', ['id' => $id]);
61
    }
62
63
    #[Route('/library/show/{id<\d+>}', name: 'show_book_by_id')]
64
    public function showBookById(
65
        BookRepository $bookRepository,
66
        int $id
67
    ): Response {
68
        $book = $bookRepository->find($id);
69
70
        if (!$book) {
71
            throw $this->createNotFoundException(
72
                'No book found for id '.$id
73
            );
74
        }
75
        return $this->render('library/show-book.html.twig', [
76
            'book' => $book,
77
        ]);
78
    }
79
80
    #[Route('/library/list', name: 'book-list')]
81
    public function bookLinks(BookRepository $bookRepository): Response
82
    {
83
        return $this->render('library/book-table.html.twig', [
84
            'books' => $this->getAllBooks($bookRepository),
85
        ]);
86
    }
87
88
    #[Route('/library/show/update', name: 'show_update_form')]
89
    public function showUpdateForm(Request $request): Response
90
    {
91
        $id = $request->query->get('id');
92
        if ($id) {
93
            return $this->redirectToRoute('show-book-to-update', ['id' => $id]);
94
        }
95
96
        return $this->render('library/show-update-form.html.twig');
97
    }
98
99
    #[Route('/library/update/{id<\d+>}', name: 'show-book-to-update', methods: ['GET'])]
100
    public function findBook(
101
        BookRepository $bookRepository,
102
        int $id
103
    ): Response {
104
        $book = $bookRepository->find($id);
105
106
        if (!$book) {
107
            throw $this->createNotFoundException("No book with id $id is found.");
108
        }
109
110
        return $this->render('library/update-form.html.twig', [
111
            'book' => $book,
112
        ]);
113
    }
114
115
    #[Route('/library/update', name: 'book_update', methods: ['POST'])]
116
    public function updateBook(
117
        Request $request,
118
        BookRepository $bookRepository,
119
        ManagerRegistry $doctrine,
120
    ): Response {
121
        $entityManager = $doctrine->getManager();
122
        $id = $request->request->get('id');
123
        $book = $bookRepository->find($id);
124
125
        if (!$book) {
126
            throw $this->createNotFoundException("No book with id $id is found.");
127
        }
128
129
        $this->fillBookData($book, $request);
130
131
        $entityManager->flush();
132
133
        return $this->redirectToRoute('app_library');
134
    }
135
136
    #[Route('/library/delete/{id<\d+>}', name: 'book_delete_by_id')]
137
    public function deleteProductById(
138
        ManagerRegistry $doctrine,
139
        int $id
140
    ): Response {
141
        $entityManager = $doctrine->getManager();
142
        $book = $entityManager->getRepository(Book::class)->find($id);
143
144
        if (!$book) {
145
            throw $this->createNotFoundException(
146
                'No product found for id '.$id
147
            );
148
        }
149
150
        $entityManager->remove($book);
151
        $entityManager->flush();
152
153
        return $this->redirectToRoute('app_library');
154
    }
155
156
    /**
157
     * Retrieves all Book entities from the repository.
158
     *
159
     * @param BookRepository $bookRepository The repository to fetch books from.
160
     * @return Book[] An array of Book objects.
161
     */
162
    private function getAllBooks(BookRepository $bookRepository): array
163
    {
164
        return $bookRepository->findAll();
165
    }
166
}
167