From 738989c375fdc2cc4a34b2264b8a10318bdd29b8 Mon Sep 17 00:00:00 2001 From: Ruslan Talpa Date: Thu, 29 Oct 2015 16:22:31 +0200 Subject: [PATCH] Cleanup 2 --- src/PostgREST/App.hs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 83a1c9d2c..b48b7db1b 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -55,7 +55,6 @@ import PostgREST.Types import PostgREST.Auth (tokenJWT) import Prelude ---import Debug.Trace app :: DbStructure -> AppConfig -> BL.ByteString -> Request -> H.Tx P.Postgres s Response app dbstructure conf reqBody req = @@ -70,7 +69,7 @@ app dbstructure conf reqBody req = Right (selectQuery, _, _) -> do let q = B.Stmt (createStatement selectQuery Nothing True range [] (not $ hasPrefer "count=none") isCsv) V.empty True row <- H.maybeEx q - let (tableTotal, queryTotal, _ , body) = fromMaybe (Just (0::Int), 0::Int, Just "" :: Maybe BL.ByteString, Just "" :: Maybe BL.ByteString) row + let (tableTotal, queryTotal, _ , body) = extractQueryResult row to = frm+queryTotal-1 contentRange = contentRangeH frm to tableTotal status = rangeStatus frm to tableTotal @@ -99,7 +98,7 @@ app dbstructure conf reqBody req = let pKeys = map pkName $ filter (filterPk schema table) allPrKeys -- would it be ok to move primary key detection in the query itself? q = B.Stmt (createStatement selectQuery (Just (mutateQuery, isSingle)) echoRequested Nothing pKeys False isCsv) V.empty True row <- H.maybeEx q - let (_, _, location, body) = fromMaybe (Just (0::Int), 0::Int, Just "" :: Maybe BL.ByteString, Just "" :: Maybe BL.ByteString) row + let (_, _, location, body) = extractQueryResult row return $ responseLBS status201 [ contentTypeH, @@ -141,7 +140,7 @@ app dbstructure conf reqBody req = Right (selectQuery, mutateQuery, _) -> do let q = B.Stmt (createStatement selectQuery (Just (mutateQuery, False)) echoRequested Nothing [] False isCsv) V.empty True row <- H.maybeEx q - let (_, queryTotal, _, body) = fromMaybe (Just (0::Int), 0::Int, Just "" :: Maybe BL.ByteString, Just "" :: Maybe BL.ByteString) row + let (_, queryTotal, _, body) = extractQueryResult row r = contentRangeH 0 (queryTotal-1) (Just queryTotal) s = case () of _ | queryTotal == 0 -> status404 | echoRequested -> status200 @@ -467,3 +466,7 @@ createStatement selectQuery (Just (changeQuery, isSingle)) echoRequested _ pKeys else "null" ] selectQuery Nothing + +extractQueryResult :: Maybe (Maybe Int, Int, Maybe BL.ByteString, Maybe BL.ByteString) + -> (Maybe Int, Int, Maybe BL.ByteString, Maybe BL.ByteString) +extractQueryResult = fromMaybe (Just 0, 0, Just "", Just "")