mirror of
https://github.com/Start9Labs/registry.git
synced 2026-03-26 02:11:53 +00:00
handle inserting unique apps and versions
This commit is contained in:
@@ -16,10 +16,10 @@ fetchApp appId = selectFirst [SAppAppId ==. appId] []
|
||||
fetchAppVersion :: MonadIO m => AppVersion -> Key SApp -> ReaderT SqlBackend m (Maybe (Entity Version))
|
||||
fetchAppVersion appVersion appId = selectFirst [VersionNumber ==. appVersion, VersionAppId ==. appId] []
|
||||
|
||||
createApp :: MonadIO m => AppIdentifier -> StoreApp -> ReaderT SqlBackend m (Key SApp)
|
||||
createApp :: MonadIO m => AppIdentifier -> StoreApp -> ReaderT SqlBackend m (Maybe (Key SApp))
|
||||
createApp appId StoreApp{..} = do
|
||||
time <- liftIO getCurrentTime
|
||||
insert $ SApp
|
||||
insertUnique $ SApp
|
||||
time
|
||||
Nothing
|
||||
storeAppTitle
|
||||
@@ -28,10 +28,10 @@ createApp appId StoreApp{..} = do
|
||||
storeAppDescLong
|
||||
storeAppIconType
|
||||
|
||||
createAppVersion :: MonadIO m => Key SApp -> VersionInfo -> ReaderT SqlBackend m (Key Version)
|
||||
createAppVersion :: MonadIO m => Key SApp -> VersionInfo -> ReaderT SqlBackend m (Maybe (Key Version))
|
||||
createAppVersion sId VersionInfo{..} = do
|
||||
time <- liftIO getCurrentTime
|
||||
insert $ Version
|
||||
insertUnique $ Version
|
||||
time
|
||||
Nothing
|
||||
sId
|
||||
|
||||
@@ -86,19 +86,24 @@ getApp rootDir ext@(Extension appId) = do
|
||||
sa <- runDB $ fetchApp appId'
|
||||
(appKey, versionKey) <- case sa of
|
||||
Nothing -> do
|
||||
ak <- runDB $ createApp appId' storeApp
|
||||
vk <- runDB $ createAppVersion ak versionInfo
|
||||
pure (ak, vk)
|
||||
appKey' <- runDB $ createApp appId' storeApp >>= errOnNothing status500 "duplicate app created"
|
||||
versionKey' <- runDB $ createAppVersion appKey' versionInfo >>= errOnNothing status500 "duplicate app version created"
|
||||
pure (appKey', versionKey')
|
||||
Just a -> do
|
||||
let appKey' = entityKey a
|
||||
maybeVer <- runDB $ fetchAppVersion appVersion appKey'
|
||||
case maybeVer of
|
||||
existingVersion <- runDB $ fetchAppVersion appVersion appKey'
|
||||
case existingVersion of
|
||||
Nothing -> do
|
||||
av <- runDB $ createAppVersion appKey' versionInfo
|
||||
pure (appKey', av)
|
||||
appVersion' <- runDB $ createAppVersion appKey' versionInfo >>= errOnNothing status500 "duplicate app version created"
|
||||
pure (appKey', appVersion')
|
||||
Just v -> pure (appKey', entityKey v)
|
||||
runDB $ createMetric appKey versionKey
|
||||
sz <- liftIO $ fileSize <$> getFileStatus filePath
|
||||
addHeader "Content-Length" (show sz)
|
||||
respondSource typePlain $ CB.sourceFile filePath .| awaitForever sendChunkBS
|
||||
else notFound
|
||||
else notFound
|
||||
|
||||
errOnNothing :: MonadHandler m => Status -> Text -> Maybe a -> m a
|
||||
errOnNothing status res entity = case entity of
|
||||
Nothing -> sendResponseStatus status res
|
||||
Just a -> pure a
|
||||
Reference in New Issue
Block a user