Commit 2185a36d authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

History: Don't handle media history in the History table/entity/helper

parent 1aed0f6b
......@@ -22,13 +22,12 @@
#pragma once
#include "Types.h"
#include <string>
class IHistoryEntry
{
public:
virtual ~IHistoryEntry() = default;
virtual MediaPtr media() const = 0;
virtual const std::string& mrl() const = 0;
virtual unsigned int insertionDate() const = 0;
virtual bool isFavorite() const = 0;
......
......@@ -118,7 +118,6 @@ class IMediaLibrary
/**
* History
*/
virtual bool addToHistory( MediaPtr media ) = 0;
virtual bool addToHistory( const std::string& mrl ) = 0;
virtual std::vector<HistoryPtr> history() const = 0;
......
......@@ -22,7 +22,6 @@
#include "History.h"
#include "Media.h"
#include "database/SqliteTools.h"
namespace policy
......@@ -39,13 +38,8 @@ History::History( DBConnection dbConn, sqlite::Row& row )
{
row >> m_id
>> m_mrl
>> m_mediaId
>> m_date
>> m_favorite;
if ( m_mediaId != 0 )
{
m_media = Media::load( dbConn, row );
}
}
bool History::createTable( DBConnection dbConnection )
......@@ -54,11 +48,8 @@ bool History::createTable( DBConnection dbConnection )
"("
"id_record INTEGER PRIMARY KEY AUTOINCREMENT,"
"mrl TEXT UNIQUE ON CONFLICT FAIL,"
"media_id INTEGER UNIQUE ON CONFLICT REPLACE,"
"insertion_date UNSIGNED INT NOT NULL DEFAULT (strftime('%s', 'now')),"
"favorite BOOLEAN NOT NULL DEFAULT 0,"
"FOREIGN KEY (media_id) REFERENCES " + policy::MediaTable::Name
+ "(id_media) ON DELETE CASCADE"
"favorite BOOLEAN NOT NULL DEFAULT 0"
")";
static const std::string triggerReq = "CREATE TRIGGER IF NOT EXISTS limit_nb_records AFTER INSERT ON "
+ policy::HistoryTable::Name +
......@@ -71,14 +62,6 @@ bool History::createTable( DBConnection dbConnection )
sqlite::Tools::executeRequest( dbConnection, triggerReq );
}
bool History::insert(DBConnection dbConn, const IMedia& media )
{
History::clear();
static const std::string req = "INSERT INTO " + policy::HistoryTable::Name + "(media_id)"
"VALUES(?)";
return sqlite::Tools::insert( dbConn, req, media.id() ) != 0;
}
bool History::insert( DBConnection dbConn, const std::string& mrl )
{
History::clear();
......@@ -94,15 +77,8 @@ bool History::insert( DBConnection dbConn, const std::string& mrl )
std::vector<std::shared_ptr<IHistoryEntry> > History::fetch( DBConnection dbConn )
{
static const std::string req = "SELECT * FROM " + policy::HistoryTable::Name + " h "
" LEFT OUTER JOIN " + policy::MediaTable::Name + " m ON m.id_media = h.media_id"
" ORDER BY insertion_date DESC";
return sqlite::Tools::fetchAll<History, IHistoryEntry>( dbConn, req );
}
MediaPtr History::media() const
{
return m_media;
static const std::string req = "SELECT * FROM " + policy::HistoryTable::Name + " ORDER BY insertion_date DESC";
return fetchAll<IHistoryEntry>( dbConn, req );
}
const std::string& History::mrl() const
......
......@@ -47,11 +47,9 @@ class History : public IHistoryEntry, public DatabaseHelpers<History, policy::Hi
public:
History( DBConnection dbConn, sqlite::Row& row );
static bool createTable( DBConnection dbConnection );
static bool insert( DBConnection dbConn, const IMedia& media );
static bool insert( DBConnection dbConn, const std::string& mrl );
static std::vector<std::shared_ptr<IHistoryEntry>> fetch(DBConnection dbConn);
static std::vector<std::shared_ptr<IHistoryEntry>> fetch( DBConnection dbConn );
virtual MediaPtr media() const override;
virtual const std::string& mrl() const override;
virtual unsigned int insertionDate() const override;
virtual bool isFavorite() const override;
......@@ -64,10 +62,8 @@ private:
unsigned int m_id;
std::string m_mrl;
unsigned int m_mediaId;
unsigned int m_date;
bool m_favorite;
std::shared_ptr<Media> m_media;
friend policy::HistoryTable;
};
......@@ -375,13 +375,6 @@ bool MediaLibrary::deletePlaylist( unsigned int playlistId )
return Playlist::destroy( m_dbConnection.get(), playlistId );
}
bool MediaLibrary::addToHistory( MediaPtr media )
{
if ( media == nullptr )
return false;
return History::insert( m_dbConnection.get(), *media );
}
bool MediaLibrary::addToHistory( const std::string& mrl )
{
return History::insert( m_dbConnection.get(), mrl );
......
......@@ -88,7 +88,6 @@ class MediaLibrary : public IMediaLibrary
virtual std::vector<PlaylistPtr> playlists() override;
virtual bool deletePlaylist( unsigned int playlistId ) override;
virtual bool addToHistory( MediaPtr media );
virtual bool addToHistory( const std::string& mrl );
virtual std::vector<HistoryPtr> history() const;
......
......@@ -37,24 +37,10 @@ TEST_F( HistoryTest, InsertMrl )
auto hList = ml->history();
ASSERT_EQ( 1u, hList.size() );
auto h = hList[0];
ASSERT_EQ( nullptr, h->media() );
ASSERT_EQ( h->mrl(), "upnp://stream" );
ASSERT_NE( 0u, h->insertionDate() );
}
TEST_F( HistoryTest, InsertMedia )
{
auto media = ml->addFile( "media.mkv" );
ml->addToHistory( media );
auto hList = ml->history();
ASSERT_EQ( 1u, hList.size() );
auto h = hList[0];
ASSERT_NE( nullptr, h->media() );
ASSERT_EQ( media->id(), h->media()->id() );
ASSERT_EQ( h->mrl(), "" );
ASSERT_NE( 0u, h->insertionDate() );
}
TEST_F( HistoryTest, MaxEntries )
{
for ( auto i = 0u; i < History::MaxEntries; ++i )
......@@ -92,18 +78,6 @@ TEST_F( HistoryTest, UpdateInsertionDate )
ASSERT_NE( date, hList[0]->insertionDate() );
}
TEST_F( HistoryTest, DeleteMedia )
{
auto m = ml->addFile( "media.mkv" );
ml->addToHistory( m );
auto hList = ml->history();
ASSERT_EQ( 1u, hList.size() );
auto f = m->files()[0];
m->removeFile( static_cast<File&>( *f ) );
hList = ml->history();
ASSERT_EQ( 0u, hList.size() );
}
TEST_F( HistoryTest, FavoriteMrl )
{
ml->addToHistory( "stream" );
......
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