2017-05-01 2 views
0

Ich muss einen Web-Service erstellen, um zwischen verschiedenen Währungen mit dem scotty web framework in Haskell zu konvertieren.Scotty Web Service

Der Webservice sollte antworten, um Anfragen wie/convert/15? Zu = usd & von = eur zu erhalten.

Ich habe diesen Code so weit:

{-# 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>"] 

Wenn Sie also in der browswer ausführen: http://localhost:3000/Tony das Ergebnis ist: Hallo, Tony, was los ist! Das Problem ist, dass ich nicht weiß, wie man den Code ändert, um '/ convert/15? To = usd & from = eur.' als Anfrage und erhalten Sie die richtige Antwort.

Hoffentlich kann mir jeder helfen.

Vielen Dank im Voraus.

Edited mit Endlösung:

{-# 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 
+0

Sie haben /: convert /: amount, so dass Sie zwei Captures vor der Abfragezeichenfolge haben. Es muss "/ convert /: amount" sein, no **: ** vor convert, – sumo

+0

Dies war definitiv die Lösung, vielen Dank! –

Antwort

1

Mit Blick auf die docs Sie müssen param anrufen zu bekommen, was Sie brauchen.

diese als Ausgangspunkt Versuchen:

{-# 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>" 

Ich werde die Umwandlung Stück verlassen für Sie herausfinden. Die Verwendung von <> anstelle von mconcat erschien auch sauberer.