diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-10-11 15:01:17 +0200 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-10-11 15:01:17 +0200 |
commit | 9a44128f4c2228311b5402d3b57c056f5f20558e (patch) | |
tree | 969ed3be812abbcc747efc6d388e269fc02adc0a | |
parent | 35bf45a81da20d782a8a654703eca14543e25def (diff) |
Add ACMS.API.REST.Collection
-rw-r--r-- | backend/backend.cabal | 37 | ||||
-rw-r--r-- | backend/lib/ACMS/API/REST/Collection.hs | 56 |
2 files changed, 93 insertions, 0 deletions
diff --git a/backend/backend.cabal b/backend/backend.cabal index b1b1344..4909bdf 100644 --- a/backend/backend.cabal +++ b/backend/backend.cabal @@ -7,6 +7,43 @@ maintainer: aforemny@posteo.de author: Alexander Foremny build-type: Simple +library + exposed-modules: ACMS.API.REST.Collection + hs-source-dirs: lib + default-language: GHC2021 + default-extensions: + BlockArguments LambdaCase OverloadedStrings ViewPatterns + OverloadedRecordDot NoFieldSelectors MultiWayIf + + ghc-options: -Wall -threaded + build-depends: + aeson, + astore, + attoparsec, + autotypes, + base, + bytestring, + common, + containers, + directory, + filepath, + gitlib, + gitlib-libgit2, + hinotify, + hlibgit2, + http-conduit, + http-types, + mtl, + optparse-applicative, + safe, + split, + stm, + tagged, + text, + utf8-string, + wai, + warp + executable backend main-is: Main.hs hs-source-dirs: app diff --git a/backend/lib/ACMS/API/REST/Collection.hs b/backend/lib/ACMS/API/REST/Collection.hs new file mode 100644 index 0000000..bfbd637 --- /dev/null +++ b/backend/lib/ACMS/API/REST/Collection.hs @@ -0,0 +1,56 @@ +{-# LANGUAGE OverloadedStrings #-} +module ACMS.API.REST.Collection where + +import Data.Aeson qualified as A +import Data.Aeson.KeyMap qualified as AM +import Data.ByteString.Lazy.UTF8 qualified as LB +import Data.Function ((&)) +import Data.Text qualified as T +import Network.HTTP.Simple +import Text.Printf (printf) + +type CollectionName = T.Text + +list :: T.Text -> IO [A.Object] +list c = + "http://localhost:8081" + & setRequestMethod "POST" + & setRequestBodyLBS (LB.fromString (printf "SELECT %s FROM %s" c c)) + & httpLBS + >>= A.throwDecode . getResponseBody + +read :: T.Text -> T.Text -> IO [A.Object] +read c i = + "http://localhost:8081" + & setRequestMethod "POST" + & setRequestBodyLBS + (LB.fromString (printf "SELECT %s FROM %s WHERE %s.$fileName == \"%s\"" c c c i)) + & httpLBS + >>= A.throwDecode . getResponseBody + +update :: T.Text -> T.Text -> A.Object -> IO () +update c i o = + "http://localhost:8081" + & setRequestMethod "POST" + & setRequestBodyLBS + (LB.fromString (printf "UPDATE %s SET %s WHERE %s.$fileName == \"%s\"" c (LB.toString (A.encode o)) c i)) + & httpLBS + >>= A.throwDecode . getResponseBody + +create :: T.Text -> T.Text -> A.Object -> IO () +create c i o = + "http://localhost:8081" + & setRequestMethod "POST" + & setRequestBodyLBS + (LB.fromString (printf "INSERT %s INTO %s" (LB.toString (A.encode (AM.insert "$fileName" (A.String i) o))) c)) + & httpLBS + >>= A.throwDecode . getResponseBody + +delete :: T.Text -> T.Text -> IO [A.Object] +delete c i = + "http://localhost:8081" + & setRequestMethod "POST" + & setRequestBodyLBS + (LB.fromString (printf "DELETE FROM %s WHERE %s.$fileName == \"%s\"" c c i)) + & httpLBS + >>= A.throwDecode . getResponseBody |