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

Add sorting criteria & plug it with media listing

parent 14b70d36
......@@ -49,6 +49,21 @@ namespace medialibrary
MediaSearchAggregate media;
std::vector<PlaylistPtr> playlists;
};
enum class SortingCriteria
{
/*
* Default depends on the entity type:
* - By track number (and disc number) for album tracks
* - Alphabetical order for others
*/
Default,
Alpha,
Duration,
InsertionDate,
LastModificationDate,
ReleaseDate,
};
}
class IMediaLibraryCb
......@@ -116,8 +131,8 @@ class IMediaLibrary
virtual LabelPtr createLabel( const std::string& label ) = 0;
virtual bool deleteLabel( LabelPtr label ) = 0;
virtual std::vector<MediaPtr> audioFiles() = 0;
virtual std::vector<MediaPtr> videoFiles() = 0;
virtual std::vector<MediaPtr> audioFiles( medialibrary::SortingCriteria sort, bool desc ) = 0;
virtual std::vector<MediaPtr> videoFiles( medialibrary::SortingCriteria sort, bool desc ) = 0;
virtual AlbumPtr album( unsigned int id ) = 0;
virtual std::vector<AlbumPtr> albums() = 0;
virtual ShowPtr show( const std::string& name ) = 0;
......
......@@ -348,10 +348,43 @@ void Media::removeFile( File& file )
}));
}
std::vector<MediaPtr> Media::listAll( MediaLibraryPtr ml, IMedia::Type type )
std::vector<MediaPtr> Media::listAll( MediaLibraryPtr ml, IMedia::Type type, medialibrary::SortingCriteria sort, bool desc )
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name + " WHERE type = ? AND is_present = 1 ORDER BY title";
return DatabaseHelpers::fetchAll<IMedia>( ml, req, type );
std::string req;
if ( sort == medialibrary::SortingCriteria::LastModificationDate )
{
req = "SELECT m.* FROM " + policy::MediaTable::Name + " m INNER JOIN "
+ policy::FileTable::Name + " f ON m.id_media = f.media_id"
" WHERE m.type = ?"
" AND ( f.type = ? OR f.type = ? )"
" ORDER BY f.last_modification_date";
if ( desc == true )
req += " DESC";
return fetchAll<IMedia>( ml, req, type, File::Type::Entire, File::Type::Main );
}
req = "SELECT * FROM " + policy::MediaTable::Name + " WHERE type = ? AND is_present = 1 ORDER BY ";
switch ( sort )
{
case medialibrary::SortingCriteria::Alpha:
case medialibrary::SortingCriteria::Default:
req += "title";
break;
case medialibrary::SortingCriteria::Duration:
req += "duration";
break;
case medialibrary::SortingCriteria::InsertionDate:
req += "insertion_date";
break;
case medialibrary::SortingCriteria::ReleaseDate:
req += "release_date";
break;
default:
break;
}
if ( desc == true )
req += " DESC";
return fetchAll<IMedia>( ml, req, type );
}
unsigned int Media::id() const
......
......@@ -105,7 +105,7 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
std::shared_ptr<File> addFile( const fs::IFile& fileFs, Folder& parentFolder, fs::IDirectory& parentFolderFs , IFile::Type type);
void removeFile( File& file );
static std::vector<MediaPtr> listAll( MediaLibraryPtr ml, Type type );
static std::vector<MediaPtr> listAll(MediaLibraryPtr ml, Type type , medialibrary::SortingCriteria sort, bool desc);
static std::vector<MediaPtr> search( MediaLibraryPtr ml, const std::string& title );
static std::vector<MediaPtr> fetchHistory( MediaLibraryPtr ml );
......
......@@ -226,14 +226,14 @@ void MediaLibrary::setVerbosity(LogLevel v)
Log::setLogLevel( v );
}
std::vector<MediaPtr> MediaLibrary::audioFiles()
std::vector<MediaPtr> MediaLibrary::audioFiles( medialibrary::SortingCriteria sort, bool desc )
{
return Media::listAll( this, IMedia::Type::AudioType );
return Media::listAll( this, IMedia::Type::AudioType, sort, desc );
}
std::vector<MediaPtr> MediaLibrary::videoFiles()
std::vector<MediaPtr> MediaLibrary::videoFiles( medialibrary::SortingCriteria sort, bool desc )
{
return Media::listAll( this, IMedia::Type::VideoType );
return Media::listAll( this, IMedia::Type::VideoType, sort, desc );
}
std::shared_ptr<Media> MediaLibrary::addFile( const fs::IFile& fileFs, Folder& parentFolder, fs::IDirectory& parentFolderFs )
......
......@@ -56,8 +56,9 @@ class MediaLibrary : public IMediaLibrary
virtual bool initialize( const std::string& dbPath, const std::string& thumbnailPath, IMediaLibraryCb* metadataCb ) override;
virtual void setVerbosity( LogLevel v ) override;
virtual std::vector<MediaPtr> audioFiles() override;
virtual std::vector<MediaPtr> videoFiles() override;
virtual std::vector<MediaPtr> audioFiles( medialibrary::SortingCriteria sort, bool desc) override;
virtual std::vector<MediaPtr> videoFiles( medialibrary::SortingCriteria sort, bool desc) override;
std::shared_ptr<Media> addFile( const fs::IFile& fileFs, Folder& parentFolder, fs::IDirectory& parentFolderFs );
bool deleteFolder(const Folder& folder );
......
......@@ -78,6 +78,11 @@ std::shared_ptr<Media> MediaLibraryTester::addFile( const std::string& path )
return MediaLibrary::addFile( file, dummyFolder, *dummyDirectory );
}
std::shared_ptr<Media> MediaLibraryTester::addFile( fs::IFile& file )
{
return MediaLibrary::addFile( file, dummyFolder, *dummyDirectory );
}
std::shared_ptr<Playlist> MediaLibraryTester::playlist(unsigned int playlistId)
{
return Playlist::fetch( this, playlistId );
......
......@@ -46,6 +46,7 @@ public:
void setFsFactory( std::shared_ptr<factory::IFileSystem> fsFactory );
void deleteTrack( unsigned int trackId );
std::shared_ptr<AlbumTrack> albumTrack( unsigned int id );
std::shared_ptr<Media> addFile(fs::IFile& file);
private:
std::unique_ptr<fs::IDirectory> dummyDirectory;
......
......@@ -199,12 +199,14 @@ class NoopFile : public fs::IFile
std::string m_path;
std::string m_fileName;
std::string m_extension;
unsigned int m_lastModifDate;
public:
NoopFile( const std::string& file )
: m_path( file )
, m_fileName( utils::file::fileName( file ) )
, m_extension( utils::file::extension( file ) )
, m_lastModifDate( 123 )
{
}
......@@ -232,7 +234,12 @@ public:
{
// Ensure a non-0 value so tests can easily verify that the value
// is initialized
return 123;
return m_lastModifDate;
}
void setLastModificationDate( unsigned int date )
{
m_lastModifDate = date;
}
};
......
......@@ -70,12 +70,12 @@ TEST_P( Tests, Parse )
checkMedias( expected["media"] );
if ( expected.HasMember( "nbVideos" ) == true )
{
const auto videos = m_ml->videoFiles();
const auto videos = m_ml->videoFiles( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( expected["nbVideos"].GetUint(), videos.size() );
}
if ( expected.HasMember( "nbAudios" ) == true )
{
const auto audios = m_ml->audioFiles();
const auto audios = m_ml->audioFiles( medialibrary::SortingCriteria::Default, false );
ASSERT_EQ( expected["nbAudios"].GetUint(), audios.size() );
}
if ( expected.HasMember( "artists" ) )
......
......@@ -28,6 +28,7 @@
#include "Artist.h"
#include "Album.h"
#include "AlbumTrack.h"
#include "mocks/FileSystem.h"
class Medias : public Tests
{
......@@ -311,3 +312,45 @@ TEST_F( Medias, SetReleaseDate )
auto m2 = ml->media( m->id() );
ASSERT_EQ( m2->releaseDate(), 1234u );
}
TEST_F( Medias, SortByAlpha )
{
auto m1 = ml->addFile( "media1.mp3" );
m1->setTitle( "Abcd" );
m1->save();
auto m2 = ml->addFile( "media2.mp3" );
m2->setTitle( "Zyxw" );
m2->save();
auto media = ml->audioFiles( medialibrary::SortingCriteria::Alpha, false );
ASSERT_EQ( 2u, media.size() );
ASSERT_EQ( m1->id(), media[0]->id() );
ASSERT_EQ( m2->id(), media[1]->id() );
media = ml->audioFiles( medialibrary::SortingCriteria::Alpha, true );
ASSERT_EQ( 2u, media.size() );
ASSERT_EQ( m2->id(), media[0]->id() );
ASSERT_EQ( m1->id(), media[1]->id() );
}
TEST_F( Medias, SortByLastModifDate )
{
auto file1 = std::make_shared<mock::NoopFile>( "media.mkv" );
file1->setLastModificationDate( 666 );
auto m1 = ml->addFile( *file1 );
auto file2 = std::make_shared<mock::NoopFile>( "media2.mkv" );
file2->setLastModificationDate( 111 );
auto m2 = ml->addFile( *file2 );
auto media = ml->videoFiles( medialibrary::SortingCriteria::LastModificationDate, false );
ASSERT_EQ( 2u, media.size() );
ASSERT_EQ( m2->id(), media[0]->id() );
ASSERT_EQ( m1->id(), media[1]->id() );
media = ml->videoFiles( medialibrary::SortingCriteria::LastModificationDate, true );
ASSERT_EQ( 2u, media.size() );
ASSERT_EQ( m2->id(), media[1]->id() );
ASSERT_EQ( m1->id(), media[0]->id() );
}
......@@ -31,6 +31,7 @@ class Tests : public testing::Test
protected:
std::unique_ptr<MediaLibraryTester> ml;
std::unique_ptr<mock::NoopCallback> cbMock;
std::shared_ptr<factory::IFileSystem> fsFactory;
virtual void SetUp() override;
virtual void InstantiateMediaLibrary();
......
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