|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Book; |
|
6
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
|
7
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @extends ServiceEntityRepository<Book> |
|
11
|
|
|
*/ |
|
12
|
|
|
class BookRepository extends ServiceEntityRepository |
|
13
|
|
|
{ |
|
14
|
|
|
private ManagerRegistry $doctrine; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* __construct. |
|
18
|
|
|
* |
|
19
|
|
|
* @return void |
|
20
|
|
|
*/ |
|
21
|
10 |
|
public function __construct(ManagerRegistry $doctrine) |
|
22
|
|
|
{ |
|
23
|
10 |
|
$this->doctrine = $doctrine; |
|
24
|
|
|
|
|
25
|
10 |
|
parent::__construct($doctrine, Book::class); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* returnBook. |
|
30
|
|
|
* |
|
31
|
|
|
* Return book object (works like a pseudo constructor) |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function returnBook(string $title, ?string $isbn = null, ?string $author = null, ?string $img = null): Book |
|
34
|
|
|
{ |
|
35
|
2 |
|
$book = new Book(); |
|
36
|
|
|
|
|
37
|
2 |
|
$book->setTitle($title); |
|
38
|
2 |
|
$book->setISBN($isbn); |
|
39
|
2 |
|
$book->setAuthor($author); |
|
40
|
2 |
|
$book->setImg($img); |
|
41
|
|
|
|
|
42
|
2 |
|
return $book; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* saveBook. |
|
47
|
|
|
* |
|
48
|
|
|
* Saves a book to the database |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public function saveBook(string $title, ?string $isbn = null, ?string $author = null, ?string $img = null): void |
|
51
|
|
|
{ |
|
52
|
|
|
// If title is empty |
|
53
|
2 |
|
if ('' === $title) { |
|
54
|
1 |
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
$book = $this->returnBook($title, $isbn, $author, $img); |
|
58
|
|
|
|
|
59
|
2 |
|
$entityManager = $this->doctrine->getManager(); |
|
60
|
|
|
|
|
61
|
|
|
// tell Doctrine you want to (eventually) save the Book |
|
62
|
|
|
// (no queries yet) |
|
63
|
2 |
|
$entityManager->persist($book); |
|
64
|
|
|
|
|
65
|
|
|
// actually executes the queries (i.e. the INSERT query) |
|
66
|
2 |
|
$entityManager->flush(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* updateBook. |
|
71
|
|
|
* |
|
72
|
|
|
* Saves a book to the database |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public function updateBook(int $id, string $title, ?string $isbn = null, ?string $author = null, ?string $img = null): void |
|
75
|
|
|
{ |
|
76
|
2 |
|
$book = $this->findOneBy(['id' => $id]); |
|
77
|
|
|
|
|
78
|
|
|
// If the book can't be found |
|
79
|
2 |
|
if (null === $book) { |
|
80
|
1 |
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// If title is empty |
|
84
|
2 |
|
if ('' === $title) { |
|
85
|
1 |
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
$book->setTitle($title); |
|
89
|
|
|
|
|
90
|
|
|
// If you have new ISBN |
|
91
|
2 |
|
if (null !== $isbn) { |
|
92
|
2 |
|
$book->setIsbn($isbn); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// If you have new Author |
|
96
|
2 |
|
if (null !== $author) { |
|
97
|
2 |
|
$book->setAuthor($author); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// If you have new image |
|
101
|
2 |
|
if (null !== $img) { |
|
102
|
1 |
|
$book->setImg($img); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// Get the entity manager |
|
106
|
2 |
|
$entityManager = $this->doctrine->getManager(); |
|
107
|
|
|
|
|
108
|
|
|
// Persist is optional for existing entities, but it doesn't hurt |
|
109
|
2 |
|
$entityManager->persist($book); |
|
110
|
|
|
|
|
111
|
|
|
// Flush to save changes |
|
112
|
2 |
|
$entityManager->flush(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* deleteBook. |
|
117
|
|
|
*/ |
|
118
|
2 |
|
public function deleteBook(int $id): void |
|
119
|
|
|
{ |
|
120
|
2 |
|
$book = $this->findOneBy(['id' => $id]); |
|
121
|
|
|
|
|
122
|
|
|
// If the book can't be found |
|
123
|
2 |
|
if (null === $book) { |
|
124
|
1 |
|
return; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// Get the entity manager |
|
128
|
2 |
|
$entityManager = $this->doctrine->getManager(); |
|
129
|
|
|
|
|
130
|
|
|
// Remove the book |
|
131
|
2 |
|
$entityManager->remove($book); |
|
132
|
2 |
|
$entityManager->flush(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* readAllBooks. |
|
137
|
|
|
* |
|
138
|
|
|
* Reads all books from the database and returns a array of books |
|
139
|
|
|
* |
|
140
|
|
|
* @return array{ |
|
|
|
|
|
|
141
|
|
|
* books: array<array{ |
|
142
|
|
|
* id: int|null, |
|
143
|
|
|
* title: string|null, |
|
144
|
|
|
* isbn: string|null, |
|
145
|
|
|
* author: string|null, |
|
146
|
|
|
* img: string|null |
|
147
|
|
|
* }> |
|
148
|
|
|
* } |
|
149
|
|
|
*/ |
|
150
|
10 |
|
public function readAllBooks(): array |
|
151
|
|
|
{ |
|
152
|
10 |
|
$data = [ |
|
153
|
10 |
|
'books' => [], |
|
154
|
10 |
|
]; |
|
155
|
|
|
|
|
156
|
10 |
|
$books = $this->findAll(); |
|
157
|
|
|
|
|
158
|
10 |
|
$numBooks = count($books); |
|
159
|
|
|
|
|
160
|
10 |
|
for ($i = 0; $i < $numBooks; ++$i) { |
|
161
|
10 |
|
$data['books'][$i]['id'] = $books[$i]->getId(); |
|
162
|
10 |
|
$data['books'][$i]['title'] = $books[$i]->getTitle(); |
|
163
|
10 |
|
$data['books'][$i]['isbn'] = $books[$i]->getISBN(); |
|
164
|
10 |
|
$data['books'][$i]['author'] = $books[$i]->getAuthor(); |
|
165
|
10 |
|
$data['books'][$i]['img'] = $books[$i]->getImg(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
10 |
|
return $data; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* readOneBook. |
|
173
|
|
|
* |
|
174
|
|
|
* @return array{ |
|
|
|
|
|
|
175
|
|
|
* book: array{ |
|
176
|
|
|
* id: int, |
|
177
|
|
|
* title: string|null, |
|
178
|
|
|
* isbn: string|null, |
|
179
|
|
|
* author: string|null, |
|
180
|
|
|
* img: string|null |
|
181
|
|
|
* } |
|
182
|
|
|
* } |
|
183
|
|
|
*/ |
|
184
|
4 |
|
public function readOneBook(int $id): array |
|
185
|
|
|
{ |
|
186
|
4 |
|
$data = [ |
|
187
|
4 |
|
'book' => [ |
|
188
|
4 |
|
'id' => $id, |
|
189
|
4 |
|
'title' => '', |
|
190
|
4 |
|
'isbn' => null, |
|
191
|
4 |
|
'author' => null, |
|
192
|
4 |
|
'img' => null, |
|
193
|
4 |
|
], |
|
194
|
4 |
|
]; |
|
195
|
|
|
|
|
196
|
|
|
// If id is less then 0 |
|
197
|
4 |
|
if ($id < 0) { |
|
198
|
1 |
|
return $data; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
4 |
|
$book = $this->findOneBy(['id' => $id]); |
|
202
|
|
|
|
|
203
|
|
|
// If no book is found |
|
204
|
4 |
|
if (null === $book) { |
|
205
|
2 |
|
return $data; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
4 |
|
$data['book']['title'] = $book->getTitle(); |
|
209
|
4 |
|
$data['book']['isbn'] = $book->getISBN(); |
|
210
|
4 |
|
$data['book']['author'] = $book->getAuthor(); |
|
211
|
4 |
|
$data['book']['img'] = $book->getImg(); |
|
212
|
|
|
|
|
213
|
4 |
|
return $data; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* readOneBookISBN. |
|
218
|
|
|
* |
|
219
|
|
|
* @return array{ |
|
|
|
|
|
|
220
|
|
|
* book: array{ |
|
221
|
|
|
* id: int|null, |
|
222
|
|
|
* title: string|null, |
|
223
|
|
|
* isbn: string|null, |
|
224
|
|
|
* author: string|null, |
|
225
|
|
|
* img: string|null |
|
226
|
|
|
* } |
|
227
|
|
|
* } |
|
228
|
|
|
*/ |
|
229
|
3 |
|
public function readOneBookISBN(string $isbn): array |
|
230
|
|
|
{ |
|
231
|
3 |
|
$data = [ |
|
232
|
3 |
|
'book' => [ |
|
233
|
3 |
|
'id' => -1, |
|
234
|
3 |
|
'title' => '', |
|
235
|
3 |
|
'isbn' => $isbn, |
|
236
|
3 |
|
'author' => null, |
|
237
|
3 |
|
'img' => null, |
|
238
|
3 |
|
], |
|
239
|
3 |
|
]; |
|
240
|
|
|
|
|
241
|
|
|
// isbn is not 13 digits return the empty data |
|
242
|
3 |
|
if (13 != strlen($isbn)) { |
|
243
|
1 |
|
return $data; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
3 |
|
$book = $this->findOneBy(['isbn' => $isbn]); |
|
247
|
|
|
|
|
248
|
|
|
// If no book is found |
|
249
|
3 |
|
if (null === $book) { |
|
250
|
2 |
|
return $data; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
3 |
|
$data['book']['id'] = $book->getId(); |
|
254
|
3 |
|
$data['book']['title'] = $book->getTitle(); |
|
255
|
3 |
|
$data['book']['author'] = $book->getAuthor(); |
|
256
|
3 |
|
$data['book']['img'] = $book->getImg(); |
|
257
|
|
|
|
|
258
|
3 |
|
return $data; |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|