From 5588a6b4b73586e5462f61efc5d6b59664abd4b3 Mon Sep 17 00:00:00 2001 From: Petter Rasmussen Date: Sat, 27 Feb 2021 12:11:56 +0100 Subject: [PATCH] Keep created date when updating snippet --- Glot/Snippet.hs | 8 ++++---- Handler/Api/Snippets.hs | 4 ++-- Handler/Compose.hs | 2 +- Handler/Snippet.hs | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Glot/Snippet.hs b/Glot/Snippet.hs index a9fa59a..49fdd3e 100644 --- a/Glot/Snippet.hs +++ b/Glot/Snippet.hs @@ -30,16 +30,16 @@ data SnippetPayload = SnippetPayload instance Aeson.FromJSON SnippetPayload -toCodeSnippet :: Text -> UTCTime -> Maybe UserId -> SnippetPayload -> CodeSnippet -toCodeSnippet slug time maybeUserId SnippetPayload{..} = +toCodeSnippet :: Text -> UTCTime -> UTCTime -> Maybe UserId -> SnippetPayload -> CodeSnippet +toCodeSnippet slug createdAt modifiedAt maybeUserId SnippetPayload{..} = CodeSnippet { codeSnippetSlug = slug , codeSnippetLanguage = pack (show language) , codeSnippetTitle = titleToText title , codeSnippetPublic = public , codeSnippetUserId = maybeUserId - , codeSnippetCreated = time - , codeSnippetModified = time + , codeSnippetCreated = createdAt + , codeSnippetModified = modifiedAt } diff --git a/Handler/Api/Snippets.hs b/Handler/Api/Snippets.hs index 3380d2b..84dd749 100644 --- a/Handler/Api/Snippets.hs +++ b/Handler/Api/Snippets.hs @@ -119,7 +119,7 @@ postApiSnippetsR = do Right payload -> do let snippetSlug = Snippet.newSlug now - let snippet = Snippet.toCodeSnippet snippetSlug now maybeUserId payload + let snippet = Snippet.toCodeSnippet snippetSlug now now maybeUserId payload runDB $ do snippetId <- insert snippet insertMany_ (map (Snippet.toCodeFile snippetId) (NonEmpty.toList $ Snippet.files payload)) @@ -139,10 +139,10 @@ putApiSnippetR snippetSlug = do sendResponseStatus status400 $ object ["message" .= ("Invalid request body: " <> err)] Right payload -> do - let snippet = Snippet.toCodeSnippet snippetSlug now maybeUserId payload runDB $ do Entity snippetId oldSnippet <- getBy404 (UniqueCodeSnippetSlug snippetSlug) lift $ SnippetHandler.ensureSnippetOwner maybeUserId oldSnippet + let snippet = Snippet.toCodeSnippet snippetSlug (codeSnippetCreated oldSnippet) now maybeUserId payload replace snippetId snippet deleteWhere [ CodeFileCodeSnippetId ==. snippetId ] insertMany_ (map (Snippet.toCodeFile snippetId) (NonEmpty.toList $ Snippet.files payload)) diff --git a/Handler/Compose.hs b/Handler/Compose.hs index bafebe1..af4d278 100644 --- a/Handler/Compose.hs +++ b/Handler/Compose.hs @@ -71,7 +71,7 @@ postComposeR _ = do Right payload -> do let snippetSlug = Glot.Snippet.newSlug now - let snippet = Glot.Snippet.toCodeSnippet snippetSlug now maybeUserId payload + let snippet = Glot.Snippet.toCodeSnippet snippetSlug now now maybeUserId payload runDB $ do snippetId <- insert snippet insertMany_ (map (Glot.Snippet.toCodeFile snippetId) (NonEmpty.toList $ Glot.Snippet.files payload)) diff --git a/Handler/Snippet.hs b/Handler/Snippet.hs index abb213f..f97fd3d 100644 --- a/Handler/Snippet.hs +++ b/Handler/Snippet.hs @@ -61,10 +61,10 @@ putSnippetR snippetSlug = do sendResponseStatus status400 $ object ["message" .= ("Invalid request body: " <> err)] Right payload -> do - let snippet = Glot.Snippet.toCodeSnippet snippetSlug now maybeUserId payload runDB $ do Entity snippetId oldSnippet <- getBy404 (UniqueCodeSnippetSlug snippetSlug) lift $ ensureSnippetOwner maybeUserId oldSnippet + let snippet = Glot.Snippet.toCodeSnippet snippetSlug (codeSnippetCreated oldSnippet) now maybeUserId payload replace snippetId snippet deleteWhere [ CodeFileCodeSnippetId ==. snippetId ] insertMany_ (map (Glot.Snippet.toCodeFile snippetId) (NonEmpty.toList $ Glot.Snippet.files payload))