Cleanup redundant methods in Collection model

This commit is contained in:
Timothy Warren 2019-01-28 14:31:48 -05:00
parent 4e2437f2bc
commit 8a8ea0b470
3 changed files with 32 additions and 50 deletions

View File

@ -141,6 +141,7 @@ final class AnimeCollection extends BaseController {
$data = $this->request->getParsedBody(); $data = $this->request->getParsedBody();
if (array_key_exists('hummingbird_id', $data)) if (array_key_exists('hummingbird_id', $data))
{ {
// @TODO verify data was updated correctly
$this->animeCollectionModel->update($data); $this->animeCollectionModel->update($data);
$this->setFlashMessage('Successfully updated collection item.', 'success'); $this->setFlashMessage('Successfully updated collection item.', 'success');
} }
@ -165,9 +166,18 @@ final class AnimeCollection extends BaseController {
$data = $this->request->getParsedBody(); $data = $this->request->getParsedBody();
if (array_key_exists('id', $data)) if (array_key_exists('id', $data))
{ {
// 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->animeCollectionModel->add($data);
$this->setFlashMessage('Successfully added collection item', 'success'); $this->setFlashMessage('Successfully added collection item', 'success');
} }
}
else else
{ {
$this->setFlashMessage('Failed to add collection item.', 'error'); $this->setFlashMessage('Failed to add collection item.', 'error');
@ -189,10 +199,11 @@ final class AnimeCollection extends BaseController {
$this->redirect('/anime-collection/view', 303); $this->redirect('/anime-collection/view', 303);
} }
// @TODO verify that item was actually deleted
$this->animeCollectionModel->delete($data); $this->animeCollectionModel->delete($data);
$this->setFlashMessage('Successfully removed anime from collection.', 'success'); $this->setFlashMessage('Successfully removed anime from collection.', 'success');
$this->redirect('/anime-collection/view', 303); $this->redirect('/anime-collection/view', 303);
} }
} }
// End of CollectionController.php // End of AnimeCollection.php

View File

@ -112,7 +112,7 @@ final class AnimeCollection extends Collection {
// Add genres associated with each item // Add genres associated with each item
$rows = $query->fetchAll(PDO::FETCH_ASSOC); $rows = $query->fetchAll(PDO::FETCH_ASSOC);
$genres = $this->getGenresForList(); $genres = $this->getGenreList();
foreach($rows as &$row) foreach($rows as &$row)
{ {
@ -275,29 +275,6 @@ final class AnimeCollection extends Collection {
return $output; 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 * Update genre information for selected anime
* *
@ -353,9 +330,16 @@ final class AnimeCollection extends Collection {
* @return array * @return array
*/ */
private function getGenreData(): array private function getGenreData(): array
{
return [
'genres' => $this->getExistingGenres(),
'links' => $this->getExistingGenreLinkEntries(),
];
}
private function getExistingGenres(): array
{ {
$genres = []; $genres = [];
$links = [];
// Get existing genres // Get existing genres
$query = $this->db->select('id, genre') $query = $this->db->select('id, genre')
@ -366,7 +350,13 @@ final class AnimeCollection extends Collection {
$genres[$genre['id']] = $genre['genre']; $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') $query = $this->db->select('hummingbird_id, genre_id')
->from('genre_anime_set_link') ->from('genre_anime_set_link')
->get(); ->get();
@ -375,17 +365,13 @@ final class AnimeCollection extends Collection {
if (array_key_exists($link['hummingbird_id'], $links)) if (array_key_exists($link['hummingbird_id'], $links))
{ {
$links[$link['hummingbird_id']][] = $link['genre_id']; $links[$link['hummingbird_id']][] = $link['genre_id'];
} } else
else
{ {
$links[$link['hummingbird_id']] = [$link['genre_id']]; $links[$link['hummingbird_id']] = [$link['genre_id']];
} }
} }
return [ return $links;
'genres' => $genres,
'links' => $links
];
} }
} }
// End of AnimeCollectionModel.php // End of AnimeCollectionModel.php

View File

@ -88,21 +88,6 @@ final class MangaCollection extends Collection {
return $output; 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 * Get full collection from the database
* *