diff --git a/src/Database/Queries.hs b/src/Database/Queries.hs index b408f0f..9707e37 100644 --- a/src/Database/Queries.hs +++ b/src/Database/Queries.hs @@ -19,7 +19,7 @@ import Model ( Metric (Metric), PkgDependency (..), PkgRecord (PkgRecord), - VersionRecord (VersionRecord), VersionPlatform, EntityField (VersionPlatformPkgId, VersionPlatformVersionNumber, VersionPlatformArch, VersionPlatformId, VersionRecordId), + VersionRecord (VersionRecord), VersionPlatform, EntityField (VersionPlatformPkgId, VersionPlatformVersionNumber, VersionPlatformArch), ) import Orphans.Emver () import Startlude ( diff --git a/src/Handler/Package/V0/Latest.hs b/src/Handler/Package/V0/Latest.hs index cbbf836..102def0 100644 --- a/src/Handler/Package/V0/Latest.hs +++ b/src/Handler/Package/V0/Latest.hs @@ -19,7 +19,6 @@ import Network.HTTP.Types (status400) import Startlude (Bool (True), Down (Down), Either (..), Generic, Maybe (..), NonEmpty, Show, const, encodeUtf8, filter, flip, nonEmpty, pure, ($), (.), (<$>), (<&>)) import Yesod (ToContent (..), ToTypedContent (..), YesodPersist (runDB), YesodRequest (reqGetParams), getRequest, sendResponseStatus) import Handler.Util (getArchQuery) -import Control.Monad.Reader.Has ((>>=)) newtype VersionLatestRes = VersionLatestRes (HashMap PkgId (Maybe Version)) @@ -38,33 +37,32 @@ getVersionLatestR = do getOsVersionQuery <&> \case Nothing -> const True Just v -> flip satisfies v - getArchQuery >>= \case - Nothing -> sendResponseStatus status400 (InvalidParamsE "Param is required" "arch") - Just osArch -> do - case lookup "ids" getParameters of - Nothing -> sendResponseStatus status400 (InvalidParamsE "get:ids" "") - Just packages -> case eitherDecode $ LBS.fromStrict $ encodeUtf8 packages of - Left _ -> sendResponseStatus status400 (InvalidParamsE "get:ids" packages) - Right p -> do - let packageList = (,Nothing) <$> p - let source = getPkgDataSource p osArch - filteredPackages <- - runDB $ - runConduit $ - source - -- group conduit pipeline by pkg id - .| collateVersions - -- filter out versions of apps that are incompatible with the OS predicate - .| mapC (second (filter (osPredicate' . versionRecordOsVersion))) - -- prune empty version sets - .| concatMapC (\(pkgId, vs) -> (pkgId,) <$> nonEmpty vs) - -- grab the latest matching version if it exists - .| mapC (\(a, b) -> (a, (Just $ selectLatestVersion b))) - .| sinkList - -- if the requested package does not have available versions, return it as a key with a null value - pure $ - VersionLatestRes $ - HM.union (HM.fromList $ filteredPackages) (HM.fromList packageList) + osArch <- getArchQuery + do + case lookup "ids" getParameters of + Nothing -> sendResponseStatus status400 (InvalidParamsE "get:ids" "") + Just packages -> case eitherDecode $ LBS.fromStrict $ encodeUtf8 packages of + Left _ -> sendResponseStatus status400 (InvalidParamsE "get:ids" packages) + Right p -> do + let packageList = (,Nothing) <$> p + let source = getPkgDataSource p osArch + filteredPackages <- + runDB $ + runConduit $ + source + -- group conduit pipeline by pkg id + .| collateVersions + -- filter out versions of apps that are incompatible with the OS predicate + .| mapC (second (filter (osPredicate' . versionRecordOsVersion))) + -- prune empty version sets + .| concatMapC (\(pkgId, vs) -> (pkgId,) <$> nonEmpty vs) + -- grab the latest matching version if it exists + .| mapC (\(a, b) -> (a, (Just $ selectLatestVersion b))) + .| sinkList + -- if the requested package does not have available versions, return it as a key with a null value + pure $ + VersionLatestRes $ + HM.union (HM.fromList $ filteredPackages) (HM.fromList packageList) where selectLatestVersion :: NonEmpty VersionRecord -> Version selectLatestVersion vs = NE.head $ (versionRecordNumber <$>) $ NE.sortOn (Down . versionRecordNumber) $ vs diff --git a/src/Handler/Package/V1/Index.hs b/src/Handler/Package/V1/Index.hs index ba19fbd..ce39c1c 100644 --- a/src/Handler/Package/V1/Index.hs +++ b/src/Handler/Package/V1/Index.hs @@ -87,9 +87,6 @@ import Yesod ( YesodPersist (runDB), lookupGetParam, ) -import Yesod.Core.Handler (sendResponseStatus) -import Network.HTTP.Types (status400) -import Lib.Error (S9Error(InvalidParamsE)) data PackageReq = PackageReq { packageReqId :: !PkgId