From 313df9af39a629974c7a3be5e04708a81be58137 Mon Sep 17 00:00:00 2001 From: nek0 Date: Thu, 19 Dec 2019 02:40:54 +0100 Subject: [PATCH] provide metainformation to frontends --- app/Main.hs | 9 ++++++++- mateamt.cabal | 3 +++ src/API.hs | 11 +++++++++-- src/Control.hs | 1 + src/Control/Meta.hs | 20 ++++++++++++++++++++ src/Model/Product.hs | 2 +- src/Types.hs | 1 + src/Types/Meta.hs | 19 +++++++++++++++++++ src/Types/Reader.hs | 8 ++++++-- 9 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 src/Control/Meta.hs create mode 100644 src/Types/Meta.hs diff --git a/app/Main.hs b/app/Main.hs index 27bf487..8f2db84 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -14,6 +14,7 @@ import Data.ByteString.Lazy as BL hiding (putStrLn) import qualified Data.Text as T import Data.String import Data.YAML +import Data.Version (showVersion) import Database.PostgreSQL.Simple @@ -39,6 +40,8 @@ import Types import Control import Janitor +import Paths_mateamt (version) + main :: IO () main = do (Options confLoc) <- execParser opts @@ -88,6 +91,8 @@ main = do initState = ReadState { rsConnection = conn , rsTicketStore = store + , rsCurrencySymbol = sym + , rsSoftwareVersion = T.pack (showVersion version) } runSettings settings (app block_registration initState) where @@ -193,7 +198,9 @@ app block_registration initState = avatarGet :<|> avatarInsert :<|> avatarUpdate :<|> - avatarList + avatarList :<|> + + metaGet ) mateApi :: Proxy MateAPI diff --git a/mateamt.cabal b/mateamt.cabal index be253f9..1421fab 100644 --- a/mateamt.cabal +++ b/mateamt.cabal @@ -25,6 +25,7 @@ executable mateamt other-modules: AppTypes , AppTypes.Configuration , Janitor + , Paths_mateamt -- other-extensions: build-depends: base >=4.12.0.0 && < 5 , mateamt @@ -58,6 +59,7 @@ library , Control.Product , Control.Auth , Control.Avatar + , Control.Meta , Model , Model.User , Model.Product @@ -75,6 +77,7 @@ library , Types.Amount , Types.Journal , Types.Avatar + , Types.Meta , Util -- other-extensions: build-depends: base >=4.12.0.0 && < 5 diff --git a/src/API.hs b/src/API.hs index e9de946..01b5a88 100644 --- a/src/API.hs +++ b/src/API.hs @@ -17,7 +17,7 @@ import Data.Proxy import Types -type MateAPI = +type MateAPI = "v1" :> ( "auth" :> "get" :> ReqBody '[JSON] TicketRequest :> Post '[JSON] AuthInfo :<|> "auth" :> ReqBody '[JSON] AuthRequest :> Post '[JSON] AuthResult :<|> "auth" :> AuthProtect "header-auth" :> Delete '[JSON] () @@ -68,6 +68,9 @@ type MateAPI = :> ReqBody '[JSON] AvatarData :> Patch '[JSON] () :<|> "avatar" :> "list" :> Get '[JSON] [Avatar] + :<|> "meta" :> Get '[JSON] MetaInformation + ) + authGetLink :: Link authSendLink :: Link @@ -101,6 +104,8 @@ avaterInsertLink :: Link avatarUpdateLink :: Int -> Link avatarListLink :: Link +metaGetLink :: Link + ( authGetLink :<|> authSendLink :<|> authLogoutLink :<|> @@ -131,5 +136,7 @@ avatarListLink :: Link avatarGetLink :<|> avaterInsertLink :<|> avatarUpdateLink :<|> - avatarListLink + avatarListLink :<|> + + metaGetLink ) = allLinks (Proxy :: Proxy MateAPI) diff --git a/src/Control.hs b/src/Control.hs index ac7dba6..f6f46ff 100644 --- a/src/Control.hs +++ b/src/Control.hs @@ -8,3 +8,4 @@ import Control.User as C import Control.Auth as C import Control.Product as C import Control.Avatar as C +import Control.Meta as C diff --git a/src/Control/Meta.hs b/src/Control/Meta.hs new file mode 100644 index 0000000..5ed1eb3 --- /dev/null +++ b/src/Control/Meta.hs @@ -0,0 +1,20 @@ +{-# LANGUAGE OverloadedStrings #-} +module Control.Meta where + +import Servant + +import Control.Monad.Reader (ask) + +-- internal imports + +import Types +import Model + +metaGet :: MateHandler MetaInformation +metaGet = do + (ReadState _ _ symbol version) <- ask + return (MetaInformation + { metaInfoVersion = version + , metaInfoCurrency = symbol + } + ) diff --git a/src/Model/Product.hs b/src/Model/Product.hs index efd28aa..c12d294 100644 --- a/src/Model/Product.hs +++ b/src/Model/Product.hs @@ -331,7 +331,7 @@ manualProductAmountRefill aups conn = [ ( C.constant pid , C.constant now - , C.constant (oldamount + amountSingles + perCrate * amountCrates) + , C.constant (oldamount + (amountSingles + (perCrate * amountCrates))) , C.constant oldprice , C.constant False ) diff --git a/src/Types.hs b/src/Types.hs index 1c43023..1cd36e5 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -11,3 +11,4 @@ import Types.Purchase as T import Types.Amount as T import Types.Journal as T import Types.Avatar as T +import Types.Meta as T diff --git a/src/Types/Meta.hs b/src/Types/Meta.hs new file mode 100644 index 0000000..f6c2c5e --- /dev/null +++ b/src/Types/Meta.hs @@ -0,0 +1,19 @@ +{-# LANGUAGE DeriveGeneric #-} +module Types.Meta where + +import qualified Data.Text as T + +import Data.Aeson + +import GHC.Generics + +data MetaInformation = MetaInformation + { metaInfoVersion :: T.Text + , metaInfoCurrency :: T.Text + } + deriving (Show, Generic) + +instance ToJSON MetaInformation where + toEncoding = genericToEncoding defaultOptions + +instance FromJSON MetaInformation diff --git a/src/Types/Reader.hs b/src/Types/Reader.hs index 6cad660..30ca565 100644 --- a/src/Types/Reader.hs +++ b/src/Types/Reader.hs @@ -1,5 +1,7 @@ module Types.Reader where +import qualified Data.Text as T + import Servant (Handler) import Control.Monad.Reader (ReaderT) @@ -11,8 +13,10 @@ import Database.PostgreSQL.Simple (Connection) import Types.Auth (TicketStore) data ReadState = ReadState - { rsConnection :: Connection - , rsTicketStore :: TicketStore + { rsConnection :: Connection + , rsTicketStore :: TicketStore + , rsCurrencySymbol :: T.Text + , rsSoftwareVersion :: T.Text } type MateHandler = ReaderT ReadState Handler