From 733d3f3871e41f6357998722017f74925bb8ad8c Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 14 Apr 2016 19:10:03 -0400 Subject: [PATCH] Resolves #10, adds ability to delete from anime collection --- app/views/collection/cover.php | 4 +- app/views/collection/edit.php | 24 +++- .../AnimeClient/Controller/Collection.php | 8 +- src/Aviat/AnimeClient/Model/Anime.php | 1 + .../AnimeClient/Model/AnimeCollection.php | 99 +------------- src/Aviat/AnimeClient/Model/Collection.php | 127 ++++++++++++++++++ 6 files changed, 156 insertions(+), 107 deletions(-) create mode 100644 src/Aviat/AnimeClient/Model/Collection.php diff --git a/app/views/collection/cover.php b/app/views/collection/cover.php index 51e8f9d5..22db0c4a 100644 --- a/app/views/collection/cover.php +++ b/app/views/collection/cover.php @@ -1,6 +1,6 @@
is_authenticated()): ?> -Add Item +Add Item

There's nothing here!

@@ -11,7 +11,7 @@
- \ No newline at end of file diff --git a/src/Aviat/AnimeClient/Controller/Collection.php b/src/Aviat/AnimeClient/Controller/Collection.php index 70a27cd0..24a78be3 100644 --- a/src/Aviat/AnimeClient/Controller/Collection.php +++ b/src/Aviat/AnimeClient/Controller/Collection.php @@ -15,7 +15,6 @@ namespace Aviat\AnimeClient\Controller; use Aviat\Ion\Di\ContainerInterface; use Aviat\AnimeClient\Controller as BaseController; -use Aviat\AnimeClient\Config; use Aviat\AnimeClient\UrlGenerator; use Aviat\AnimeClient\Model\Anime as AnimeModel; use Aviat\AnimeClient\Model\AnimeCollection as AnimeCollectionModel; @@ -174,14 +173,15 @@ class Collection extends BaseController { public function delete() { $data = $this->request->getParsedBody(); - if ( ! array_key_exists('id', $data)) + if ( ! array_key_exists('hummingbird_id', $data)) { - $this->redirect("collection/view", 303); + $this->redirect("/collection/view", 303); } $this->anime_collection_model->delete($data); + $this->set_flash_message("Successfully removed anime from collection.", 'success'); - $this->redirect("collection/view", 303); + $this->redirect("/collection/view", 303); } } // End of CollectionController.php \ No newline at end of file diff --git a/src/Aviat/AnimeClient/Model/Anime.php b/src/Aviat/AnimeClient/Model/Anime.php index b2a3f9a7..caccac8e 100644 --- a/src/Aviat/AnimeClient/Model/Anime.php +++ b/src/Aviat/AnimeClient/Model/Anime.php @@ -173,6 +173,7 @@ class Anime extends API { * * @param string $name * @return array + * @throws RuntimeException */ public function search($name) { diff --git a/src/Aviat/AnimeClient/Model/AnimeCollection.php b/src/Aviat/AnimeClient/Model/AnimeCollection.php index a96a2eb8..7b426344 100644 --- a/src/Aviat/AnimeClient/Model/AnimeCollection.php +++ b/src/Aviat/AnimeClient/Model/AnimeCollection.php @@ -19,19 +19,7 @@ use Aviat\Ion\Di\ContainerInterface; /** * Model for getting anime collection data */ -class AnimeCollection extends DB { - - /** - * Anime API Model - * @var object $anime_model - */ - private $anime_model; - - /** - * Whether the database is valid for querying - * @var bool - */ - private $valid_database = FALSE; +class AnimeCollection extends Collection { /** * Constructor @@ -42,91 +30,10 @@ class AnimeCollection extends DB { { parent::__construct($container); - try - { - $this->db = \Query($this->db_config['collection']); - } - catch (\PDOException $e) - { - $this->valid_database = FALSE; - return FALSE; - } - $this->anime_model = $container->get('anime-model'); - - // Is database valid? If not, set a flag so the - // app can be run without a valid database - if ($this->db_config['collection']['type'] === 'sqlite') - { - $db_file_name = $this->db_config['collection']['file']; - - if ($db_file_name !== ':memory:' && file_exists($db_file_name)) - { - $db_file = file_get_contents($db_file_name); - $this->valid_database = (strpos($db_file, 'SQLite format 3') === 0); - } - else - { - $this->valid_database = FALSE; - } - } - else - { - $this->valid_database = TRUE; - } - - // Do an import if an import file exists $this->json_import(); } - /** - * Get genres for anime collection items - * - * @param array $filter - * @return array - */ - public function get_genre_list($filter = []) - { - $this->db->select('hummingbird_id, genre') - ->from('genre_anime_set_link gl') - ->join('genres g', 'g.id=gl.genre_id', 'left'); - - - if ( ! empty($filter)) - { - $this->db->where_in('hummingbird_id', $filter); - } - - $query = $this->db->order_by('hummingbird_id') - ->order_by('genre') - ->get(); - - $output = []; - - foreach ($query->fetchAll(\PDO::FETCH_ASSOC) as $row) - { - $id = $row['hummingbird_id']; - $genre = $row['genre']; - - // Empty genre names aren't useful - if (empty($genre)) - { - continue; - } - - if (array_key_exists($id, $output)) - { - array_push($output[$id], $genre); - } - else - { - $output[$id] = [$genre]; - } - } - - return $output; - } - /** * Get collection from the database, and organize by media type * @@ -265,6 +172,7 @@ class AnimeCollection extends DB { /** * Remove a colleciton item + * * @param array $data * @return void */ @@ -276,6 +184,9 @@ class AnimeCollection extends DB { return; } + $this->db->where('hummingbird_id', $data['hummingbird_id']) + ->delete('genre_anime_set_link'); + $this->db->where('hummingbird_id', $data['hummingbird_id']) ->delete('anime_set'); } diff --git a/src/Aviat/AnimeClient/Model/Collection.php b/src/Aviat/AnimeClient/Model/Collection.php new file mode 100644 index 00000000..22e19378 --- /dev/null +++ b/src/Aviat/AnimeClient/Model/Collection.php @@ -0,0 +1,127 @@ +db = \Query($this->db_config['collection']); + } + catch (\PDOException $e) + { + $this->valid_database = FALSE; + return FALSE; + } + $this->anime_model = $container->get('anime-model'); + + // Is database valid? If not, set a flag so the + // app can be run without a valid database + if ($this->db_config['collection']['type'] === 'sqlite') + { + $db_file_name = $this->db_config['collection']['file']; + + if ($db_file_name !== ':memory:' && file_exists($db_file_name)) + { + $db_file = file_get_contents($db_file_name); + $this->valid_database = (strpos($db_file, 'SQLite format 3') === 0); + } + else + { + $this->valid_database = FALSE; + } + } + else + { + $this->valid_database = TRUE; + } + } + + /** + * Get genres for anime collection items + * + * @param array $filter + * @return array + */ + public function get_genre_list($filter = []) + { + $this->db->select('hummingbird_id, genre') + ->from('genre_anime_set_link gl') + ->join('genres g', 'g.id=gl.genre_id', 'left'); + + + if ( ! empty($filter)) + { + $this->db->where_in('hummingbird_id', $filter); + } + + $query = $this->db->order_by('hummingbird_id') + ->order_by('genre') + ->get(); + + $output = []; + + foreach ($query->fetchAll(\PDO::FETCH_ASSOC) as $row) + { + $id = $row['hummingbird_id']; + $genre = $row['genre']; + + // Empty genre names aren't useful + if (empty($genre)) + { + continue; + } + + if (array_key_exists($id, $output)) + { + array_push($output[$id], $genre); + } + else + { + $output[$id] = [$genre]; + } + } + + return $output; + } + +} +// End of Collection.php \ No newline at end of file