Fixed #225, regression #184

This commit is contained in:
emeric
2022-05-18 12:51:25 +02:00
parent 17f8819d59
commit 703d510c2f
+16 -31
View File
@@ -22,6 +22,7 @@
#include <atomic> #include <atomic>
#include <ctime> #include <ctime>
#include <iomanip> #include <iomanip>
#include <map>
#include <unordered_map> #include <unordered_map>
#include <Wt/WLocalDateTime.h> #include <Wt/WLocalDateTime.h>
@@ -1065,46 +1066,30 @@ handleGetArtistsRequestCommon(RequestContext& context, bool id3)
break; break;
} }
Response::Node* currentIndexNode {}; std::map<char, std::vector<Artist::pointer>> artistsSortedByFirstChar;
char currentIndex{};
Response::Node* unknownIndexNode {};
auto getOrCreateUnknownIndexNode {[&]
{
if (!unknownIndexNode)
{
unknownIndexNode = &artistsNode.createArrayChild("index");
unknownIndexNode->setAttribute("name", "?");
}
return unknownIndexNode;
}};
auto getOrCreateIndexNode {[&](char first)
{
if (!currentIndexNode || currentIndex != first)
{
currentIndexNode = &artistsNode.createArrayChild("index");
currentIndexNode->setAttribute("name", std::string {first});
currentIndex = first;
}
return currentIndexNode;
}};
const RangeResults<ArtistId> artists {Artist::find(context.dbSession, parameters)}; const RangeResults<ArtistId> artists {Artist::find(context.dbSession, parameters)};
for (const ArtistId artistId : artists.results) for (const ArtistId artistId : artists.results)
{ {
const Artist::pointer artist {Artist::find(context.dbSession, artistId)}; const Artist::pointer artist {Artist::find(context.dbSession, artistId)};
const std::string& sortName {artist->getSortName()}; const std::string& sortName {artist->getSortName()};
Response::Node* indexNode{}; char sortChar;
if (sortName.empty() || !std::isalpha(sortName[0])) if (sortName.empty() || !std::isalpha(sortName[0]))
indexNode = getOrCreateUnknownIndexNode(); sortChar = '?';
else else
indexNode = getOrCreateIndexNode(std::toupper(sortName[0])); sortChar = std::toupper(sortName[0]);
indexNode->addArrayChild("artist", artistToResponseNode(user, artist, id3)); artistsSortedByFirstChar[sortChar].push_back(artist);
}
for (const auto& [sortChar, artists] : artistsSortedByFirstChar)
{
Response::Node& indexNode {artistsNode.createArrayChild("index")};
indexNode.setAttribute("name", std::string {sortChar});
for (const Artist::pointer& artist :artists)
indexNode.addArrayChild("artist", artistToResponseNode(user, artist, id3));
} }
return response; return response;