diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-13 21:11:13 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-13 21:11:13 +0100 |
commit | c3911983dbe7a1c383b3887846cf5a01444879ce (patch) | |
tree | 168bad75989ff606973c900de47c5f3b722b97d7 | |
parent | a6f85b9b8894a7817baad1a5e850366d02eb197a (diff) | |
download | uni-c3911983dbe7a1c383b3887846cf5a01444879ce.tar uni-c3911983dbe7a1c383b3887846cf5a01444879ce.tar.gz uni-c3911983dbe7a1c383b3887846cf5a01444879ce.tar.bz2 uni-c3911983dbe7a1c383b3887846cf5a01444879ce.tar.xz uni-c3911983dbe7a1c383b3887846cf5a01444879ce.zip |
FFP 11.2
-rw-r--r-- | ws2015/ffp/blaetter/11/FFP_U11-2_Yesod.hs | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/ws2015/ffp/blaetter/11/FFP_U11-2_Yesod.hs b/ws2015/ffp/blaetter/11/FFP_U11-2_Yesod.hs index 83f7945..156e002 100644 --- a/ws2015/ffp/blaetter/11/FFP_U11-2_Yesod.hs +++ b/ws2015/ffp/blaetter/11/FFP_U11-2_Yesod.hs | |||
@@ -17,6 +17,7 @@ | |||
17 | module Main where | 17 | module Main where |
18 | 18 | ||
19 | import Yesod | 19 | import Yesod |
20 | import Data.Text (Text) | ||
20 | import qualified Data.Text as T | 21 | import qualified Data.Text as T |
21 | 22 | ||
22 | {- | 23 | {- |
@@ -58,26 +59,46 @@ main = warp 3000 CalcApp | |||
58 | 59 | ||
59 | data CalcApp = CalcApp | 60 | data CalcApp = CalcApp |
60 | 61 | ||
61 | instance Yesod CalcApp | 62 | data ArithOp = ArithAdd |
63 | | ArithMult | ||
64 | deriving (Show, Eq, Read) | ||
65 | |||
66 | instance PathPiece ArithOp where | ||
67 | fromPathPiece "plus" = Just ArithAdd | ||
68 | fromPathPiece "mal" = Just ArithMult | ||
69 | fromPathPiece _ = Nothing | ||
70 | toPathPiece ArithAdd = "plus" | ||
71 | toPathPiece ArithMult = "mal" | ||
62 | 72 | ||
63 | mkYesod "CalcApp" [parseRoutes| | 73 | mkYesod "CalcApp" [parseRoutes| |
64 | / HomeR GET | 74 | / IndexR GET |
75 | !#Int/#ArithOp/#Int/ist MathR GET | ||
76 | !#Text/#ArithOp/#Text/ist MathErrR GET | ||
65 | |] | 77 | |] |
66 | 78 | ||
67 | 79 | getIndexR :: Handler Html | |
68 | getHomeR :: Handler Html | 80 | getIndexR = defaultLayout $ do |
69 | getHomeR = defaultLayout $ do | 81 | setTitle "math!" |
70 | setTitle "Hello!" | 82 | [whamlet| |
71 | let x = 2 | 83 | <h1>Math! |
72 | let y = 3 | 84 | <p>We support: |
73 | toWidget [whamlet| | 85 | <ul> |
74 | <h2>Hello World! | 86 | <li>plus |
75 | <p> Some text that is <i>displayed</i> here. | 87 | <li>mal |
76 | <p> We have #{show x}+#{show y}=#{show $ x + y}! | 88 | |] |
77 | |] | 89 | |
78 | 90 | instance Yesod CalcApp where | |
79 | 91 | errorHandler NotFound = fmap toTypedContent getIndexR | |
80 | 92 | ||
93 | getMathR :: Int -> ArithOp -> Int -> Handler Text | ||
94 | getMathR x op y = return . T.pack . show $ runCalc op x y | ||
95 | where | ||
96 | runCalc ArithAdd = (+) | ||
97 | runCalc ArithMult = (*) | ||
98 | |||
99 | getMathErrR :: Text -> ArithOp -> Text -> Handler Text | ||
100 | getMathErrR _ _ _ = return . T.pack $ "Nur ganze Zahlen!" | ||
101 | |||
81 | 102 | ||
82 | 103 | ||
83 | -- | 104 | -- |