Commit 1aed0f6b authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Add favorite support for streams in history

parent 5d3a7906
......@@ -31,4 +31,6 @@ public:
virtual MediaPtr media() const = 0;
virtual const std::string& mrl() const = 0;
virtual unsigned int insertionDate() const = 0;
virtual bool isFavorite() const = 0;
virtual bool setFavorite( bool isFavorite ) = 0;
};
......@@ -35,11 +35,13 @@ unsigned int History::* const HistoryTable::PrimaryKey = &History::m_id;
constexpr unsigned int History::MaxEntries;
History::History( DBConnection dbConn, sqlite::Row& row )
: m_dbConn( dbConn )
{
row >> m_id
>> m_mrl
>> m_mediaId
>> m_date;
>> m_date
>> m_favorite;
if ( m_mediaId != 0 )
{
m_media = Media::load( dbConn, row );
......@@ -51,9 +53,10 @@ bool History::createTable( DBConnection dbConnection )
static const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::HistoryTable::Name +
"("
"id_record INTEGER PRIMARY KEY AUTOINCREMENT,"
"mrl TEXT UNIQUE ON CONFLICT REPLACE,"
"mrl TEXT UNIQUE ON CONFLICT FAIL,"
"media_id INTEGER UNIQUE ON CONFLICT REPLACE,"
"insertion_date UNSIGNED INT DEFAULT (strftime('%s', 'now')),"
"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"
")";
......@@ -70,6 +73,7 @@ bool History::createTable( DBConnection dbConnection )
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;
......@@ -77,9 +81,15 @@ bool History::insert(DBConnection dbConn, const IMedia& media )
bool History::insert( DBConnection dbConn, const std::string& mrl )
{
static const std::string req = "INSERT INTO " + policy::HistoryTable::Name + "(mrl)"
"VALUES(?)";
return sqlite::Tools::insert( dbConn, req, mrl ) != 0;
History::clear();
static const std::string req = "INSERT OR REPLACE INTO " + policy::HistoryTable::Name +
"(id_record, mrl, insertion_date, favorite)"
" SELECT id_record, mrl, strftime('%s', 'now'), favorite FROM " +
policy::HistoryTable::Name + " WHERE mrl = ?"
" UNION SELECT NULL, ?, NULL, NULL"
" ORDER BY id_record DESC"
" LIMIT 1";
return sqlite::Tools::insert( dbConn, req, mrl, mrl ) != 0;
}
std::vector<std::shared_ptr<IHistoryEntry> > History::fetch( DBConnection dbConn )
......@@ -104,3 +114,20 @@ unsigned int History::insertionDate() const
{
return m_date;
}
bool History::isFavorite() const
{
return m_favorite;
}
bool History::setFavorite( bool isFavorite )
{
if ( isFavorite == m_favorite )
return true;
static const std::string req = "UPDATE " + policy::HistoryTable::Name + " SET favorite = ? WHERE id_record = ?";
if ( sqlite::Tools::executeUpdate( m_dbConn, req, isFavorite, m_id ) == false )
return false;
m_favorite = isFavorite;
return true;
}
......@@ -54,14 +54,19 @@ public:
virtual MediaPtr media() const override;
virtual const std::string& mrl() const override;
virtual unsigned int insertionDate() const override;
virtual bool isFavorite() const override;
virtual bool setFavorite( bool isFavorite ) override;
static constexpr unsigned int MaxEntries = 100u;
private:
DBConnection m_dbConn;
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;
......
......@@ -103,3 +103,29 @@ TEST_F( HistoryTest, DeleteMedia )
hList = ml->history();
ASSERT_EQ( 0u, hList.size() );
}
TEST_F( HistoryTest, FavoriteMrl )
{
ml->addToHistory( "stream" );
auto hList = ml->history();
ASSERT_EQ( 1u, hList.size() );
auto item = hList[0];
ASSERT_FALSE( item->isFavorite() );
item->setFavorite( true );
ASSERT_TRUE( item->isFavorite() );
}
TEST_F( HistoryTest, ReinsertFavorited )
{
ml->addToHistory( "stream" );
auto hList = ml->history();
auto item = hList[0];
auto date = item->insertionDate();
item->setFavorite( true );
std::this_thread::sleep_for( std::chrono::seconds{ 1 } );
ml->addToHistory( "stream" );
hList = ml->history();
item = hList[0];
ASSERT_NE( date, item->insertionDate() );
ASSERT_TRUE( item->isFavorite() );
}
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