All tests passing except hlint warning about duplicated code blocks

This commit is contained in:
Joe Nelson
2014-09-10 21:20:56 -07:00
parent dd9d9cb283
commit d3ae4aca2b
2 changed files with 25 additions and 7 deletions
+20 -5
View File
@@ -11,7 +11,7 @@ import Control.Arrow ((***))
import Control.Applicative import Control.Applicative
import Options.Applicative hiding (columns) import Options.Applicative hiding (columns)
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe, isJust)
import Text.Read (readMaybe) import Text.Read (readMaybe)
import Text.Regex.TDFA ((=~)) import Text.Regex.TDFA ((=~))
import Data.Map (intersection, fromList, toList) import Data.Map (intersection, fromList, toList)
@@ -111,16 +111,30 @@ app conn req respond = do
if S.fromList keys /= S.fromList specifiedKeys if S.fromList keys /= S.fromList specifiedKeys
then return $ responseLBS status405 [] then return $ responseLBS status405 []
"You must speficy all and only primary keys as params" "You must speficy all and only primary keys as params"
else
if isJust cRange
then return $ responseLBS status400 []
"Content-Range is not allowed in PUT request"
else do else do
cols <- columns ver (unpack table) conn cols <- columns ver (unpack table) conn
let colNames = S.fromList $ map (pack . colName) cols let colNames = S.fromList $ map (pack . colName) cols
let specifiedCols = S.fromList $ map fst $ getRow row let specifiedCols = S.fromList $ map fst $ getRow row
if colNames == specifiedCols then do if colNames == specifiedCols then do
_ <- upsert ver table row qq conn allvals <- upsert ver table row qq conn
return $ responseLBS status201 [] "" let keyvals = if null keys
else return $ responseLBS status400 [] then allvals
"Missing column(s) in PUT request" else allvals `intersection` fromList (zip keys $ repeat SqlNull)
let params = urlEncodeVars $ map (\t -> (fst t, "eq." <> convert (snd t) :: String)) $ toList keyvals
return $ responseLBS status201
[ jsonContentType
, (hLocation, "/" <> encodeUtf8 table <> "?" <> BS.pack params)
] ""
else return $ if S.null colNames then responseLBS status404 [] ""
else responseLBS status400 []
"You must specify all columns in PUT request"
) )
(_, _) -> (_, _) ->
return $ responseLBS status404 [] "" return $ responseLBS status404 [] ""
@@ -132,6 +146,7 @@ app conn req respond = do
qq = queryString req qq = queryString req
ver = fromMaybe 1 $ requestedVersion (requestHeaders req) ver = fromMaybe 1 $ requestedVersion (requestHeaders req)
range = requestedRange (requestHeaders req) range = requestedRange (requestHeaders req)
cRange = requestedContentRange (requestHeaders req)
respondWithRangedResult :: RangedResult -> Response respondWithRangedResult :: RangedResult -> Response
respondWithRangedResult rr = respondWithRangedResult rr =
+3
View File
@@ -39,6 +39,9 @@ parseRange range = do
requestedRange :: RequestHeaders -> Maybe NonnegRange requestedRange :: RequestHeaders -> Maybe NonnegRange
requestedRange hdrs = parseRange =<< BS.unpack <$> lookup hRange hdrs requestedRange hdrs = parseRange =<< BS.unpack <$> lookup hRange hdrs
requestedContentRange :: RequestHeaders -> Maybe NonnegRange
requestedContentRange hdrs = parseRange =<< BS.unpack <$> lookup "Content-Range" hdrs
limit :: NonnegRange -> Maybe Int limit :: NonnegRange -> Maybe Int
limit range = limit range =
case [rangeLower range, rangeUpper range] case [rangeLower range, rangeUpper range]