aboutsummaryrefslogtreecommitdiffstats
path: root/backend/app
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-10-13 11:55:20 +0200
committerLibravatar Alexander Foremny <aforemny@posteo.de>2024-10-13 11:55:20 +0200
commite66534eefb5979c1ec5e0a28e9c29969ae2c9884 (patch)
tree3b919222ab147741cb9332537997e590b104eb01 /backend/app
parent997442c6b99bef99c429d4ab7f676fdf9ae09096 (diff)
improve REST API
Diffstat (limited to 'backend/app')
-rw-r--r--backend/app/Main.hs19
1 files changed, 13 insertions, 6 deletions
diff --git a/backend/app/Main.hs b/backend/app/Main.hs
index 8070906..c31ea7d 100644
--- a/backend/app/Main.hs
+++ b/backend/app/Main.hs
@@ -285,6 +285,8 @@ queryApi root ref repoT app req resp = do
q <- fromString @Q.Query . LB.toString <$> W.lazyRequestBody req
resp . W.responseLBS W.status200 [] . J.encode
=<< Q.withStore root ref do Q.query q
+ _ -> do
+ error "not implemented"
_ -> app req resp
restApi :: FilePath -> T.Text -> TMVar Repo -> W.Middleware
@@ -311,9 +313,11 @@ restApi root ref repoT app req resp = do
resp . W.responseLBS W.status200 [] $
J.encode (map (.path) lastCompatibleCommit.collections)
("GET", ["collection", T.unpack -> c, "schema"]) -> do
- let [collection] = filter ((== c) . (.path)) lastCompatibleCommit.collections
- resp . W.responseLBS W.status200 [] $
- J.encode (fromAutoTypes c collection.schema)
+ case find ((== c) . (.path)) lastCompatibleCommit.collections of
+ Nothing -> error "not implemented"
+ Just collection ->
+ resp . W.responseLBS W.status200 [] $
+ J.encode (fromAutoTypes c collection.schema)
("POST", ["collection"]) -> do
when (not (sameCommit lastCompatibleCommit lastCommit)) $ error "not implemented"
Right collection <- J.eitherDecode <$> W.lazyRequestBody req
@@ -327,16 +331,17 @@ restApi root ref repoT app req resp = do
Q.query (fromString (printf "SELECT %s FROM %s" c c))
)
("GET", ["collection", c, i]) -> do
- resp . W.responseLBS W.status200 [] . J.encode
+ resp . W.responseLBS W.status200 [] . J.encode . headMay
=<< ( Q.withStore root ref $ Q.withCommit rev do
Q.query (fromString (printf "SELECT %s FROM %s WHERE %s.$fileName == \"%s\"" c c c i))
)
("PUT", ["collection", c, i]) -> do
when (not (sameCommit lastCompatibleCommit lastCommit)) $ error "not implemented"
o <- J.throwDecode @J.Object =<< W.lazyRequestBody req
- resp . W.responseLBS W.status200 [] . J.encode
+ resp . W.responseLBS W.status200 [] . J.encode . headMay
=<< ( Q.withStore root ref do
Q.query (fromString (printf "UPDATE %s SET %s WHERE %s.$fileName == \"%s\"" c (LB.toString (J.encode o)) c i))
+ Q.query (fromString (printf "SELECT %s FROM %s WHERE %s.$fileName == \"%s\"" c c c i))
)
("POST", ["collection", c]) -> do
when (not (sameCommit lastCompatibleCommit lastCommit)) $ error "not implemented"
@@ -349,9 +354,11 @@ restApi root ref repoT app req resp = do
)
("DELETE", ["collection", c, i]) -> do
when (not (sameCommit lastCompatibleCommit lastCommit)) $ error "not implemented"
- resp . W.responseLBS W.status200 [] . J.encode
+ resp . W.responseLBS W.status200 [] . J.encode . headMay
=<< ( Q.withStore root ref do
+ r <- Q.query (fromString (printf "SELECT %s FROM %s WHERE %s.$fileName == \"%s\"" c c c i))
Q.query (fromString (printf "DELETE FROM %s WHERE %s.$fileName == \"%s\"" c c i))
+ pure r
)
(method, path) -> fail $ "Method " ++ show method ++ " on route " ++ show path ++ " not supported."
_ -> app req resp