Skip to content

Commit

Permalink
refactor failNotSingular
Browse files Browse the repository at this point in the history
  • Loading branch information
monacoremo committed Apr 26, 2021
1 parent 87bcc01 commit 31fb447
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
30 changes: 15 additions & 15 deletions src/PostgREST/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ read req bField conf@AppConfig{..} dbStructure apiRequest@ApiRequest{..} = do
configDbPreparedStatements

total <- readTotal conf apiRequest tableTotal countQuery
fullResult <- liftEither $ (result, total,,) <$> gucHeaders <*> gucStatus
failNotSingular iAcceptContentType queryTotal fullResult
failNotSingular iAcceptContentType queryTotal
liftEither $ (result, total,,) <$> gucHeaders <*> gucStatus
where
countQuery = QueryBuilder.readRequestToCountQuery req

Expand All @@ -102,14 +102,16 @@ readTotal AppConfig{..} ApiRequest{..} tableTotal countQuery =
create :: QualifiedIdentifier -> AppConfig -> DbStructure -> ApiRequest -> ReadRequest -> MutateRequest -> DbHandler WriteQueryResult
create identifier@QualifiedIdentifier{..} conf dbStructure apiRequest@ApiRequest{..} readReq mutReq = do
result <- writeQuery identifier True pkCols conf dbStructure apiRequest readReq mutReq
failNotSingular iAcceptContentType (resQueryTotal result) result
failNotSingular iAcceptContentType (resQueryTotal result)
return result
where
pkCols = tablePKCols dbStructure qiSchema qiName

update :: QualifiedIdentifier -> AppConfig -> DbStructure -> ApiRequest -> ReadRequest -> MutateRequest -> DbHandler WriteQueryResult
update identifier conf dbStructure apiRequest@ApiRequest{..} readReq mutReq = do
result <- writeQuery identifier False mempty conf dbStructure apiRequest readReq mutReq
failNotSingular iAcceptContentType (resQueryTotal result) result
failNotSingular iAcceptContentType (resQueryTotal result)
return result

singleUpsert :: QualifiedIdentifier -> AppConfig -> DbStructure -> ApiRequest -> ReadRequest -> MutateRequest -> DbHandler WriteQueryResult
singleUpsert identifier conf dbStructure apiRequest@ApiRequest{..} readReq mutReq= do
Expand All @@ -133,7 +135,8 @@ singleUpsert identifier conf dbStructure apiRequest@ApiRequest{..} readReq mutRe
delete :: AppConfig -> DbStructure -> QualifiedIdentifier -> ApiRequest -> ReadRequest -> MutateRequest -> DbHandler WriteQueryResult
delete conf dbStructure identifier apiRequest@ApiRequest{..} readReq mutReq = do
result <- writeQuery identifier False mempty conf dbStructure apiRequest readReq mutReq
failNotSingular iAcceptContentType (resQueryTotal result) result
failNotSingular iAcceptContentType (resQueryTotal result)
return result

invoke :: ProcDescription -> ReadRequest -> Maybe FieldName -> AppConfig -> DbStructure -> ApiRequest -> DbHandler (Statements.ProcResults, [GucHeader], Maybe HTTP.Status)
invoke proc req bField AppConfig{..} dbStructure ApiRequest{..} = do
Expand All @@ -160,8 +163,8 @@ invoke proc req bField AppConfig{..} dbStructure ApiRequest{..} = do
(pgVersion dbStructure)
configDbPreparedStatements

fullResult <- liftEither $ (result,,) <$> gucHeaders <*> gucStatus
failNotSingular iAcceptContentType queryTotal fullResult
failNotSingular iAcceptContentType queryTotal
liftEither $ (result,,) <$> gucHeaders <*> gucStatus
where
returnsSingle (ApiRequest.TargetProc target _) = Proc.procReturnsSingle target
returnsSingle _ = False
Expand Down Expand Up @@ -221,14 +224,11 @@ writeQuery QualifiedIdentifier{..} isInsert pkCols AppConfig{..} dbStructure Api

-- | Fail a response if a single JSON object was requested and not exactly one
-- was found.
failNotSingular :: ContentType -> Int64 -> a -> DbHandler a
failNotSingular contentType queryTotal response =
if contentType == CTSingularJSON && queryTotal /= 1 then
do
lift SQL.condemn
throwError $ Error.singularityError queryTotal
else
return response
failNotSingular :: ContentType -> Int64 -> DbHandler ()
failNotSingular contentType queryTotal =
when (contentType == CTSingularJSON && queryTotal /= 1) $ do
lift SQL.condemn
throwError $ Error.singularityError queryTotal

shouldCount :: Maybe PreferCount -> Bool
shouldCount preferCount =
Expand Down
2 changes: 1 addition & 1 deletion src/PostgREST/Response.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ import PostgREST.DbStructure.Table (Table (..))
import PostgREST.GucHeader (GucHeader,
addHeadersIfNotIncluded,
unwrapGucHeader)
import PostgREST.Query (WriteQueryResult (..))
import PostgREST.Request.ApiRequest (ApiRequest (..),
InvokeMethod (..))
import PostgREST.Request.Preferences (PreferCount (..),
PreferRepresentation (..))
import PostgREST.Query (WriteQueryResult (..))

import qualified PostgREST.ContentType as ContentType

Expand Down

0 comments on commit 31fb447

Please sign in to comment.