about summary refs log tree commit diff
path: root/src/App.hs
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-07-28T08·10+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-07-28T08·12+0100
commit52ac4d79bda2c5f5cc2ff636e79b4bf3b5979868 (patch)
tree79d32e30c2e0c0227d30e542878fa3a816ee81d7 /src/App.hs
parent475f62fb16fb29e55548cc8b238caea8bf60bd8f (diff)
Allow API users to create Trip entries
Next up:
- list trips
- update existing trip entries
- delete existing trip entries
Diffstat (limited to 'src/App.hs')
-rw-r--r--src/App.hs12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/App.hs b/src/App.hs
index b80a3ba4f619..20d99e385ad2 100644
--- a/src/App.hs
+++ b/src/App.hs
@@ -17,11 +17,13 @@ import qualified Types as T
 --------------------------------------------------------------------------------
 
 server :: FilePath -> Server API
-server dbFile =
-  userAddH :<|> userGetH
+server dbFile = userAddH
+           :<|> userGetH
+           :<|> createTripH
   where
     userAddH newUser = liftIO $ userAdd newUser
     userGetH name    = liftIO $ userGet name
+    createTripH trip = liftIO $ createTrip trip
 
     -- TODO(wpcarro): Handle failed CONSTRAINTs instead of sending 500s
     userAdd :: T.Account -> IO (Maybe T.Session)
@@ -40,6 +42,12 @@ server dbFile =
         [x] -> pure (Just x)
         _   -> pure Nothing
 
+    createTrip :: T.Trip -> IO Bool
+    createTrip trip = withConnection dbFile $ \conn -> do
+      execute conn "INSERT INTO Trips (username,destination,startDate,endDate,comment) VALUES (?,?,?,?,?)"
+        (trip & T.tripFields)
+      pure True
+
 mkApp :: FilePath -> IO Application
 mkApp dbFile = do
   pure $ serve (Proxy @ API) $ server dbFile