Set up some tables and basic queries

This commit is contained in:
2022-12-02 15:20:05 +01:00
parent 424d037c22
commit b673f4dcf5
10 changed files with 574 additions and 5 deletions
+101
View File
@@ -0,0 +1,101 @@
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
module Jon.Garfield.Queries where
import Data.Int (Int64)
import Data.Scientific (Scientific)
import Data.Text (Text)
import Database.Beam
import Database.Beam.Postgres
import Database.PostgreSQL.Simple
import Text.Printf (printf)
import qualified Data.Text as Text
import Jon.Garfield.Types
-- Selects
overviewItems
:: Q Postgres GarfieldDb s (OverviewT (QExpr Postgres s), InventoryItemT (QExpr Postgres s))
overviewItems = do
ov <- all_ garfieldDb.overview
it <- related_ garfieldDb.inventoryItems ov.itemId
pure (ov, it)
overviewItemsByLocation
:: LocationId
-> Q Postgres GarfieldDb s (OverviewT (QExpr Postgres s), InventoryItemT (QExpr Postgres s))
overviewItemsByLocation loc = do
row@(_, it) <- overviewItems
guard_ $ it.location ==. val_ loc
pure row
unsoundBarcodes
:: LocationId
-> Q Postgres GarfieldDb s (QExpr Postgres s Text, QExpr Postgres s Text, QExpr Postgres s Int64, QExpr Postgres s Int64)
unsoundBarcodes loc =
filter_ (\(_, _, entries, _) -> entries >=. 2) $
aggregate_
(\(ov, it) ->
( group_ it.barcode
, fromMaybe_ "" $ max_ it.name
, as_ @Int64 countAll_
, as_ @Int64 $ cast_ (sum_ ov.unitsLeft) int
))
(overviewItemsByLocation loc)
activeItems
:: Text -- barcode
-> LocationId
-> Q Postgres GarfieldDb s (OverviewT (QExpr Postgres s))
activeItems barcode loc = do
(ov, it) <- overviewItemsByLocation loc
guard_ $ it.barcode ==. val_ barcode
guard_ $ it.available ==. val_ True
pure ov
-- Inserts
transfer
:: InventoryItemId -- ^ to
-> InventoryItemId -- ^ from
-> Int64 -- ^ amount to transfer. If negative, acts like 'transfer b a (-amount)'
-> SqlInsert Postgres CorrectionT
transfer from to amount
| amount < 0 = transfer to from (-amount)
| otherwise = insert garfieldDb.inventoryCorrections $
insertExpressions
[ Correction
(val_ from)
default_
(val_ $ -amount)
(val_ $ Text.pack $ printf "Umbuchung auf %d" $ to.unInventoryItemId)
, Correction
(val_ to)
default_
(val_ amount)
(val_ $ Text.pack $ printf "Umbuchung von %d" $ from.unInventoryItemId)
]
-- Function calls
type SqlFunction a = Connection -> IO a
snackDelete :: SnackId -> SqlFunction ()
snackDelete snack conn = do
[Only ()] <- query conn "SELECT garfield.snack_delete(?)" (Only $ snack.unSnackId)
pure ()
snackCreate :: Text -> Text -> Scientific -> TaxGroupId -> LocationId -> SqlFunction SnackId
snackCreate name barcode price taxGroup location conn = do
[Only rawSnackId] <- query conn "SELECT garfield.snack_create(?, ?, ?, ?, ?)"
(name, barcode, price, taxGroup.unTaxGroupId, location.unLocationId)
pure $ mkSnackId rawSnackId
inventoryMapSnack :: SnackId -> InventoryItemId -> SqlFunction ()
inventoryMapSnack snack item conn = do
[Only ()] <- query conn "SELECT garfield.inventory_map_snack(?, ?)" (snack.unSnackId, item.unInventoryItemId)
pure ()