module Handler.Snippet where

import Import
import Widget
import Model.Snippet.Api (getSnippet, updateSnippet)
import Network.Wai (lazyRequestBody)

getSnippetR :: Text -> Handler Html
getSnippetR snippetId = do
    snippet <- liftIO $ getSnippet snippetId Nothing
    mUserId <- maybeAuthId
    mApiUser <- maybeApiUser mUserId
    let lang = toLanguage $ snippetLanguage snippet
    defaultLayout $ do
        $(combineScripts 'StaticR [lib_ace_ace_js])
        setTitle $ "glot.io"
        $(widgetFile "snippet")

putSnippetR :: Text -> Handler Value
putSnippetR snippetId = do
    req <- reqWaiRequest <$> getRequest
    body <- liftIO $ lazyRequestBody req
    mUserId <- maybeAuthId
    mAuthToken <- maybeAuthToken mUserId
    _ <- liftIO $ updateSnippet snippetId body mAuthToken
    return $ object []

maybeAuthToken :: Maybe UserId -> Handler (Maybe Text)
maybeAuthToken Nothing = return Nothing
maybeAuthToken (Just userId) = do
    Entity _ apiUser <- runDB $ getBy404 $ UniqueApiUser userId
    return $ Just $ apiUserToken apiUser

maybeApiUser :: Maybe UserId -> Handler (Maybe ApiUser)
maybeApiUser Nothing = return Nothing
maybeApiUser (Just userId) = do
    Entity _ apiUser <- runDB $ getBy404 $ UniqueApiUser userId
    return $ Just apiUser

isSnippetOwner :: Maybe ApiUser -> Snippet -> Bool
isSnippetOwner Nothing _ = False
isSnippetOwner (Just apiUser) snippet =
    apiUserSnippetsId apiUser == snippetOwner snippet

module Handler.Snippet where

import Import
import Widget
import Model.Snippet.Api (getSnippet, updateSnippet)
import Network.Wai (lazyRequestBody)

getSnippetR :: Text -> Handler Html
getSnippetR snippetId = do
    mUserId <- maybeAuthId
    mApiUser <- maybeApiUser mUserId
    snippet <- liftIO $ getSnippet snippetId $ apiUserToken <$> mApiUser
    let lang = toLanguage $ snippetLanguage snippet
    defaultLayout $ do
        $(combineScripts 'StaticR [lib_ace_ace_js])
        setTitle $ "glot.io"
        $(widgetFile "snippet")

putSnippetR :: Text -> Handler Value
putSnippetR snippetId = do
    req <- reqWaiRequest <$> getRequest
    body <- liftIO $ lazyRequestBody req
    mUserId <- maybeAuthId
    mAuthToken <- maybeAuthToken mUserId
    _ <- liftIO $ updateSnippet snippetId body mAuthToken
    return $ object []

maybeAuthToken :: Maybe UserId -> Handler (Maybe Text)
maybeAuthToken Nothing = return Nothing
maybeAuthToken (Just userId) = do
    Entity _ apiUser <- runDB $ getBy404 $ UniqueApiUser userId
    return $ Just $ apiUserToken apiUser

maybeApiUser :: Maybe UserId -> Handler (Maybe ApiUser)
maybeApiUser Nothing = return Nothing
maybeApiUser (Just userId) = do
    Entity _ apiUser <- runDB $ getBy404 $ UniqueApiUser userId
    return $ Just apiUser

isSnippetOwner :: Maybe ApiUser -> Snippet -> Bool
isSnippetOwner Nothing _ = False
isSnippetOwner (Just apiUser) snippet =
    apiUserSnippetsId apiUser == snippetOwner snippet

Send token with get requests
Send token with get requests
