diff options
Diffstat (limited to 'frontend/app/Api.hs')
-rw-r--r-- | frontend/app/Api.hs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/frontend/app/Api.hs b/frontend/app/Api.hs index 2a26c66..c16e269 100644 --- a/frontend/app/Api.hs +++ b/frontend/app/Api.hs @@ -3,6 +3,8 @@ module Api ( fetchSchema, fetchPosts, + fetchPost, + updatePost, ) where @@ -18,8 +20,10 @@ import Miso.String qualified as J #endif import Data.Aeson qualified as A import Data.ByteString.Lazy.Char8 qualified as LB +import Data.ByteString.Lazy.UTF8 qualified as LB import Data.Function import Miso +import Safe import Schema fetchSchema :: JSM (Either String Schema) @@ -35,6 +39,24 @@ fetchPosts = & setRequestBodyLBS "SELECT posts FROM posts" ) +fetchPost :: String -> JSM (Either String (Maybe A.Value)) +fetchPost fileName = + fmap headMay . A.eitherDecode + <$> fetch + ( fromString "http://localhost:8081" + & setRequestMethod "POST" + & setRequestBodyLBS ("SELECT posts FROM posts WHERE posts.$fileName == \"" <> LB.fromString fileName <> "\"") + ) + +updatePost :: String -> A.Value -> JSM (Either String ()) +updatePost fileName value = + A.eitherDecode + <$> fetch + ( fromString "http://localhost:8081" + & setRequestMethod "POST" + & setRequestBodyLBS ("UPDATE posts SET " <> A.encode value <> " WHERE posts.$fileName == \"" <> LB.fromString fileName <> "\"") + ) + fetch :: Request -> JSM LB.ByteString fetch req = LB.fromStrict . getResponseBody <$> httpBS req |