Reputation: 493
I need to create a web service to convert between different currencies using the scotty web framework in Haskell.
The web service should respond to get requests such as /convert/15?to=usd&from=eur.
I have this code so far:
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Data.Monoid (mconcat)
functionxs :: a -> Int
functionxs a = 5
main = scotty 3000 $ do
get "/:word" $ do
name <- param "word"
html $ mconcat ["<h1>Hi, ", name, " what's up!</h1>"]
Thus, when you execute in the browswer: http://localhost:3000/Tony the result is: Hi, Tony, what's up!
The problem is that I don't know how to change the code in order to have '/convert/15?to=usd&from=eur.' as request and get the proper answer.
Hopefully anyone can help me.
Thanks in advance.
Edited with final solution:
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Data.Monoid (mconcat)
import Data.Text.Lazy (Text, pack)
main = scotty 3000 $ do
get "/convert/:amount" $ do
money <- param "amount"
to <- param "to"
frm <- param "from"
html $ mconcat ["<h1>The result is: ", pack (show (convert
money to frm)), "</h1>"]
convert :: Double -> String -> String -> Double
convert a b c = if (b=="euro" && c=="usd")
then (a*1.091)
else if (b=="usd" && c=="euro")
then (a*0.915)
else 0
Upvotes: 0
Views: 241
Reputation: 155
Looking at the docs you need to call param to get what you need.
Try this as starting point:
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Data.Monoid
main = scotty 3000 $ do
get "/convert/:amt" $ do
amt <- param "amt"
frm <- param "from"
to <- param "to"
html $ "<h1>" <> amt <>" in " <> frm <> " is " <> to <> "</h1>"
I'll leave the conversion piece for you to figure out. Using <>
instead of mconcat seemed cleaner as well.
Upvotes: 2