{-# LANGUAGE DataKinds #-} {-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE ViewPatterns #-} module Server.ControlRoom (ControlRoom(..)) where import Control.Monad (unless, when) import Control.Monad.IO.Class (MonadIO (liftIO)) import qualified Data.ByteString.Char8 as C8 import qualified Data.Map as M import Data.Swagger (ToSchema (..)) import Data.Text (Text) import qualified Data.Text as T import Web.FormUrlEncoded (ToForm) import Web.Internal.FormUrlEncoded (Form) import Control.Monad (forM_) import Control.Monad.Extra (maybeM, whenM) import Data.Functor ((<&>)) import Data.Map (Map) import Data.Pool (Pool) import Data.Time (getCurrentTime, utctDay) import Data.Time.Calendar (Day) import Data.Time.Format.ISO8601 (iso8601Show) import Data.UUID (UUID) import qualified Data.UUID as UUID import Database.Persist (Entity (..), delete, entityVal, get, insert, selectList, (==.)) import Database.Persist.Sql (SqlBackend, PersistFieldSql, runSqlPool) import GHC.Generics (Generic) import Persist (AmendmentStatus, Announcement (..), EntityField (..), Key (..), ScheduleAmendment (ScheduleAmendment), runSql) import Server.Util (Service, redirect) import Text.ProtocolBuffers (Default (defaultValue)) import Web.FormUrlEncoded (FromForm) import Fmt ((+|), (|+)) import Yesod import Data.List (lookup) import Text.Read (readMaybe) import Text.Blaze.Html (ToMarkup(..)) import qualified Data.Vector as V import GTFS data ControlRoom = ControlRoom { getBaseurl :: Text , getGtfs :: GTFS , getPool :: Pool SqlBackend } mkYesod "ControlRoom" [parseRoutes| /main MainR GET /train/#TripID/#Day TrainViewR GET |] instance Yesod ControlRoom where approot = ApprootMaster (\cr -> getBaseurl cr) -- which backend we're using and how to run an action. instance YesodPersist ControlRoom where type YesodPersistBackend ControlRoom = SqlBackend runDB action = do pool <- getYesod <&> getPool runSqlPool action pool getMainR :: Handler Html getMainR = do req <- getRequest let maybeDay = lookup "day" (reqGetParams req) >>= (readMaybe . T.unpack) day <- liftIO $ maybeM (getCurrentTime <&> utctDay) pure (pure maybeDay) gtfs <- getYesod <&> getGtfs let trips = tripsOnDay gtfs day defaultLayout [whamlet|