Add orders route

This commit is contained in:
Paul Brinkmeier 2022-09-21 23:40:06 +02:00
parent 6763d3ffa1
commit 98ef505453
3 changed files with 42 additions and 3 deletions

View File

@ -45,6 +45,11 @@ onLeft f (status, message) = f >>= (flip either pure $ \_ -> do
setStatus status
text message)
onLeftBlaze :: MonadIO m => (ActionCtxT ctx m (Either e a)) -> (Status, e -> Html) -> ActionCtxT ctx m a
onLeftBlaze f (status, view) = f >>= (flip either pure $ \err -> do
setStatus status
blaze $ view err)
-- Exports
mkConfig :: IO (SpockCfg () Session State)
@ -67,6 +72,10 @@ app = do
lectures <- getAllLecturesCached
blaze $ either Views.viewSqueakError (Views.viewLectures query) lectures
get "orders" $ do
orders <- Squeak.getAllPendingOrders `onLeftBlaze` (internalServerError500, Views.viewSqueakError)
blaze $ Views.viewOrders orders
get "login" $ blaze Views.viewLogin
post "login" $ do

View File

@ -11,6 +11,7 @@ import Data.Aeson (FromJSON(parseJSON), Options(fieldLabelModifier), ToJSON(toJS
import Data.Text (Text)
import Data.Text.Encoding (encodeUtf8)
import Data.Time.Calendar (Day)
import Data.Time.LocalTime (ZonedTime)
import GHC.Generics (Generic)
import Network.HTTP.Req (Option, Scheme(..), POST(..), ReqBodyJson(..), Url, defaultHttpConfig, header, http, port, jsonResponse, responseBody, req, runReq)
@ -102,6 +103,13 @@ instance FromJSON Documents where
parseJSON = withObject "Documents" $ \v -> Documents
<$> ((v .: "documents") >>= (.: "results"))
data Orders = Orders { unOrders :: [Order] }
deriving (Show)
instance FromJSON Orders where
parseJSON = withObject "Orders" $ \v -> Orders
<$> ((v .: "orders") >>= (.: "results"))
-- | Helper for automatically writing 'FromJSON' instances.
--
-- >>> removePrefixModifier "document" "documentId"
@ -188,6 +196,14 @@ getDocumentsByLectureId lecture = fmap unDocuments <$> joinErrors <$> mkQueryWit
\ } \
\}"
getAllPendingOrders :: (SqueakCtx m, MonadIO m) => m (Either SqueakError [Order])
getAllPendingOrders = fmap unOrders <$> joinErrors <$> mkQuery
"{ orders(filters: {state: PENDING}) \
\ { results \
\ { id created numPages price tag } \
\ } \
\}"
-- Mutations
data LoginResult = LoginResult { unLoginResult :: SqueakErrorOr Credentials }
@ -255,8 +271,10 @@ instance (FromJSON CreateOrderResult) where
data Order = Order
{ orderId :: Text
, orderTag :: Text
, orderCreated :: ZonedTime
, orderNumPages :: Int
, orderPrice :: Double
, orderTag :: Text
}
deriving (Generic, Show)

View File

@ -12,7 +12,7 @@ import Text.Blaze.Html5 hiding (map, style)
import Text.Blaze.Html5.Attributes hiding (form, label)
import Lisa.Types (Session)
import Lisa.Squeak (Document(..), Lecture(..), SqueakError(..))
import Lisa.Squeak (Document(..), Lecture(..), Order(..), SqueakError(..))
viewIndex :: Html
viewIndex = do
@ -20,6 +20,7 @@ viewIndex = do
ul $ do
li $ a "Login" ! href "/login"
li $ a "Vorlesungen" ! href "/lectures"
li $ a "Bestellungen" ! href "/orders"
li $ a "Debuginformation" ! href "/debug"
viewLectures :: Text -> [Lecture] -> Html
@ -49,6 +50,17 @@ viewLecture lecture documents = do
td $ text $ documentSemester document
td $ string $ show $ documentNumPages document
viewOrders :: [Order] -> Html
viewOrders orders = do
h1 $ text "Bestellungen"
table $ do
thead $ mapM_ th ["Bezeichner", "Seiten", "Preis", "Erstellt"]
tbody $ forM_ orders $ \order -> tr $ do
td $ text $ orderTag order
td $ string $ show $ orderNumPages order
td $ string $ show $ orderPrice order
td $ string $ show $ orderCreated order
viewLogin :: Html
viewLogin = do
h1 $ text "FSMI-Login"