Allow API users to create Trip entries

Next up:
- list trips
- update existing trip entries
- delete existing trip entries
This commit is contained in:
William Carroll 2020-07-28 09:10:54 +01:00
parent 475f62fb16
commit 52ac4d79bd
3 changed files with 79 additions and 3 deletions

View file

@ -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