add edit page
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user