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 setStatus status
text message) 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 -- Exports
mkConfig :: IO (SpockCfg () Session State) mkConfig :: IO (SpockCfg () Session State)
@ -67,6 +72,10 @@ app = do
lectures <- getAllLecturesCached lectures <- getAllLecturesCached
blaze $ either Views.viewSqueakError (Views.viewLectures query) lectures 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 get "login" $ blaze Views.viewLogin
post "login" $ do 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 (Text)
import Data.Text.Encoding (encodeUtf8) import Data.Text.Encoding (encodeUtf8)
import Data.Time.Calendar (Day) import Data.Time.Calendar (Day)
import Data.Time.LocalTime (ZonedTime)
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Network.HTTP.Req (Option, Scheme(..), POST(..), ReqBodyJson(..), Url, defaultHttpConfig, header, http, port, jsonResponse, responseBody, req, runReq) 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 parseJSON = withObject "Documents" $ \v -> Documents
<$> ((v .: "documents") >>= (.: "results")) <$> ((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. -- | Helper for automatically writing 'FromJSON' instances.
-- --
-- >>> removePrefixModifier "document" "documentId" -- >>> removePrefixModifier "document" "documentId"
@ -182,12 +190,20 @@ getDocumentsByLectureId :: (SqueakCtx m, MonadIO m) => Text -> m (Either SqueakE
getDocumentsByLectureId lecture = fmap unDocuments <$> joinErrors <$> mkQueryWithVars getDocumentsByLectureId lecture = fmap unDocuments <$> joinErrors <$> mkQueryWithVars
(object ["lecture" .= lecture]) (object ["lecture" .= lecture])
"query DocumentsByLectureId($lecture: LectureId!) \ "query DocumentsByLectureId($lecture: LectureId!) \
\ { documents(filters: [{ lectures: [$lecture] }]) \ \{ documents(filters: [{ lectures: [$lecture] }]) \
\ { results \ \ { results \
\ { id date semester publicComment downloadable faculty { id displayName } lectures { id displayName aliases } type numPages } \ \ { id date semester publicComment downloadable faculty { id displayName } lectures { id displayName aliases } type numPages } \
\ } \ \ } \
\}" \}"
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 -- Mutations
data LoginResult = LoginResult { unLoginResult :: SqueakErrorOr Credentials } data LoginResult = LoginResult { unLoginResult :: SqueakErrorOr Credentials }
@ -255,8 +271,10 @@ instance (FromJSON CreateOrderResult) where
data Order = Order data Order = Order
{ orderId :: Text { orderId :: Text
, orderTag :: Text , orderCreated :: ZonedTime
, orderNumPages :: Int , orderNumPages :: Int
, orderPrice :: Double
, orderTag :: Text
} }
deriving (Generic, Show) 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 Text.Blaze.Html5.Attributes hiding (form, label)
import Lisa.Types (Session) import Lisa.Types (Session)
import Lisa.Squeak (Document(..), Lecture(..), SqueakError(..)) import Lisa.Squeak (Document(..), Lecture(..), Order(..), SqueakError(..))
viewIndex :: Html viewIndex :: Html
viewIndex = do viewIndex = do
@ -20,6 +20,7 @@ viewIndex = do
ul $ do ul $ do
li $ a "Login" ! href "/login" li $ a "Login" ! href "/login"
li $ a "Vorlesungen" ! href "/lectures" li $ a "Vorlesungen" ! href "/lectures"
li $ a "Bestellungen" ! href "/orders"
li $ a "Debuginformation" ! href "/debug" li $ a "Debuginformation" ! href "/debug"
viewLectures :: Text -> [Lecture] -> Html viewLectures :: Text -> [Lecture] -> Html
@ -49,6 +50,17 @@ viewLecture lecture documents = do
td $ text $ documentSemester document td $ text $ documentSemester document
td $ string $ show $ documentNumPages 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 :: Html
viewLogin = do viewLogin = do
h1 $ text "FSMI-Login" h1 $ text "FSMI-Login"