feat: changing to new rest countries api

as returned by https://restcountries.com/v3.1/all
This commit is contained in:
efim
2023-09-27 05:29:15 +00:00
parent 2070bbebb0
commit f383085910
8 changed files with 80 additions and 16892 deletions

View File

@@ -4,29 +4,45 @@ import upickle.default._
import scala.jdk.CollectionConverters._
final case class Country(
name: String,
alpha3Code: String,
nativeName: String = "",
population: Int,
region: String,
subregion: String,
flag: String,
capital: String = "",
topLevelDomain: Option[String], // maybe optional?
currencies: List[Currency] = List.empty,
languages: List[Language],
borders: List[String] = List.empty,
name: Name,
cca3: String,
population: Int,
region: String,
subregion: String = "",
flags: Flags,
capital: List[String] = List.empty,
tld: Option[String] = None, // maybe optional?
currencies: Map[String, Currency] = Map.empty,
languages: Map[String, String] = Map.empty,
borders: List[String] = List.empty
) derives ReadWriter {
def currenciesView = currencies.map(_.name).asJava
def languagesView = languages.map(_.name).asJava
def currenciesView = currencies.values.map(_.name).toList.asJava
def languagesView = languages.values.toList.asJava
def nameView = name.common
def capitalView = capital.headOption.getOrElse("")
def nativeName = name.nativeName.headOption.map(_._2.common).getOrElse("")
def topLevelDomain = tld
def alpha3Code = cca3
def flag = flags.svg
}
final case class Name(
common: String,
nativeName: Map[String, Name.Native] = Map.empty
) derives ReadWriter
object Name {
final case class Native(
common: String
) derives ReadWriter
}
final case class Currency(
code: String,
name: String
name: String,
symbol: String = ""
) derives ReadWriter
final case class Language(
name: String,
nativeName: String = ""
final case class Flags(
png: String,
svg: String,
alt: String = ""
) derives ReadWriter

View File

@@ -27,7 +27,7 @@ object MinimalApplication extends cask.Routes{
def loadCountries() = {
val countries: List[Country] = upickle.default.read[List[Country]](
scala.io.Source.fromResource("temporary/data.json").getLines().mkString,
scala.io.Source.fromResource("temporary/all.json").getLines().mkString,
true
)
countries

View File

@@ -106,12 +106,12 @@ case class Routes(countries: List[Country])(implicit
@cask.get("/country")
def getCountryPage(countryName: String) = {
val context = new Context()
countries.find(_.name == countryName) match {
countries.find(_.name.common == countryName) match {
case Some(selectedCountry) =>
context.setVariable("country", selectedCountry)
val borderCountries = countries
.filter(c => selectedCountry.borders.contains(c.alpha3Code))
.map(_.name)
.map(_.name.common)
.sortBy(_.length())
.asJava