Skip to content

Commit

Permalink
Keep created date when updating snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Feb 27, 2021
1 parent 3ec5b7f commit 5588a6b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Glot/Snippet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down
4 changes: 2 additions & 2 deletions Handler/Api/Snippets.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion Handler/Compose.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion Handler/Snippet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 5588a6b

Please sign in to comment.