diff --git a/app/controllers/BookController.php b/app/controllers/BookController.php index 2367ded7b89e97f5d5d9740c3e6e3b63e6122d57..79c33cfb1909bc867fcf1a00b91d3b159b678799 100644 --- a/app/controllers/BookController.php +++ b/app/controllers/BookController.php @@ -75,16 +75,31 @@ class BookController extends Controller implements ControllerInterface{ $addBookView->render(); break; case 'POST': + $uploadedImage = null; + if (isset($_POST['imagePath'])) { + $uploadedImage = $_POST['imagePath']; + } + + $uploadedAudio = null; + if (isset($_POST['audioPath'])) { + $uploadedAudio = $_POST['audioPath']; + } + $fileHandler = new FileHandler(); - $imageFile = $_FILES['cover']['tmp_name']; + if(!$uploadedImage) { + $imageFile = $_FILES['cover']['tmp_name']; + $uploadedImage = $fileHandler->saveImageTo($imageFile, $_POST['title'], BOOK_COVER_PATH); + $uploadedImage = BASE_URL . '/' . $uploadedImage; + } + + if (!$uploadedAudio) { + $audioFile = $_FILES['audio']['tmp_name']; + $uploadedAudio = $fileHandler->saveAudioTo($audioFile, $_POST['title'], AUDIOBOOK_PATH); + } - $audioFile = $_FILES['audio']['tmp_name']; $duration = (int) $fileHandler->getAudioDuration($audioFile); - - $uploadedAudio = $fileHandler->saveAudioTo($audioFile, $_POST['title'], AUDIOBOOK_PATH); - $uploadedImage = $fileHandler->saveImageTo($imageFile, $_POST['title'], BOOK_COVER_PATH); - + $title = $_POST['title']; $year = (int)$_POST['year']; $summary = $_POST['summary']; diff --git a/app/models/AuthorModel.php b/app/models/AuthorModel.php index 39b1578ed5b4b3d7eb4280749b6ee0d803bf580d..bd8538961e011220422ad703211a95a177216c10 100644 --- a/app/models/AuthorModel.php +++ b/app/models/AuthorModel.php @@ -9,13 +9,27 @@ class AuthorModel { } // CRUD Operations - public function addAuthor(string $fullName, int $age = null): bool { + public function addAuthor(string $fullName, int $age = null) { + if ($this->authorExists($fullName)) { + return false; + } + $sql = "INSERT INTO author (full_name, age) VALUES (?, ?)"; $stmt = $this->db->prepare($sql); $this->db->bindParams($stmt, "si", $fullName, $age); return $this->db->execute($stmt); } + public function authorExists(string $authorName) : bool { + $sql = "SELECT * FROM author WHERE full_name = ?"; + $stmt = $this->db->prepare($sql); + $this->db->bindParams($stmt, "s", $authorName); + $this->db->execute($stmt); + $author = $this->db->getSingleRecord($stmt); + $stmt->close(); + return $author ? true : false; + } + public function getAuthorById(int $authorId) { $sql = "SELECT * FROM author WHERE author_id = ?"; $stmt = $this->db->prepare($sql); diff --git a/app/pages/book/BookListPage.php b/app/pages/book/BookListPage.php index 7e482d988a00ef2c24ed5b67e4366e5110932bac..e550d9ec4157f117db8af2799ed430c1cba80418 100644 --- a/app/pages/book/BookListPage.php +++ b/app/pages/book/BookListPage.php @@ -78,9 +78,8 @@ } ?> <?php foreach ($this->data['book'] as $book): ?> <div class="book-container"> - <img class="book-image" src="<?= BASE_URL ?>/<?= str_replace('/var/www/html/config/', '', $book['cover_path']) ?>" alt="book-cover" /> - - <div class="info-text title-text"><?= limit_text_length($book['title']) ?></div> + <img class="book-image" src="<?= str_replace('/var/www/html/config/', '', $book['cover_path']) ?>" alt="book-cover" /> + <div class="info-text title-text"><?= $book['title'] ?></div> <div class="info-text author-text"> <?= current(explode(',', limit_text_length($book['authors']))) ?> <?php if (count(explode(',', $book['authors'])) > 1): ?> @@ -130,24 +129,29 @@ <div class="pagination vert-m-50"> <? + function clamp($val, $min, $max) : int{ + return min(max($val, $min), $max); + } + $page = $this->data['page']; $maxPage = $this->data['totalPages']; - $prevPage = $page - 1; - $params = $this->data['q_params']; - $topPage = $page + 10; - if ($topPage > $maxPage) { - $topPage = $maxPage; - } + $bottomPage = clamp($page - 4, 1, $maxPage); + $topPageTreshold = 9 - ($page - $bottomPage); + $topPage = $page + $topPageTreshold; + + echo '<a href="/book/search/' . 1 . $params . '" class="page-btn"><div class=""> First Page </div></a> '; - for ($i = 1; $i <= $topPage; $i++) { + for ($i = $bottomPage; $i <= $topPage; $i++) { if ($i == $page) { - echo '<a href="/book/search/' . $i . $params . '" class="page-btn"><b>' . $i . '</b></a>'; + echo ' <a href="/book/search/' . $i . $params . '" class="page-btn"><b>' . $i . '</b></a> '; } else { - echo '<a href="/book/search/' . $i . $params . '" class="page-btn"><div class="">' . $i . '</div></a>'; + echo ' <a href="/book/search/' . $i . $params . '" class="page-btn"><div class="">' . $i . '</div></a> '; } } + + echo ' <a href="/book/search/' . $maxPage . $params . '" class="page-btn"> <div class=""> Last Page </div></a>'; ?> </div> </div> diff --git a/db-init/seeding/book.py b/db-init/seeding/book.py index d551699b8baf36a73cf0773e91b6ee6f3803bb5b..2ae534e1eef3a542d6b411eb1443987906d589ca 100644 --- a/db-init/seeding/book.py +++ b/db-init/seeding/book.py @@ -65,7 +65,8 @@ for b in books: image_content = requests.get(book_cover_url).content image_file.write(image_content) - audio_path = f'tmp/tmp.mp3' + audio_rnd = random.randint(0, 2) + audio_path = f'tmp/tmp{audio_rnd}.mp3' files = { 'cover': open(book_cover_temp_path, 'rb'), diff --git a/db-init/seeding/tmp/Democracy Awakening.jpg b/db-init/seeding/tmp/Democracy Awakening.jpg deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/db-init/seeding/tmp/tmp.mp3 b/db-init/seeding/tmp/tmp.mp3 deleted file mode 100644 index 5fda7ff876cd56252a91b6b925014fa17535a62f..0000000000000000000000000000000000000000 Binary files a/db-init/seeding/tmp/tmp.mp3 and /dev/null differ diff --git a/db-init/seeding/user.py b/db-init/seeding/user.py index 80af1a6f54fe39e677a685b6a8bdf429b3919728..3b916f312cde4aa9e80ed551c74a7af4e64f884b 100644 --- a/db-init/seeding/user.py +++ b/db-init/seeding/user.py @@ -41,6 +41,21 @@ for _ in range(20): users.append((user_data, image_path)) + +users.append(({ + 'username': 'admin', + 'role': 'admin', + 'email': 'admin@gmail.com', + 'password': 'admin' +}, 'tmp/tmp.jpg')) + +users.append(({ + 'username': 'cust', + 'role': 'cust', + 'email': 'cust@gmail.com', + 'password': 'cust' +}, 'tmp/tmp.jpg')) + # Send POST requests to create users with file uploads for user_data, image_path in users: files = {'profile-pic': open(image_path, 'rb')} @@ -52,4 +67,5 @@ for user_data, image_path in users: print(f"Failed to insert user '{user_data['username']}'.") # Clean up: remove temporary image file - os.remove(image_path) \ No newline at end of file + if (image_path != 'tmp/tmp.jpg'): + os.remove(image_path) \ No newline at end of file