summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-01-13 21:11:13 +0100
committerGregor Kleen <gkleen@yggdrasil.li>2016-01-13 21:11:13 +0100
commitc3911983dbe7a1c383b3887846cf5a01444879ce (patch)
tree168bad75989ff606973c900de47c5f3b722b97d7
parenta6f85b9b8894a7817baad1a5e850366d02eb197a (diff)
downloaduni-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.hs53
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 @@
17module Main where 17module Main where
18 18
19import Yesod 19import Yesod
20import Data.Text (Text)
20import qualified Data.Text as T 21import qualified Data.Text as T
21 22
22{- 23{-
@@ -58,26 +59,46 @@ main = warp 3000 CalcApp
58 59
59data CalcApp = CalcApp 60data CalcApp = CalcApp
60 61
61instance Yesod CalcApp 62data ArithOp = ArithAdd
63 | ArithMult
64 deriving (Show, Eq, Read)
65
66instance 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
63mkYesod "CalcApp" [parseRoutes| 73mkYesod "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 79getIndexR :: Handler Html
68getHomeR :: Handler Html 80getIndexR = defaultLayout $ do
69getHomeR = 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 90instance Yesod CalcApp where
79 91 errorHandler NotFound = fmap toTypedContent getIndexR
80 92
93getMathR :: Int -> ArithOp -> Int -> Handler Text
94getMathR x op y = return . T.pack . show $ runCalc op x y
95 where
96 runCalc ArithAdd = (+)
97 runCalc ArithMult = (*)
98
99getMathErrR :: Text -> ArithOp -> Text -> Handler Text
100getMathErrR _ _ _ = return . T.pack $ "Nur ganze Zahlen!"
101
81 102
82 103
83-- 104--