aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordan <[email protected]>2024-03-20 15:04:35 -0400
committerdan <[email protected]>2024-03-20 15:04:35 -0400
commit250e4bca9b449ee589e29f9c56744f02ab3d3685 (patch)
tree480f8554bc23d923df8d46c1b94c0ab2eaa82c93
parentfe339012688c113327503aa15945c4cbf524b9f5 (diff)
downloadbizexp-250e4bca9b449ee589e29f9c56744f02ab3d3685.tar.gz
bizexp-250e4bca9b449ee589e29f9c56744f02ab3d3685.tar.bz2
bizexp-250e4bca9b449ee589e29f9c56744f02ab3d3685.zip
feat: can insert rows and eval expressions by API
-rwxr-xr-x[-rw-r--r--]hs.cabal0
-rwxr-xr-x[-rw-r--r--]src/BizExpr.hs0
-rwxr-xr-x[-rw-r--r--]src/MyLib.hs0
-rwxr-xr-x[-rw-r--r--]src/RestService.hs34
4 files changed, 28 insertions, 6 deletions
diff --git a/hs.cabal b/hs.cabal
index 700d609..700d609 100644..100755
--- a/hs.cabal
+++ b/hs.cabal
diff --git a/src/BizExpr.hs b/src/BizExpr.hs
index 1026e47..1026e47 100644..100755
--- a/src/BizExpr.hs
+++ b/src/BizExpr.hs
diff --git a/src/MyLib.hs b/src/MyLib.hs
index c8dc95d..c8dc95d 100644..100755
--- a/src/MyLib.hs
+++ b/src/MyLib.hs
diff --git a/src/RestService.hs b/src/RestService.hs
index cb62423..6d92e69 100644..100755
--- a/src/RestService.hs
+++ b/src/RestService.hs
@@ -58,6 +58,16 @@ getTable s n = atomically $ IM.lookup n <$> readTVar s
setTable :: State -> Int -> Table -> IO (Maybe Table)
setTable s n t = atomically $ (\x -> writeTVar s x >> return (Just t)) . IM.insert n t =<< readTVar s
+insertRow :: State -> Int -> TableRow -> IO (Maybe Table)
+insertRow s n row = atomically $ do
+ tables <- readTVar s
+ case IM.lookup n tables of
+ Nothing -> return Nothing
+ Just table ->
+ let updatedTable = addRow table row
+ updatedTables = IM.insert n updatedTable tables
+ in writeTVar s updatedTables >> return (Just updatedTable)
+
routes :: State -> ScottyM ()
routes state = do
S.middleware addHeaders
@@ -66,9 +76,18 @@ routes state = do
S.json $ evalTableExpressions (TableExpressions dummyTable ["sum(2,3,5)", "any(0,0,1)", "sum(age, -10)", "any(0,1)"])
get "/t/:id" $
S.json =<< liftAndCatchIO . getTable state =<< S.param "id"
+ get "/t/:id/e/:e" $ do
+ id <- S.param "id"
+ ex <- S.param "e"
+ S.json
+ . fmap (evalTableExpressions . flip TableExpressions [ex])
+ =<< liftAndCatchIO (getTable state id)
post "/t/:id" $ do
id <- S.param "id"
S.json =<< liftAndCatchIO . setTable state id =<< S.jsonData
+ post "/t/:id/row" $ do
+ id <- S.param "id"
+ S.json =<< liftAndCatchIO . insertRow state id =<< S.jsonData
get "/:expr" $
S.text =<< S.param "expr"
post "/eval" $
@@ -131,19 +150,22 @@ dummyTable =
{ table_name = "dummy table",
table_headings = ["name", "age", "height"],
table_rows =
- [ M.fromList
- [ ("name", "alice"),
- ("age", "30")
- ],
+ map
M.fromList
+ [ [ ("name", "alice"),
+ ("age", "30"),
+ ("height", "1.60")
+ ],
[ ("name", "bob"),
("age", "40")
],
- M.fromList
[ ("name", "charles"),
("height", "1.80")
+ ],
+ [ ("age", "30"),
+ ("height", "1.60")
]
- ]
+ ]
}
addRow :: Table -> TableRow -> Table