Add simple (and bad) frontend

This commit is contained in:
2022-12-07 19:37:39 +01:00
parent 4be962a6fb
commit 5bd3832d27
9 changed files with 286 additions and 10 deletions
+13 -3
View File
@@ -70,11 +70,11 @@ locations = all_ garfieldDb.locations
-- Inserts
runIns
runInserts
:: Connection
-> SqlInsert Postgres table
-> [SqlInsert Postgres table]
-> IO ()
runIns conn i = runBeamPostgresDebug putStrLn conn $ runInsert i
runInserts conn is = runBeamPostgresDebug putStrLn conn $ mapM_ runInsert is
transfer
:: InventoryItemId -- ^ to
@@ -97,6 +97,16 @@ transfer from to amount
(val_ $ Text.pack $ printf "Umbuchung von %d" $ from.unInventoryItemId)
]
-- Updates
runUpdates :: Connection -> [SqlUpdate Postgres table] -> IO ()
runUpdates conn us = runBeamPostgresDebug putStrLn conn $ mapM_ runUpdate us
disableItem :: InventoryItemId -> SqlUpdate Postgres InventoryItemT
disableItem itemId = update (inventoryItems garfieldDb)
(\it -> it.available <-. val_ False)
(\it -> it.id ==. val_ itemId.unInventoryItemId)
-- Function calls
type SqlFunction a = Connection -> IO a
+2 -2
View File
@@ -24,9 +24,9 @@ import Jon.Server (JonAPI, jonSwaggerDoc, server)
main :: IO ()
main = withGarfieldConn $ \conn ->
run 8080 $ serve p $ server conn :<|> swaggerSchemaUIServer jonSwaggerDoc
run 8080 $ serve p $ server conn :<|> swaggerSchemaUIServer jonSwaggerDoc :<|> serveDirectoryFileServer "./static"
where
p :: Proxy (JonAPI :<|> SwaggerSchemaUI "swagger" "swagger.json")
p :: Proxy (JonAPI :<|> SwaggerSchemaUI "swagger" "swagger.json" :<|> Raw)
p = Proxy
withGarfieldConn :: (Connection -> IO a) -> IO a
+19 -4
View File
@@ -49,6 +49,9 @@ type JonAPI =
:> Description "If `amount` is negative, its absolute value is transferred in the opposite direction."
:> ReqBody '[JSON] TransferInventoryP
:> PostNoContent
:<|> "disableItems" :> Summary "Disable inventory items"
:> ReqBody '[JSON] DisableItemsP
:> PostNoContent
:<|> SnackAPI
)
@@ -88,11 +91,19 @@ data GetActiveItemsP = GetActiveItemsP
} deriving (Generic, FromJSON, ToSchema)
data TransferInventoryP = TransferInventoryP
{ transfers :: [InventoryTransferDTO]
} deriving (Generic, FromJSON, ToSchema)
data InventoryTransferDTO = InventoryTransfer
{ from :: InventoryItemId
, to :: InventoryItemId
, amount :: Int64
} deriving (Generic, FromJSON, ToSchema)
data DisableItemsP = DisableItemsP
{ items :: [InventoryItemId]
} deriving (Generic, FromJSON, ToSchema)
data CreateSnackP = CreateSnackP
{ name :: Text
, barcode :: Text
@@ -163,6 +174,7 @@ server conn =
:<|> getActiveItems
:<|> getLocations
:<|> transferInventory
:<|> disableItems
:<|> createSnack
:<|> updateSnack
where
@@ -187,11 +199,14 @@ server conn =
getLocations = do
liftIO $ Queries.runSelect conn Queries.locations
transferInventory :: TransferInventoryP -> Handler NoContent
transferInventory params = do
liftIO $ Queries.runIns conn $ Queries.transfer
params.from
params.to
params.amount
liftIO $ Queries.runInserts conn $ map (\t -> Queries.transfer t.from t.to t.amount) params.transfers
pure NoContent
disableItems :: DisableItemsP -> Handler NoContent
disableItems params = do
liftIO $ Queries.runUpdates conn $ map Queries.disableItem params.items
pure NoContent
createSnack params = do