aboutsummaryrefslogtreecommitdiff
path: root/src/executable/haskell/View
diff options
context:
space:
mode:
Diffstat (limited to 'src/executable/haskell/View')
-rw-r--r--src/executable/haskell/View/Ad.hs58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/executable/haskell/View/Ad.hs b/src/executable/haskell/View/Ad.hs
new file mode 100644
index 0000000..ba0c550
--- /dev/null
+++ b/src/executable/haskell/View/Ad.hs
@@ -0,0 +1,58 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module View.Ad
+ ( renderConsoleAds
+ , renderAds
+ ) where
+
+import qualified Data.Maybe as Maybe
+import Data.Text (Text)
+import qualified Data.Text as T
+
+import Model.Ad (Ad)
+import qualified Model.Ad as Ad
+
+renderConsoleAds :: Text -> [Ad] -> Text
+renderConsoleAds time ads =
+ let (title, message) = renderAds ads
+ titleWithTime =
+ T.concat
+ [ "\n["
+ , time
+ , "] "
+ , title
+ ]
+ line = T.map (\_ -> '-') titleWithTime
+ in T.intercalate
+ "\n"
+ [ titleWithTime
+ , line
+ , ""
+ , message
+ ]
+
+renderAds :: [Ad] -> (Text, Text)
+renderAds ads =
+ let titleMessage = renderTitle $ length ads
+ adsMessage = T.intercalate "\n\n" . map renderAd $ ads
+ in (titleMessage, adsMessage)
+
+renderTitle :: Int -> Text
+renderTitle count =
+ T.concat
+ [ T.pack . show $ count
+ , agreement " nouvelle"
+ , agreement " annonce"
+ ]
+ where agreement word =
+ T.concat
+ [ word
+ , if count > 1 then "s" else ""
+ ]
+
+renderAd :: Ad -> Text
+renderAd ad =
+ let formatPrice price = T.concat [" - ", price]
+ getPrice = Maybe.fromMaybe "" . fmap formatPrice . Ad.price $ ad
+ titleLine = T.concat [Ad.name ad, Ad.location ad, getPrice]
+ in T.intercalate "\n" [titleLine, Ad.url ad]