diff --git a/src/API.hs b/src/API.hs index 8dde479..0de7cb4 100644 --- a/src/API.hs +++ b/src/API.hs @@ -41,7 +41,8 @@ type MateAPI = :> ReqBody '[JSON] [AmountRefill] :> Patch '[JSON] () :<|> "product" :> AuthProtect "header-auth" :> ReqBody '[JSON] [AmountUpdate] :> Put '[JSON] () - :<|> "product" :> "list" :> Get '[JSON] [ProductOverview] + :<|> "product" :> "list" :> QueryParam "refine" ProductRefine + :> Get '[JSON] [ProductOverview] :<|> "buy" :> AuthProtect "header-auth" :> ReqBody '[JSON] [PurchaseDetail] :> Post '[JSON] PurchaseResult diff --git a/src/Control/Product.hs b/src/Control/Product.hs index c6e43ba..cb718dc 100644 --- a/src/Control/Product.hs +++ b/src/Control/Product.hs @@ -56,7 +56,7 @@ productStockUpdate Nothing _ = { errBody = "No Authentication present." } -productList :: MateHandler [ProductOverview] -productList = do +productList :: Maybe ProductRefine :> MateHandler [ProductOverview] +productList mrefine = do conn <- rsConnection <$> ask - productOverviewSelect conn + productOverviewSelect (fromMaybe AvailableProducts mrefine) conn diff --git a/src/Types/Refine.hs b/src/Types/Refine.hs index 3c68da2..d636f2e 100644 --- a/src/Types/Refine.hs +++ b/src/Types/Refine.hs @@ -7,12 +7,35 @@ import GHC.Generics import Web.HttpApiData -data Refine = All | Old +data UserRefine = AllUsers | OldUsers deriving (Generic, Show, Enum) -instance FromHttpApiData Refine where +instance FromHttpApiData UserRefine where parseQueryParam t = case t of - "all" -> Right All - "old" -> Right Old + "all" -> Right AllUsers + "old" -> Right OldUsers x -> Left ("Error: Unknown refine " <> x) + +instance ToHttpApiData UserRefine where + toUrlPiece x = case x of + AllUsers -> "all" + OldUsers -> "old" + + +data ProductRefine = AllProducts | AvailableProducts | DepletedProducts + deriving (Generic, Show, Enum) + +instance FromHttpApiData ProductRefine where + parseQueryParam t = + case t of + "all" -> Right AllProducts + "available" -> Right AvailableProducts + "depleted" -> Right DepletedProducts + x -> Left ("Error: Unknown refine " <> x) + +instance ToHttpApiData ProductRefine where + toUrlPiece x = case x of + AllProducts -> "all" + AvailableProducts -> "available" + DepletedProducts -> "depleted"