From 0348d0db000d989ef7c2311c6b69abd2495c6a70 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Mon, 28 Jan 2019 14:31:48 -0500 Subject: [PATCH] Cleanup redundant methods in Collection model --- src/Controller/AnimeCollection.php | 17 ++++++++-- src/Model/AnimeCollection.php | 50 +++++++++++------------------- src/Model/MangaCollection.php | 15 --------- 3 files changed, 32 insertions(+), 50 deletions(-) diff --git a/src/Controller/AnimeCollection.php b/src/Controller/AnimeCollection.php index ea2b45b5..3510910a 100644 --- a/src/Controller/AnimeCollection.php +++ b/src/Controller/AnimeCollection.php @@ -141,6 +141,7 @@ final class AnimeCollection extends BaseController { $data = $this->request->getParsedBody(); if (array_key_exists('hummingbird_id', $data)) { + // @TODO verify data was updated correctly $this->animeCollectionModel->update($data); $this->setFlashMessage('Successfully updated collection item.', 'success'); } @@ -165,8 +166,17 @@ final class AnimeCollection extends BaseController { $data = $this->request->getParsedBody(); if (array_key_exists('id', $data)) { - $this->animeCollectionModel->add($data); - $this->setFlashMessage('Successfully added collection item', 'success'); + // Check for existing entry + if ($this->animeCollectionModel->get($data['id']) !== FALSE) + { + $this->setFlashMessage('Anime already exists, can not create duplicate', 'info'); + } + else + { + // @TODO actually verify that collection item was added + $this->animeCollectionModel->add($data); + $this->setFlashMessage('Successfully added collection item', 'success'); + } } else { @@ -189,10 +199,11 @@ final class AnimeCollection extends BaseController { $this->redirect('/anime-collection/view', 303); } + // @TODO verify that item was actually deleted $this->animeCollectionModel->delete($data); $this->setFlashMessage('Successfully removed anime from collection.', 'success'); $this->redirect('/anime-collection/view', 303); } } -// End of CollectionController.php \ No newline at end of file +// End of AnimeCollection.php \ No newline at end of file diff --git a/src/Model/AnimeCollection.php b/src/Model/AnimeCollection.php index ea42e848..c69888c6 100644 --- a/src/Model/AnimeCollection.php +++ b/src/Model/AnimeCollection.php @@ -112,7 +112,7 @@ final class AnimeCollection extends Collection { // Add genres associated with each item $rows = $query->fetchAll(PDO::FETCH_ASSOC); - $genres = $this->getGenresForList(); + $genres = $this->getGenreList(); foreach($rows as &$row) { @@ -275,29 +275,6 @@ final class AnimeCollection extends Collection { return $output; } - /** - * Get the list of genres from the database - * - * @return array - */ - private function getGenresForList(): array - { - $query = $this->db->select('hummingbird_id, genre') - ->from('genres g') - ->join('genre_anime_set_link gasl', 'gasl.genre_id=g.id') - ->get(); - - $rows = $query->fetchAll(PDO::FETCH_ASSOC); - $output = []; - - foreach($rows as $row) - { - $output[$row['hummingbird_id']][] = $row['genre']; - } - - return $output; - } - /** * Update genre information for selected anime * @@ -353,9 +330,16 @@ final class AnimeCollection extends Collection { * @return array */ private function getGenreData(): array + { + return [ + 'genres' => $this->getExistingGenres(), + 'links' => $this->getExistingGenreLinkEntries(), + ]; + } + + private function getExistingGenres(): array { $genres = []; - $links = []; // Get existing genres $query = $this->db->select('id, genre') @@ -366,7 +350,13 @@ final class AnimeCollection extends Collection { $genres[$genre['id']] = $genre['genre']; } - // Get existing link table entries + return $genres; + } + + private function getExistingGenreLinkEntries(): array + { + $links = []; + $query = $this->db->select('hummingbird_id, genre_id') ->from('genre_anime_set_link') ->get(); @@ -375,17 +365,13 @@ final class AnimeCollection extends Collection { if (array_key_exists($link['hummingbird_id'], $links)) { $links[$link['hummingbird_id']][] = $link['genre_id']; - } - else + } else { $links[$link['hummingbird_id']] = [$link['genre_id']]; } } - return [ - 'genres' => $genres, - 'links' => $links - ]; + return $links; } } // End of AnimeCollectionModel.php \ No newline at end of file diff --git a/src/Model/MangaCollection.php b/src/Model/MangaCollection.php index 6c42f2bd..71238f42 100644 --- a/src/Model/MangaCollection.php +++ b/src/Model/MangaCollection.php @@ -88,21 +88,6 @@ final class MangaCollection extends Collection { return $output; } - /** - * Get item from collection for editing - * - * @param int $id - * @return array - */ - public function getCollectionEntry($id): array - { - $query = $this->db->from('anime_set') - ->where('hummingbird_id', $id) - ->get(); - - return $query->fetch(PDO::FETCH_ASSOC); - } - /** * Get full collection from the database *