aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/app/Api.hs
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-06-05 10:41:02 +0200
committerLibravatar Alexander Foremny <aforemny@posteo.de>2024-06-05 10:41:02 +0200
commita7a4dd01127506dba991cc5f3f39c4a370fff699 (patch)
tree086c6b306a1bbfb9cd13a727bc2284894991f24d /frontend/app/Api.hs
parentd5f3f2333a4a167054c0a8556dfd8cd87f955595 (diff)
add edit page
Diffstat (limited to 'frontend/app/Api.hs')
-rw-r--r--frontend/app/Api.hs22
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