Use MBID for internal path when possible

This commit is contained in:
emeric
2021-05-21 23:28:50 +02:00
parent 43fa93bb39
commit 52447c0b56
5 changed files with 61 additions and 14 deletions
+8 -2
View File
@@ -277,7 +277,10 @@ LmsApplication::finalize()
Wt::WLink
LmsApplication::createArtistLink(Database::Artist::pointer artist)
{
return Wt::WLink {Wt::LinkType::InternalPath, "/artist/" + std::to_string(artist.id())};
if (const auto mbid {artist->getMBID()})
return Wt::WLink {Wt::LinkType::InternalPath, "/artist/mbid/" + std::string {mbid->getAsString()}};
else
return Wt::WLink {Wt::LinkType::InternalPath, "/artist/" + std::to_string(artist.id())};
}
std::unique_ptr<Wt::WAnchor>
@@ -298,7 +301,10 @@ LmsApplication::createArtistAnchor(Database::Artist::pointer artist, bool addTex
Wt::WLink
LmsApplication::createReleaseLink(Database::Release::pointer release)
{
return Wt::WLink {Wt::LinkType::InternalPath, "/release/" + std::to_string(release.id())};
if (const auto mbid {release->getMBID()})
return Wt::WLink {Wt::LinkType::InternalPath, "/release/mbid/" + std::string {mbid->getAsString()}};
else
return Wt::WLink {Wt::LinkType::InternalPath, "/release/" + std::to_string(release.id())};
}
std::unique_ptr<Wt::WAnchor>
+3 -3
View File
@@ -34,19 +34,19 @@ class LmsApplicationException : public LmsException
class ArtistNotFoundException : public LmsApplicationException
{
public:
ArtistNotFoundException(Database::IdType artistId) : LmsApplicationException {Wt::WString::tr("Lms.Error.artist-not-found").arg(artistId)} {}
ArtistNotFoundException() : LmsApplicationException {Wt::WString::tr("Lms.Error.artist-not-found")} {}
};
class ReleaseNotFoundException : public LmsApplicationException
{
public:
ReleaseNotFoundException(Database::IdType releaseId) : LmsApplicationException {Wt::WString::tr("Lms.Error.release-not-found").arg(releaseId)} {}
ReleaseNotFoundException() : LmsApplicationException {Wt::WString::tr("Lms.Error.release-not-found")} {}
};
class UserNotFoundException : public LmsApplicationException
{
public:
UserNotFoundException(Database::IdType userId) : LmsApplicationException {Wt::WString::tr("Lms.Error.user-not-found").arg(userId)} {}
UserNotFoundException() : LmsApplicationException {Wt::WString::tr("Lms.Error.user-not-found")} {}
};
class UserNotAllowedException : public LmsApplicationException
+3 -3
View File
@@ -84,7 +84,7 @@ class UserModel : public Wt::WFormModel
// Update user
Database::User::pointer user {Database::User::getById(LmsApp->getDbSession(), *_userId)};
if (!user)
throw UserNotFoundException {*_userId};
throw UserNotFoundException {};
if (_authPasswordService && !valueText(PasswordField).empty())
_authPasswordService->setPassword(LmsApp->getDbSession(), user.id(), valueText(PasswordField).toUTF8());
@@ -117,7 +117,7 @@ class UserModel : public Wt::WFormModel
const Database::User::pointer user {Database::User::getById(LmsApp->getDbSession(), *_userId)};
if (!user)
throw UserNotFoundException {*_userId};
throw UserNotFoundException {};
else if (user == LmsApp->getUser())
throw UserNotAllowedException {};
}
@@ -215,7 +215,7 @@ UserView::refreshView()
const Database::User::pointer user {Database::User::getById(LmsApp->getDbSession(), *userId)};
if (!user)
throw UserNotFoundException {*userId};
throw UserNotFoundException {};
t->bindString("title", Wt::WString::tr("Lms.Admin.User.user-edit").arg(user->getLoginName()), Wt::TextFormat::Plain);
t->setCondition("if-has-last-login", true);
+23 -3
View File
@@ -64,6 +64,26 @@ Artist::Artist(Filters* filters)
refreshView();
}
static
std::optional<IdType>
extractArtistIdFromInternalPath()
{
if (wApp->internalPathMatches("/artist/mbid/"))
{
const auto mbid {UUID::fromString(wApp->internalPathNextPart("/artist/mbid/"))};
if (mbid)
{
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
if (const Database::Artist::pointer artist {Database::Artist::getByMBID(LmsApp->getDbSession(), *mbid)})
return artist.id();
}
return std::nullopt;
}
return StringUtils::readAs<Database::IdType>(wApp->internalPathNextPart("/artist/"));
}
void
Artist::refreshView()
{
@@ -72,9 +92,9 @@ Artist::refreshView()
clear();
const auto artistId {StringUtils::readAs<Database::IdType>(wApp->internalPathNextPart("/artist/"))};
const auto artistId {extractArtistIdFromInternalPath()};
if (!artistId)
throw ArtistNotFoundException {*artistId};
throw ArtistNotFoundException {};
const auto similarArtistIds {Service<Recommendation::IEngine>::get()->getSimilarArtists(LmsApp->getDbSession(),
*artistId,
@@ -85,7 +105,7 @@ Artist::refreshView()
const Database::Artist::pointer artist {Database::Artist::getById(LmsApp->getDbSession(), *artistId)};
if (!artist)
throw ArtistNotFoundException {*artistId};
throw ArtistNotFoundException {};
refreshLinks(artist);
refreshSimilarArtists(similarArtistIds);
+24 -3
View File
@@ -67,6 +67,27 @@ Release::Release(Filters* filters)
refreshView();
}
static
std::optional<IdType>
extractReleaseIdFromInternalPath()
{
if (wApp->internalPathMatches("/release/mbid/"))
{
const auto mbid {UUID::fromString(wApp->internalPathNextPart("/release/mbid/"))};
if (mbid)
{
auto transaction {LmsApp->getDbSession().createSharedTransaction()};
if (const Database::Release::pointer release {Database::Release::getByMBID(LmsApp->getDbSession(), *mbid)})
return release.id();
}
return std::nullopt;
}
return StringUtils::readAs<Database::IdType>(wApp->internalPathNextPart("/release/"));
}
void
Release::refreshView()
{
@@ -75,9 +96,9 @@ Release::refreshView()
clear();
const auto releaseId {StringUtils::readAs<Database::IdType>(wApp->internalPathNextPart("/release/"))};
const auto releaseId {extractReleaseIdFromInternalPath()};
if (!releaseId)
throw ReleaseNotFoundException {*releaseId};
throw ReleaseNotFoundException {};
auto similarReleasesIds {Service<Recommendation::IEngine>::get()->getSimilarReleases(LmsApp->getDbSession(), *releaseId, 6)};
@@ -85,7 +106,7 @@ Release::refreshView()
const Database::Release::pointer release {Database::Release::getById(LmsApp->getDbSession(), *releaseId)};
if (!release)
throw ReleaseNotFoundException {*releaseId};
throw ReleaseNotFoundException {};
refreshCopyright(release);
refreshLinks(release);