aboutsummaryrefslogtreecommitdiffstats
path: root/backend/lib/ACMS/API/REST/Collection.hs
blob: 09f7e326872effd641f7c5bc47fa227d41322b1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{-# LANGUAGE OverloadedStrings #-}

module ACMS.API.REST.Collection where

#ifndef ghcjs_HOST_OS
import Network.HTTP.Simple
#else
import ACMS.API.REST (setRequestMethod, setRequestBodyLBS, getResponseBody)
import Data.ByteString.Char8 qualified as B
import Data.Maybe
import JavaScript.Web.XMLHttpRequest
import Miso.String qualified as J
#endif
import Collection
import ACMS.API.REST (APIMonad, fetch, restRequest)
import Data.Aeson qualified as A
import Data.Function ((&))
import Miso.String (MisoString)
import Text.Printf (printf)
import Debug.Trace

list :: (APIMonad m) => Collection -> m [A.Object]
list c =
  restRequest (printf "/collection/%s" c.name)
    & fetch
    >>= A.throwDecode

read :: (APIMonad m) => CollectionItem -> m [A.Object]
read ci =
  restRequest (printf "/collection/%s/%s" ci.collection.name ci.itemFileName)
    & fetch
    >>= A.throwDecode

update :: (APIMonad m) => CollectionItem -> A.Object -> m A.Object
update ci o =
  restRequest (printf "/collection/%s/%s" ci.collection.name ci.itemFileName)
    & setRequestMethod "PUT"
    & setRequestBodyLBS (A.encode o)
    & fetch
    >>= A.throwDecode

create :: (APIMonad m) => Collection -> A.Object -> m A.Object
create c o = do
  restRequest (printf "/collection/%s" c.name)
    & setRequestMethod "POST"
    & setRequestBodyLBS (A.encode o)
    & fetch
    >>= A.throwDecode

delete :: (APIMonad m) => CollectionItem -> m [A.Object]
delete ci =
  restRequest (printf "/collection/%s/%s" ci.collection.name ci.itemFileName)
    & setRequestMethod "DELETE"
    & fetch
    >>= A.throwDecode

schema :: (APIMonad m) => (A.FromJSON a) => Collection -> m a
schema c =
  restRequest (printf "/collection/%s/schema" c.name)
    & fetch
    >>= A.throwDecode