@@ -76,12 +76,17 @@ getAllQuery = Query (T.pack "SELECT account, amount FROM events;")
76
76
-- NOTE! Do not add anything to the name, otherwise you'll get weird
77
77
-- test failures later.
78
78
openDatabase :: String -> IO Connection
79
- openDatabase = todo
79
+ openDatabase f = do
80
+ db <- open f
81
+ execute_ db initQuery
82
+ return db
80
83
81
84
-- given a db connection, an account name, and an amount, deposit
82
85
-- should add an (account, amount) row into the database
83
86
deposit :: Connection -> T. Text -> Int -> IO ()
84
- deposit = todo
87
+ deposit db account amount = do
88
+ execute db depositQuery (account, amount)
89
+ return ()
85
90
86
91
------------------------------------------------------------------------------
87
92
-- Ex 2: Fetching an account's balance. Below you'll find
@@ -112,7 +117,9 @@ balanceQuery :: Query
112
117
balanceQuery = Query (T. pack " SELECT amount FROM events WHERE account = ?;" )
113
118
114
119
balance :: Connection -> T. Text -> IO Int
115
- balance = todo
120
+ balance db account = do
121
+ rows <- query db balanceQuery [account] :: IO [[Int ]]
122
+ return $ sum $ map head rows
116
123
117
124
------------------------------------------------------------------------------
118
125
-- Ex 3: Now that we have the database part covered, let's think about
@@ -144,14 +151,22 @@ balance = todo
144
151
-- parseCommand [T.pack "deposit", T.pack "madoff", T.pack "123456"]
145
152
-- ==> Just (Deposit "madoff" 123456)
146
153
147
- data Command = Deposit T. Text Int | Balance T. Text
154
+ data Command = Deposit T. Text Int | Balance T. Text | Withdraw T. Text Int
148
155
deriving (Show , Eq )
149
156
150
157
parseInt :: T. Text -> Maybe Int
151
158
parseInt = readMaybe . T. unpack
152
159
153
160
parseCommand :: [T. Text ] -> Maybe Command
154
- parseCommand = todo
161
+ parseCommand cmd
162
+ | length cmd == 2 && head cmd == T. pack " balance" = Just $ Balance (cmd !! 1 )
163
+ | length cmd == 3 = case parseInt $ cmd !! 2 of
164
+ Just amount -> case T. unpack $ head cmd of
165
+ " deposit" -> Just $ Deposit (cmd !! 1 ) amount
166
+ " withdraw" -> Just $ Withdraw (cmd !! 1 ) amount
167
+ otherwise -> Nothing
168
+ Nothing -> Nothing
169
+ | otherwise = Nothing
155
170
156
171
------------------------------------------------------------------------------
157
172
-- Ex 4: Running commands. Implement the IO operation perform that takes a
@@ -177,7 +192,17 @@ parseCommand = todo
177
192
-- "0"
178
193
179
194
perform :: Connection -> Maybe Command -> IO T. Text
180
- perform = todo
195
+ perform db cmd = case cmd of
196
+ Just (Deposit account amount) -> do
197
+ deposit db account amount
198
+ return (T. pack " OK" )
199
+ Just (Balance account) -> do
200
+ balance' <- balance db account
201
+ return (T. pack $ show balance')
202
+ Just (Withdraw account amount) -> do
203
+ deposit db account (- amount)
204
+ return (T. pack " OK" )
205
+ otherwise -> return (T. pack " ERROR" )
181
206
182
207
------------------------------------------------------------------------------
183
208
-- Ex 5: Next up, let's set up a simple HTTP server. Implement a WAI
@@ -197,7 +222,7 @@ encodeResponse t = LB.fromStrict (encodeUtf8 t)
197
222
-- Remember:
198
223
-- type Application = Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
199
224
simpleServer :: Application
200
- simpleServer request respond = todo
225
+ simpleServer request respond = respond $ responseLBS status200 [] (encodeResponse ( T. pack " BANK " ))
201
226
202
227
------------------------------------------------------------------------------
203
228
-- Ex 6: Now we finally have all the pieces we need to actually
@@ -226,7 +251,10 @@ simpleServer request respond = todo
226
251
-- Remember:
227
252
-- type Application = Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
228
253
server :: Connection -> Application
229
- server db request respond = todo
254
+ server db request respond = do
255
+ let cmd = parseCommand (pathInfo request)
256
+ response <- perform db cmd
257
+ respond $ responseLBS status200 [] (encodeResponse response)
230
258
231
259
port :: Int
232
260
port = 3421
@@ -257,6 +285,7 @@ main = do
257
285
-- - Open <http://localhost:3421/balance/simon> in your browser.
258
286
-- You should see the text 11.
259
287
288
+ -- Ex 3 & Ex4
260
289
261
290
------------------------------------------------------------------------------
262
291
-- Ex 8: Error handling. Modify the parseCommand function so that it
@@ -277,4 +306,4 @@ main = do
277
306
-- - http://localhost:3421/balance
278
307
-- - http://localhost:3421/balance/matti/pekka
279
308
280
-
309
+ -- Ex 3 & Ex4
0 commit comments