diff src/core/anime_db.cc @ 284:e66ffc338d82

anime: refactor title structure to a map
author Paper <paper@paper.us.eu.org>
date Wed, 08 May 2024 16:21:05 -0400
parents 657fda1b9cac
children 53e3c015a973
line wrap: on
line diff
--- a/src/core/anime_db.cc	Wed May 08 16:20:37 2024 -0400
+++ b/src/core/anime_db.cc	Wed May 08 16:21:05 2024 -0400
@@ -157,11 +157,6 @@
 	// clang-format off
 	json = {
 		{"id", anime.GetId()},
-		{"title", {
-			{"native", anime.GetNativeTitle()},
-			{"romaji", anime.GetRomajiTitle()},
-			{"english", anime.GetEnglishTitle()}
-		}},
 		{"synonyms", anime.GetTitleSynonyms()},
 		{"episodes", anime.GetEpisodes()},
 		{"airing_status", Translate::ToString(anime.GetAiringStatus())},
@@ -177,6 +172,13 @@
 	};
 	// clang-format on
 
+	/* now for dynamically-filled stuff */
+	for (const auto& lang : TitleLanguages) {
+		std::optional<std::string> title = anime.GetTitle(lang);
+		if (title.has_value())
+			json["title"][Strings::ToLower(Translate::ToString(lang))] = title.value();
+	}
+
 	nlohmann::json user;
 	if (GetListDataAsJSON(anime, user))
 		json.push_back({"list_data", user});
@@ -236,9 +238,12 @@
 	Anime& anime = database.items[id];
 
 	anime.SetId(id);
-	anime.SetNativeTitle(JSON::GetString<std::string>(json, "/title/native"_json_pointer, ""));
-	anime.SetRomajiTitle(JSON::GetString<std::string>(json, "/title/romaji"_json_pointer, ""));
-	anime.SetEnglishTitle(JSON::GetString<std::string>(json, "/title/english"_json_pointer, ""));
+	for (const auto& lang : TitleLanguages) {
+		nlohmann::json::json_pointer p("/title/" + Strings::ToLower(Translate::ToString(lang)));
+
+		if (json.contains(p) && json[p].is_string())
+			anime.SetTitle(lang, json[p].get<std::string>());
+	}
 	anime.SetTitleSynonyms(JSON::GetArray<std::vector<std::string>>(json, "/synonyms"_json_pointer, {}));
 	anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0));
 	anime.SetAiringStatus(