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

Show: Split sorting in a separate function

Also ensure that sorting by release date is functionnal
parent fa9de2ad
......@@ -203,7 +203,14 @@ std::shared_ptr<Show> Show::create( MediaLibraryPtr ml, const std::string& name
Query<IShow> Show::listAll( MediaLibraryPtr ml, const QueryParameters* params )
{
std::string req = "FROM " + policy::ShowTable::Name + " ORDER BY ";
std::string req = "FROM " + policy::ShowTable::Name;
req += orderBy( params );
return make_query<Show, IShow>( ml, "*", std::move( req ) );
}
std::string Show::orderBy( const QueryParameters* params )
{
std::string req = " ORDER BY ";
SortingCriteria sort = params != nullptr ? params->sort : SortingCriteria::Default;
switch ( sort )
{
......@@ -219,7 +226,7 @@ Query<IShow> Show::listAll( MediaLibraryPtr ml, const QueryParameters* params )
}
if ( params != nullptr && params->desc == true )
req += " DESC";
return make_query<Show, IShow>( ml, "*", req );
return req;
}
}
......@@ -74,6 +74,9 @@ class Show : public IShow, public DatabaseHelpers<Show, policy::ShowTable>
static Query<IShow> listAll( MediaLibraryPtr ml, const QueryParameters* params );
private:
static std::string orderBy( const QueryParameters* params );
protected:
MediaLibraryPtr m_ml;
......
......@@ -202,8 +202,11 @@ TEST_F( Shows, SetEpisodeTvdbId )
TEST_F( Shows, ListAll )
{
auto show1 = ml->createShow( "aaaa" );
show1->setReleaseDate( 5 );
auto show2 = ml->createShow( "zzzz" );
show2->setReleaseDate( 1 );
auto show3 = ml->createShow( "pppp" );
show3->setReleaseDate( 10 );
auto shows = ml->shows( nullptr )->all();
ASSERT_EQ( 3u, shows.size() );
......@@ -217,6 +220,14 @@ TEST_F( Shows, ListAll )
ASSERT_EQ( show2->id(), shows[0]->id() );
ASSERT_EQ( show3->id(), shows[1]->id() );
ASSERT_EQ( show1->id(), shows[2]->id() );
params.sort = SortingCriteria::ReleaseDate;
params.desc = false;
shows = ml->shows( &params )->all();
ASSERT_EQ( 3u, shows.size() );
ASSERT_EQ( show2->id(), shows[0]->id() );
ASSERT_EQ( show1->id(), shows[1]->id() );
ASSERT_EQ( show3->id(), shows[2]->id() );
}
TEST_F( Shows, ListEpisodes )
......
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