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
+6
View File
@@ -277,6 +277,9 @@ LmsApplication::finalize()
Wt::WLink Wt::WLink
LmsApplication::createArtistLink(Database::Artist::pointer artist) LmsApplication::createArtistLink(Database::Artist::pointer artist)
{ {
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())}; return Wt::WLink {Wt::LinkType::InternalPath, "/artist/" + std::to_string(artist.id())};
} }
@@ -298,6 +301,9 @@ LmsApplication::createArtistAnchor(Database::Artist::pointer artist, bool addTex
Wt::WLink Wt::WLink
LmsApplication::createReleaseLink(Database::Release::pointer release) LmsApplication::createReleaseLink(Database::Release::pointer release)
{ {
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())}; return Wt::WLink {Wt::LinkType::InternalPath, "/release/" + std::to_string(release.id())};
} }
+3 -3
View File
@@ -34,19 +34,19 @@ class LmsApplicationException : public LmsException
class ArtistNotFoundException : public LmsApplicationException class ArtistNotFoundException : public LmsApplicationException
{ {
public: 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 class ReleaseNotFoundException : public LmsApplicationException
{ {
public: 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 class UserNotFoundException : public LmsApplicationException
{ {
public: 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 class UserNotAllowedException : public LmsApplicationException
+3 -3
View File
@@ -84,7 +84,7 @@ class UserModel : public Wt::WFormModel
// Update user // Update user
Database::User::pointer user {Database::User::getById(LmsApp->getDbSession(), *_userId)}; Database::User::pointer user {Database::User::getById(LmsApp->getDbSession(), *_userId)};
if (!user) if (!user)
throw UserNotFoundException {*_userId}; throw UserNotFoundException {};
if (_authPasswordService && !valueText(PasswordField).empty()) if (_authPasswordService && !valueText(PasswordField).empty())
_authPasswordService->setPassword(LmsApp->getDbSession(), user.id(), valueText(PasswordField).toUTF8()); _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)}; const Database::User::pointer user {Database::User::getById(LmsApp->getDbSession(), *_userId)};
if (!user) if (!user)
throw UserNotFoundException {*_userId}; throw UserNotFoundException {};
else if (user == LmsApp->getUser()) else if (user == LmsApp->getUser())
throw UserNotAllowedException {}; throw UserNotAllowedException {};
} }
@@ -215,7 +215,7 @@ UserView::refreshView()
const Database::User::pointer user {Database::User::getById(LmsApp->getDbSession(), *userId)}; const Database::User::pointer user {Database::User::getById(LmsApp->getDbSession(), *userId)};
if (!user) 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->bindString("title", Wt::WString::tr("Lms.Admin.User.user-edit").arg(user->getLoginName()), Wt::TextFormat::Plain);
t->setCondition("if-has-last-login", true); t->setCondition("if-has-last-login", true);
+23 -3
View File
@@ -64,6 +64,26 @@ Artist::Artist(Filters* filters)
refreshView(); 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 void
Artist::refreshView() Artist::refreshView()
{ {
@@ -72,9 +92,9 @@ Artist::refreshView()
clear(); clear();
const auto artistId {StringUtils::readAs<Database::IdType>(wApp->internalPathNextPart("/artist/"))}; const auto artistId {extractArtistIdFromInternalPath()};
if (!artistId) if (!artistId)
throw ArtistNotFoundException {*artistId}; throw ArtistNotFoundException {};
const auto similarArtistIds {Service<Recommendation::IEngine>::get()->getSimilarArtists(LmsApp->getDbSession(), const auto similarArtistIds {Service<Recommendation::IEngine>::get()->getSimilarArtists(LmsApp->getDbSession(),
*artistId, *artistId,
@@ -85,7 +105,7 @@ Artist::refreshView()
const Database::Artist::pointer artist {Database::Artist::getById(LmsApp->getDbSession(), *artistId)}; const Database::Artist::pointer artist {Database::Artist::getById(LmsApp->getDbSession(), *artistId)};
if (!artist) if (!artist)
throw ArtistNotFoundException {*artistId}; throw ArtistNotFoundException {};
refreshLinks(artist); refreshLinks(artist);
refreshSimilarArtists(similarArtistIds); refreshSimilarArtists(similarArtistIds);
+24 -3
View File
@@ -67,6 +67,27 @@ Release::Release(Filters* filters)
refreshView(); 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 void
Release::refreshView() Release::refreshView()
{ {
@@ -75,9 +96,9 @@ Release::refreshView()
clear(); clear();
const auto releaseId {StringUtils::readAs<Database::IdType>(wApp->internalPathNextPart("/release/"))}; const auto releaseId {extractReleaseIdFromInternalPath()};
if (!releaseId) if (!releaseId)
throw ReleaseNotFoundException {*releaseId}; throw ReleaseNotFoundException {};
auto similarReleasesIds {Service<Recommendation::IEngine>::get()->getSimilarReleases(LmsApp->getDbSession(), *releaseId, 6)}; 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)}; const Database::Release::pointer release {Database::Release::getById(LmsApp->getDbSession(), *releaseId)};
if (!release) if (!release)
throw ReleaseNotFoundException {*releaseId}; throw ReleaseNotFoundException {};
refreshCopyright(release); refreshCopyright(release);
refreshLinks(release); refreshLinks(release);