From b066fc2531c78df8d54aff08c52b79adbbed15b6 Mon Sep 17 00:00:00 2001 From: Hugo van Rijswijk Date: Sat, 27 Jan 2024 12:27:19 +0100 Subject: [PATCH] feat: show price in correct currency and decimals (#63) --- Model.scala | 16 ++++++++++++---- project.scala | 6 +++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Model.scala b/Model.scala index 0f0c004..133a571 100644 --- a/Model.scala +++ b/Model.scala @@ -43,11 +43,19 @@ case class GetItemsResponse(items: Seq[ItemWrapper]) derives Decoder case class ItemWrapper(item: Item, store: Store, items_available: Int, display_name: String) derives Decoder given Show[ItemWrapper] = i => - show"${i.store.store_name} has ${if i.item.name.isEmpty then "box" else i.item.name} available for ${i.item.price_including_taxes.toEuros}" + show"${i.store.store_name} has ${if i.item.name.isBlank() then "box" else i.item.name} available for ${i.item.item_price}" -case class Item(name: String, price_including_taxes: Price) -case class Price(minor_units: Int): - def toEuros = show"€${minor_units / 100}" +case class Item(name: String, item_price: Price) + +case class Price(code: String, minor_units: Int, decimals: Int) + +given Show[Price] = Show.show: price => + val currencySymbol = price.code match + case "EUR" => "€" + case "USD" => "$" + case "GBP" => "£" + case _ => price.code + show"$currencySymbol ${price.minor_units / math.pow(10, price.decimals)}" case class Store(store_name: String) diff --git a/project.scala b/project.scala index 8432662..6d3d7ab 100644 --- a/project.scala +++ b/project.scala @@ -1,7 +1,7 @@ //> using scala "3" -//> using dep "org.typelevel::cats-effect::3.5.2" -//> using dep co.fs2::fs2-core::3.9.3 -//> using dep co.fs2::fs2-io::3.9.3 +//> using dep "org.typelevel::cats-effect::3.5.3" +//> using dep co.fs2::fs2-core::3.9.4 +//> using dep co.fs2::fs2-io::3.9.4 //> using dep "com.softwaremill.sttp.client4::circe::4.0.0-M8" //> using dep "com.softwaremill.sttp.client4::cats::4.0.0-M8" //> using dep "io.chrisdavenport::rediculous::0.5.1"