@@ -59,43 +59,11 @@ import qualified Web.Scotty as S
5959-- with the help of plants. \\
6060-- This is a very simple example and of course not how
6161-- plants grow in nature. \\
62−-- The main goal of this example is to showcas the updating
63−-- of data and the different options of persistance.
64−
65−
66−
67−-- | Here is a python script for pretty printing the output \
68−-- if you include the acii:
69−{-|
70−#!/usr/bin/python3
71−# this can be replaced with the path to your python executable
72−# this is a vary hacky script just for visualisation of the json response
73−import json
74−import sys
75−import re
76−
77−for line in sys.stdin:
78− parsed = json.loads(line)
79− jstr = json.dumps(parsed, indent=4, sort_keys=True)
80− leng = len(re.findall(r"((?: )*\"stage\": \")", jstr)[0])
81− l = jstr.encode('utf-8').decode('unicode_escape')
82− ls = l.split("\n")
83− res = ""
84− for i in ls:
85− if not re.match(r"(?: )*(?:{|\"\w|}|\[|\])", i):
86− spaces = leng
87− if re.match(r"(?: )*\",$", i):
88− spaces -= 1
89− i = " " * spaces + i
90− res += i + "\n"
91− print(res)
92−-}
93−-- Either you make it executable with chmod +x scriptfilename or you \
94−-- execute it with python3 scriptfilename. \
95−-- Either way you then can use it to prettyprint when making a request. \
96−-- You can just pipe the putput form culr for eamxple into this command:
62+-- The main goal of this example is to showcase the updating
63+-- of data and the different options of persistence.
64+-- You can just pipe the output from curl into jq:
9765--
98−-- curl -X POST -H 'Accept: application/json' localhost:8001/plant/water/angu | python3 scriptfilename
66+-- curl -X POST -H 'Accept: application/json' http://localhost:8001/plant/water/angu | jq -r '.stage'
9967
10068
10169data Stage = Seed | Sprout | Seedling | Adult | Dead
@@ -176,7 +144,7 @@ main = do
176144 putStrLn $ "Started Plant farm at " <> show port
177145 S.scotty port $ do
178146
179− -- retive a plant by name
147+ -- retrieve a plant by name
180148 S.get "/plant/:name" $ do
181149 n <- S.param "name"
182150 e <- liftIO $ getPlant c n
@@ -199,7 +167,7 @@ main = do
199167 S.json p
200168
201169 -- watering the plant having the provided name.
202− -- This will water the plant and migth let it progress to the next stage. It might also die.
170+ -- This will water the plant and might let it progress to the next stage. It might also die.
203171 S.post "/plant/water/:name" $ do
204172 n <- S.param "name"
205173 e <- liftIO $ waterPlant c n
@@ -219,14 +187,14 @@ main = do
219187 p <- handleWebError e
220188 S.json p
221189
222− -- deleting all plants at a speciffic stage
190+ -- deleting all plants at a specific stage
223191 S.delete "/plants?stage=:stage" $ do
224192 s <- S.param "stage"
225193 e <- liftIO $ deletePlantsByStage c s
226194 p <- handleWebError e
227195 S.json p
228196
229− -- deleting all plants at a speciffic stage
197+ -- deleting all plants at a specific stage
230198 S.delete "/plants" $ do
231199 s <- S.param "name"
232200 e <- liftIO $ deletePlantByName c s
@@ -240,17 +208,17 @@ main = do
240208 S.json ps
241209
242210handleWebError :: Either Err b -> S.ActionM b
243−handleWebError (Left e) = S.raise (TL.pack $ "An error Orrcured:\n" <> show e)
211+handleWebError (Left e) = S.raise (TL.pack $ "An error occurred:\n" <> show e)
244212handleWebError (Right v) = pure v
245213
246214handleWebErrors :: [Either Err b] -> S.ActionM [b]
247215handleWebErrors e = do
248216 case lefts e of
249217 [] -> pure (rights e)
250− l -> S.raise (TL.pack $ "Errors Orrcured:\n" <> concatMap ((<>"\n") . show)l)
218+ l -> S.raise (TL.pack $ "Errors occurred:\n" <> concatMap ((<>"\n") . show) l)
251219
252220
253−-- | watering a plant and therby possibly updating its stage
221+-- | watering a plant and thereby possibly updating its stage
254222waterPlant :: DBConnection -> Text -> IO (Either Err Plant)
255223waterPlant db n = do
256224 p <- getPlant db n
@@ -271,9 +239,9 @@ updateStage db p = do
271239 case tmp of
272240 Left e -> pure $ Left e
273241 Right _ -> pure $ Right res
274− where calculateStage p = do
242+ where calculateStage p' = do
275243 r1 <- R.randomRIO (1, 10) :: IO Integer
276− let np = if r1 < waterings p then p { stage = next $ stage p, waterings = 0 } else p
244+ let np = if r1 < waterings p' then p' { stage = next $ stage p', waterings = 0 } else p'
277245 r2 <- R.randomRIO (1, 20)
278246 pure $ if r2 < waterings np then np { stage = Dead, waterings = 0 } else np
279247
@@ -419,7 +387,7 @@ update db rlv attr rlvName = executeWithTransaction db $ toUpdateExpr rlvName at
419387delete :: (Tupleable a) => DBConnection -> a -> [AttributeName]-> Base.RelVarName -> IO (Either Err ())
420388delete db rlv attr rlvName = executeWithTransaction db $ toDeleteExpr rlvName attr rlv
421389
422−-- | A convinience function to make executing DBContextExpr with commiting simpler. \\
390+-- | A convenience function to make executing DBContextExpr with commiting simpler. \\
423391-- In particular for expr that just insert, delete and update.
424392-- Therefor ultimately return Either _ ()
425393executeWithTransaction :: DBConnection -> Either RelationalError DatabaseContextExpr -> IO (Either Err ())
@@ -448,7 +416,7 @@ closeConn (DB _ conn) = close conn
448416
449417-- | Error handling is heavily inspired by 'blog.hs' the blog example \\
450418-- by (agentm)[https://github.com/agentm] (https://github.com/agentm/project-m36, commit: f8432522adaafeae7c32bc2b8b6cb09c00396fc6). \\
451−-- As stated there your application should have propper error handling.
419+-- As stated there, your application should have proper error handling.
452420handleIOErrorAndQuit :: Show e => IO (Either e a) -> IO a
453421handleIOErrorAndQuit m = do
454422 v <- m