Commit 62521d88 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

IShow: Allow show episodes to be sorted

parent 6da22990
......@@ -38,7 +38,7 @@ class IShow
virtual const std::string& shortSummary() const = 0;
virtual const std::string& artworkMrl() const = 0;
virtual const std::string& tvdbId() const = 0;
virtual Query<IShowEpisode> episodes() const = 0;
virtual Query<IShowEpisode> episodes( const QueryParameters* params = nullptr ) const = 0;
virtual uint32_t nbSeasons() const = 0;
virtual uint32_t nbEpisodes() const = 0;
};
......
......@@ -137,10 +137,28 @@ std::shared_ptr<ShowEpisode> Show::addEpisode( Media& media, unsigned int episod
return episode;
}
Query<IShowEpisode> Show::episodes() const
Query<IShowEpisode> Show::episodes( const QueryParameters* params ) const
{
static const std::string req = "FROM " + policy::ShowEpisodeTable::Name
+ " WHERE show_id = ?";
std::string req = "FROM " + policy::ShowEpisodeTable::Name + " ep "
" INNER JOIN " + policy::MediaTable::Name + " med ON ep.media_id = med.id_media "
+ " WHERE ep.show_id = ? ORDER BY ";
auto sort = params != nullptr ? params->sort : SortingCriteria::Default;
auto desc = params != nullptr ? params->desc : false;
switch ( sort )
{
case SortingCriteria::Alpha:
req += "med.name";
if ( desc == true )
req += " DESC";
break;
default:
if ( desc == true )
req += "ep.season_number DESC, ep.episode_number DESC";
else
req += "ep.season_number, ep.episode_number";
break;
}
return make_query<ShowEpisode, IShowEpisode>( m_ml, "*", req, m_id );
}
......
......@@ -63,7 +63,7 @@ class Show : public IShow, public DatabaseHelpers<Show, policy::ShowTable>
virtual const std::string& tvdbId() const override;
bool setTvdbId( const std::string& summary );
std::shared_ptr<ShowEpisode> addEpisode( Media& media, unsigned int episodeNumber );
virtual Query<IShowEpisode> episodes() const override;
virtual Query<IShowEpisode> episodes( const QueryParameters* params ) const override;
virtual uint32_t nbSeasons() const override;
virtual uint32_t nbEpisodes() const override;
......
......@@ -125,7 +125,7 @@ TEST_F( Shows, AddEpisode )
ASSERT_EQ( e->episodeNumber(), 1u );
ASSERT_EQ( e->show(), show );
auto episodes = show->episodes()->all();
auto episodes = show->episodes( nullptr )->all();
ASSERT_EQ( episodes.size(), 1u );
ASSERT_EQ( episodes[0], e );
}
......@@ -163,7 +163,7 @@ TEST_F( Shows, SetEpisodeSeasonNumber )
Reload();
show = std::static_pointer_cast<Show>( ml->show( show->id() ) );
auto episodes = show->episodes()->all();
auto episodes = show->episodes( nullptr )->all();
ASSERT_EQ( episodes[0]->seasonNumber(), e->seasonNumber() );
}
......@@ -179,7 +179,7 @@ TEST_F( Shows, SetEpisodeSummary )
Reload();
show = std::static_pointer_cast<Show>( ml->show( show->id() ) );
auto episodes = show->episodes()->all();
auto episodes = show->episodes( nullptr )->all();
ASSERT_EQ( episodes[0]->shortSummary(), e->shortSummary() );
}
......@@ -195,7 +195,7 @@ TEST_F( Shows, SetEpisodeTvdbId )
Reload();
show = std::static_pointer_cast<Show>( ml->show( show->id() ) );
auto episodes = show->episodes()->all();
auto episodes = show->episodes( nullptr )->all();
ASSERT_EQ( episodes[0]->tvdbId(), e->tvdbId() );
}
......@@ -219,6 +219,35 @@ TEST_F( Shows, ListAll )
ASSERT_EQ( show1->id(), shows[2]->id() );
}
TEST_F( Shows, ListEpisodes )
{
auto show = ml->createShow( "show" );
auto m1 = std::static_pointer_cast<Media>( ml->addMedia( "episode1.avi" ) );
auto s02e01 = show->addEpisode( *m1, 1 );
s02e01->setSeasonNumber( 2 );
auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "episode2.avi" ) );
auto s01e01 = show->addEpisode( *m2, 1 );
s01e01->setSeasonNumber( 1 );
auto m3 = std::static_pointer_cast<Media>( ml->addMedia( "episode3.avi" ) );
auto s01e02 = show->addEpisode( *m3, 2 );
s01e02->setSeasonNumber( 1 );
auto episodes = show->episodes( nullptr )->all();
ASSERT_EQ( 3u, episodes.size() );
ASSERT_EQ( s01e01->id(), episodes[0]->id() );
ASSERT_EQ( s01e02->id(), episodes[1]->id() );
ASSERT_EQ( s02e01->id(), episodes[2]->id() );
QueryParameters params { SortingCriteria::Default, true };
episodes = show->episodes( &params )->all();
ASSERT_EQ( 3u, episodes.size() );
ASSERT_EQ( s02e01->id(), episodes[0]->id() );
ASSERT_EQ( s01e02->id(), episodes[1]->id() );
ASSERT_EQ( s01e01->id(), episodes[2]->id() );
}
////////////////////////////////////////////////////
// Files links:
////////////////////////////////////////////////////
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment