From 6aeb7dfdf942bc5ff5eb7f9484f4328cfc1db9b1 Mon Sep 17 00:00:00 2001 From: Lucy Cifferello Date: Mon, 22 Jun 2020 17:27:31 -0600 Subject: [PATCH] handle inserting unique apps and versions --- src/Database/Queries.hs | 8 ++++---- src/Handler/Apps.hs | 21 +++++++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Database/Queries.hs b/src/Database/Queries.hs index f687662..d15d664 100644 --- a/src/Database/Queries.hs +++ b/src/Database/Queries.hs @@ -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 diff --git a/src/Handler/Apps.hs b/src/Handler/Apps.hs index ebe028f..2a111e4 100644 --- a/src/Handler/Apps.hs +++ b/src/Handler/Apps.hs @@ -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 \ No newline at end of file + 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 \ No newline at end of file