/
Handlers.hs
77 lines (61 loc) · 2.16 KB
/
Handlers.hs
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
{-# LANGUAGE OverloadedStrings, DeriveGeneric #-}
module Handlers where
import Web.Scotty
import Network.HTTP.Types.Status
import Control.Monad.IO.Class
import qualified Data.Aeson as A
import Data.Text
import Data.Maybe
import Control.Monad.Writer.Lazy
import GHC.Generics
import State
import Weather
import Result
import qualified Charts as C
import Dice
index :: ActionM ()
index = do
setHeader "Content-Type" "text/html"
file "./static/index.html"
chartList :: [Text]
chartList = ["4-2", "4-3"]
descriptorTable :: Text -> Maybe C.ChartDescriptor
descriptorTable "4-2" = Just $ C.get defaultWeatherInZone
descriptorTable "4-3" = Just $ C.get defaultBadWeatherImpact
descriptorTable _ = Nothing
listCharts :: ActionM ()
listCharts = do
setHeader "Content-Type" "application/json"
json $ [table | table <- [ descriptorTable x | x <- chartList ], isJust table ]
status ok200
getChart :: ActionM ()
getChart = do
chart <- param "chart"
setHeader "Content-Type" "application/json"
case descriptorTable chart of
Just table -> do
json $ A.object ["descriptor" A..= table]
status ok200
Nothing -> do
status notFound404
postChart :: (C.Chart a) => Maybe (a, Int) -> Int -> ActionM ()
postChart maybeRequest numDice = do
setHeader "Content-Type" "application/json"
case maybeRequest of
Just (initial, roll) -> do
case d6 numDice roll of
Just dice -> do
let (newState, results) = runWriter $ C.post initial dice
json $ A.object ["newState" A..= newState, "results" A..= results]
status ok200
Nothing -> do
json $ A.object ["reason" A..= ("Bad Dice" :: Text)]
status badRequest400
Nothing -> do
json $ A.object ["reason" A..= ("Request improper for this URL" :: Text)]
status badRequest400
error404 :: ActionM ()
error404 = do
setHeader "Content-Type" "application/json"
json $ A.object ["reason" A..= ("Endpoint does not exist" :: Text)]
status notFound404