From a7a4dd01127506dba991cc5f3f39c4a370fff699 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Wed, 5 Jun 2024 10:41:02 +0200 Subject: add edit page --- frontend/app/Api.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'frontend/app/Api.hs') 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 -- cgit v1.2.3