mirror of
https://github.com/Start9Labs/registry.git
synced 2026-03-30 11:51:57 +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 :: MonadIO m => AppVersion -> Key SApp -> ReaderT SqlBackend m (Maybe (Entity Version))
|
||||||
fetchAppVersion appVersion appId = selectFirst [VersionNumber ==. appVersion, VersionAppId ==. appId] []
|
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
|
createApp appId StoreApp{..} = do
|
||||||
time <- liftIO getCurrentTime
|
time <- liftIO getCurrentTime
|
||||||
insert $ SApp
|
insertUnique $ SApp
|
||||||
time
|
time
|
||||||
Nothing
|
Nothing
|
||||||
storeAppTitle
|
storeAppTitle
|
||||||
@@ -28,10 +28,10 @@ createApp appId StoreApp{..} = do
|
|||||||
storeAppDescLong
|
storeAppDescLong
|
||||||
storeAppIconType
|
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
|
createAppVersion sId VersionInfo{..} = do
|
||||||
time <- liftIO getCurrentTime
|
time <- liftIO getCurrentTime
|
||||||
insert $ Version
|
insertUnique $ Version
|
||||||
time
|
time
|
||||||
Nothing
|
Nothing
|
||||||
sId
|
sId
|
||||||
|
|||||||
@@ -86,19 +86,24 @@ getApp rootDir ext@(Extension appId) = do
|
|||||||
sa <- runDB $ fetchApp appId'
|
sa <- runDB $ fetchApp appId'
|
||||||
(appKey, versionKey) <- case sa of
|
(appKey, versionKey) <- case sa of
|
||||||
Nothing -> do
|
Nothing -> do
|
||||||
ak <- runDB $ createApp appId' storeApp
|
appKey' <- runDB $ createApp appId' storeApp >>= errOnNothing status500 "duplicate app created"
|
||||||
vk <- runDB $ createAppVersion ak versionInfo
|
versionKey' <- runDB $ createAppVersion appKey' versionInfo >>= errOnNothing status500 "duplicate app version created"
|
||||||
pure (ak, vk)
|
pure (appKey', versionKey')
|
||||||
Just a -> do
|
Just a -> do
|
||||||
let appKey' = entityKey a
|
let appKey' = entityKey a
|
||||||
maybeVer <- runDB $ fetchAppVersion appVersion appKey'
|
existingVersion <- runDB $ fetchAppVersion appVersion appKey'
|
||||||
case maybeVer of
|
case existingVersion of
|
||||||
Nothing -> do
|
Nothing -> do
|
||||||
av <- runDB $ createAppVersion appKey' versionInfo
|
appVersion' <- runDB $ createAppVersion appKey' versionInfo >>= errOnNothing status500 "duplicate app version created"
|
||||||
pure (appKey', av)
|
pure (appKey', appVersion')
|
||||||
Just v -> pure (appKey', entityKey v)
|
Just v -> pure (appKey', entityKey v)
|
||||||
runDB $ createMetric appKey versionKey
|
runDB $ createMetric appKey versionKey
|
||||||
sz <- liftIO $ fileSize <$> getFileStatus filePath
|
sz <- liftIO $ fileSize <$> getFileStatus filePath
|
||||||
addHeader "Content-Length" (show sz)
|
addHeader "Content-Length" (show sz)
|
||||||
respondSource typePlain $ CB.sourceFile filePath .| awaitForever sendChunkBS
|
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