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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} module Lib ( webServer ) where import Control.Monad.IO.Class (liftIO) import Webpage import Cart import Cookie import Catalogue import ShowItem import Stripe import Data.Foldable (for_) import Models import Web.Scotty import Text.Blaze.Html5 ((!)) import qualified Web.Scotty as S import qualified Text.Blaze.Html5 as H import qualified Text.Blaze.Html5.Attributes as A import Database.SQLite.Simple as SQL import Network.HTTP.Client import Network.HTTP.Client.TLS import Data.Text.Lazy import Text.Blaze.Html.Renderer.Text webServer :: IO () webServer = do conn <- SQL.open "mckb.db" manager <- newManager tlsManagerSettings accountDetails <- details manager print accountDetails session <- checkout manager scotty 3000 $ do get "/" $ do items <- liftIO $ catalogue conn render $ webpage $ items get "/about" $ do render $ webpage $ H.div "it's a shop with keyboards" get "/cart" $ do render $ webpage cart get "/item/:id" $ do itemId :: Text <- param "id" (item :: [Item]) <- liftIO $ SQL.query conn "select * from items where id = (?)" (Only itemId) render $ webpage $ showItem $ Prelude.head item post "/pay" $ do S.redirect $ pack session get "/insert" $ do render $ webpage $ H.div $ do H.form ! A.method "post" ! A.action "/item" $ do H.input ! A.type_ "text" ! A.name "name" H.input ! A.type_ "text" ! A.name "description" H.input ! A.type_ "submit" ! A.value "submit" post "/item" $ do (name :: String) <- S.param "name" (description :: String) <- S.param "description" liftIO $ SQL.execute conn "INSERT INTO items (name, description) VALUES (?, ?)" (name, description) S.redirect "/" get "/style.css" $ do file "assets/style.css" get "/assets/keeb.jpg" $ do file "assets/keeb.jpg" get "/cookie" $ do text :: String <- param "text" setCookie "cookie_test" $ toStrict $ pack text S.redirect "/" notFound $ do render $ webpage $ H.div "this webpage does not exist" close conn render = S.html . renderHtml |