aboutsummaryrefslogtreecommitdiff
path: root/src/executable/haskell/View/Ad.hs
blob: 90025d9cc860252197d85565bb75fa54bec0434c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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 price = Maybe.fromMaybe "" . fmap (\p -> T.concat [" - ", p]) . Ad.price $ ad
      titleLine = T.concat [Ad.name ad, " - ", Ad.location ad, price]
  in  T.intercalate "\n" [titleLine, Ad.url ad]