Add getUnsoundBarcodes route

This commit is contained in:
Paul Brinkmeier 2022-12-02 15:55:23 +01:00
parent b673f4dcf5
commit 1c0c1457bd
6 changed files with 90 additions and 16 deletions

View File

@ -24,21 +24,25 @@ library
Jon.Garfield.Queries Jon.Garfield.Queries
Jon.Garfield.Types Jon.Garfield.Types
Jon.Main Jon.Main
Jon.Server
other-modules: other-modules:
Paths_jon Paths_jon
hs-source-dirs: hs-source-dirs:
src src
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
build-depends: build-depends:
base >=4.7 && <5 aeson
, base >=4.7 && <5
, beam-core , beam-core
, beam-postgres , beam-postgres
, bytestring , bytestring
, postgresql-simple , postgresql-simple
, scientific , scientific
, servant
, servant-server , servant-server
, text , text
, time , time
, warp
default-language: Haskell2010 default-language: Haskell2010
executable jon-exe executable jon-exe
@ -47,16 +51,19 @@ executable jon-exe
app app
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends: build-depends:
base >=4.7 && <5 aeson
, base >=4.7 && <5
, beam-core , beam-core
, beam-postgres , beam-postgres
, bytestring , bytestring
, jon , jon
, postgresql-simple , postgresql-simple
, scientific , scientific
, servant
, servant-server , servant-server
, text , text
, time , time
, warp
default-language: Haskell2010 default-language: Haskell2010
test-suite jon-test test-suite jon-test
@ -68,14 +75,17 @@ test-suite jon-test
test test
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends: build-depends:
base >=4.7 && <5 aeson
, base >=4.7 && <5
, beam-core , beam-core
, beam-postgres , beam-postgres
, bytestring , bytestring
, jon , jon
, postgresql-simple , postgresql-simple
, scientific , scientific
, servant
, servant-server , servant-server
, text , text
, time , time
, warp
default-language: Haskell2010 default-language: Haskell2010

View File

@ -1,10 +1,14 @@
haskellPackages: with haskellPackages; [ haskellPackages: with haskellPackages; [
aeson
beam-core beam-core
beam-postgres beam-postgres
bytestring bytestring
postgresql-simple postgresql-simple
servant
servant-server servant-server
servant-swagger-ui
scientific scientific
text text
time time
warp
] ]

View File

@ -13,14 +13,17 @@ extra-source-files:
dependencies: dependencies:
- base >= 4.7 && < 5 - base >= 4.7 && < 5
- aeson
- beam-core - beam-core
- beam-postgres - beam-postgres
- bytestring - bytestring
- postgresql-simple - postgresql-simple
- servant
- servant-server - servant-server
- scientific - scientific
- text - text
- time - time
- warp
ghc-options: ghc-options:
- -Wall - -Wall

View File

@ -18,6 +18,13 @@ import Jon.Garfield.Types
-- Selects -- Selects
runSelect
:: (FromBackendRow Postgres (QExprToIdentity e), Projectible Postgres e)
=> Connection
-> Q Postgres db QBaseScope e
-> IO [QExprToIdentity e]
runSelect conn q = runBeamPostgresDebug putStrLn conn $ runSelectReturningList $ select q
overviewItems overviewItems
:: Q Postgres GarfieldDb s (OverviewT (QExpr Postgres s), InventoryItemT (QExpr Postgres s)) :: Q Postgres GarfieldDb s (OverviewT (QExpr Postgres s), InventoryItemT (QExpr Postgres s))
overviewItems = do overviewItems = do

View File

@ -7,36 +7,37 @@ module Jon.Main
, runIns , runIns
) where ) where
import Control.Exception (bracket)
import Database.Beam import Database.Beam
import Database.Beam.Postgres import Database.Beam.Postgres
import Servant (Proxy(..), serve)
import System.Environment import System.Environment
import Network.Wai.Handler.Warp (run)
import qualified Data.ByteString.Char8 as BS8 import qualified Data.ByteString.Char8 as BS8
import Jon.Server (JonAPI, server)
main :: IO () main :: IO ()
main = pure () main = withGarfieldConn (run 8080 . serve (Proxy :: Proxy JonAPI) . server)
withGarfieldConn = bracket
(do pass <- getEnv "JON_PASS"
connectPostgreSQL $ BS8.pack $ "host=localhost dbname=garfield password=" ++ pass)
close
runQuery runQuery
:: (FromBackendRow Postgres (QExprToIdentity e), Projectible Postgres e) :: (FromBackendRow Postgres (QExprToIdentity e), Projectible Postgres e)
=> Q Postgres db QBaseScope e => Q Postgres db QBaseScope e
-> IO [QExprToIdentity e] -> IO [QExprToIdentity e]
runQuery q = do runQuery q = withGarfieldConn $ \conn -> runBeamPostgresDebug putStrLn conn $ runSelectReturningList $ select q
pass <- getEnv "JON_PASS"
conn <- connectPostgreSQL $ BS8.pack $ "host=localhost dbname=garfield password=" ++ pass
runBeamPostgresDebug putStrLn conn $ runSelectReturningList $ select q
runIns runIns
:: SqlInsert Postgres table :: SqlInsert Postgres table
-> IO () -> IO ()
runIns i = do runIns i = withGarfieldConn $ \conn -> runBeamPostgresDebug putStrLn conn $ runInsert i
pass <- getEnv "JON_PASS"
conn <- connectPostgreSQL $ BS8.pack $ "host=localhost dbname=garfield password=" ++ pass
runBeamPostgresDebug putStrLn conn $ runInsert i
runFunction runFunction
:: (Connection -> IO a) :: (Connection -> IO a)
-> IO a -> IO a
runFunction f = do runFunction = withGarfieldConn
pass <- getEnv "JON_PASS"
conn <- connectPostgreSQL $ BS8.pack $ "host=localhost dbname=garfield password=" ++ pass
f conn

49
src/Jon/Server.hs Normal file
View File

@ -0,0 +1,49 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedRecordDot #-}
module Jon.Server where
import Control.Monad.IO.Class (liftIO)
import Data.Aeson (FromJSON, ToJSON)
import Data.Text (Text)
import Data.Int (Int32)
import Database.PostgreSQL.Simple (Connection)
import GHC.Generics (Generic)
import Servant
import Jon.Garfield.Types
import qualified Jon.Garfield.Queries as Queries
-- API and types
type JonAPI = "getUnsoundBarcodes" :> ReqBody '[JSON] GetUnsoundBarcodesP :> Post '[JSON] GetUnsoundBarcodesR
data GetUnsoundBarcodesP = GetUnsoundBarcodesP
{ location :: Int32
} deriving (Show, Generic, FromJSON)
data GetUnsoundBarcodesR = GetUnsoundBarcodesR
{ unsoundBarcodes :: [UnsoundBarcode]
} deriving (Show, Generic, ToJSON)
data UnsoundBarcode = UnsoundBarcode
{ barcode :: Text
, name :: Text
, entries :: Int
, unitsLeft :: Int
} deriving (Show, Generic, ToJSON)
-- server
server :: Connection -> Server JonAPI
server conn = getUnsoundBarcodes
where
getUnsoundBarcodes :: GetUnsoundBarcodesP -> Handler GetUnsoundBarcodesR
getUnsoundBarcodes params = do
rows <- liftIO $ Queries.runSelect conn $ Queries.unsoundBarcodes $ mkLocationId params.location
pure $ GetUnsoundBarcodesR $ map mkUnsoundBarcode rows
where
mkUnsoundBarcode (a, b, c, d) = UnsoundBarcode a b (fromIntegral c) (fromIntegral d)