Commit 17bd66de authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Album: Update duration as part of a trigger

In order to update the duration when a media gets removed, similar to
what was done for the number of tracks for #432
parent 4ad27893
Pipeline #206305 passed with stage
in 41 seconds
......@@ -382,20 +382,7 @@ Query<IMedia> Album::searchTracks( const std::string& pattern,
bool Album::addTrack( std::shared_ptr<Media> media, unsigned int trackNb,
unsigned int discNumber, int64_t artistId, Genre* genre )
{
/*
* The transaction should already exist, but in order to avoid sprinkling
* all the tests with a transaction block, we just create one here. It's
* likely to be a noop transaction outside of the tests but it doesn't matter
* much.
*/
auto t = m_ml->getConn()->newTransaction();
static const std::string req = "UPDATE " + Table::Name + " SET "
"duration = duration + ? "
"WHERE id_album = ?";
auto duration = media->duration() >= 0 ? media->duration() : 0;
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, duration,
m_id ) == false )
return false;
if ( media->markAsAlbumTrack( m_id, trackNb, discNumber, artistId, genre ) == false )
return false;
if ( genre != nullptr && genre->updateNbTracks( 1 ) == false )
......@@ -413,7 +400,6 @@ bool Album::addTrack( std::shared_ptr<Media> media, unsigned int trackNb,
if ( ( m_tracks.empty() == true && m_nbTracks == 1 ) ||
( m_tracks.empty() == false && m_nbTracks > 1 ) )
m_tracks.push_back( std::move( media ) );
t->commit();
return true;
}
......@@ -754,11 +740,13 @@ std::string Album::trigger( Triggers trigger, uint32_t dbModel )
" BEGIN"
" UPDATE " + Table::Name + " SET "
" is_present = is_present - IIF(old.is_present != 0, 1, 0),"
" nb_tracks = nb_tracks - 1"
" nb_tracks = nb_tracks - 1,"
" duration = duration - IIF(old.duration >= 0, old.duration, 0)"
" WHERE old.album_id IS NOT NULL AND id_album = old.album_id;"
" UPDATE " + Table::Name + " SET "
" is_present = is_present + IIF(old.is_present != 0, 1, 0),"
" nb_tracks = nb_tracks + 1"
" nb_tracks = nb_tracks + 1,"
" duration = duration + IIF(new.duration >= 0, new.duration, 0)"
" WHERE new.album_id IS NOT NULL AND id_album = new.album_id;"
" END";
}
......
......@@ -888,16 +888,22 @@ static void ConvertToExternal( Tests* T )
T->ml->addMedia( "track.mp3", IMedia::Type::Audio ) );
auto m2 = std::static_pointer_cast<Media>(
T->ml->addMedia( "track2.mp3", IMedia::Type::Audio ) );
auto res = m->setDuration( 10 );
ASSERT_TRUE( res );
res = m2->setDuration( 90 );
ASSERT_TRUE( res );
auto res = a->addTrack( m, 1, 1, 0, nullptr );
res = a->addTrack( m, 1, 1, 0, nullptr );
ASSERT_TRUE( res );
res = a->addTrack( m2, 2, 1, 0, nullptr );
ASSERT_TRUE( res );
ASSERT_EQ( 2u, a->nbTracks() );
ASSERT_EQ( 100, a->duration() );
a = std::static_pointer_cast<Album>( T->ml->album( a->id() ) );
ASSERT_EQ( 2u, a->nbTracks() );
ASSERT_EQ( 2u, a->nbPresentTracks() );
ASSERT_EQ( 100, a->duration() );
auto deviceId = m->deviceId();
auto folderId = m->folderId();
......@@ -920,6 +926,7 @@ static void ConvertToExternal( Tests* T )
a = std::static_pointer_cast<Album>( T->ml->album( a->id() ) );
ASSERT_EQ( 1u, a->nbTracks() );
ASSERT_EQ( 1u, a->nbPresentTracks() );
ASSERT_EQ( 90, a->duration() );
res = m->markAsAlbumTrack( a->id(), 1, 1, 0, nullptr );
ASSERT_TRUE( res );
......@@ -927,6 +934,7 @@ static void ConvertToExternal( Tests* T )
a = std::static_pointer_cast<Album>( T->ml->album( a->id() ) );
ASSERT_EQ( 2u, a->nbTracks() );
ASSERT_EQ( 2u, a->nbPresentTracks() );
ASSERT_EQ( 100, a->duration() );
res = m->convertToExternal();
ASSERT_TRUE( res );
......
Supports Markdown
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