2020-07-24 23:46:54 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
module App where
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
import Control.Monad.IO.Class (liftIO)
|
|
|
|
import Control.Monad.Logger (runStderrLoggingT)
|
2020-07-27 16:22:22 +02:00
|
|
|
import Data.Function ((&))
|
2020-07-24 23:46:54 +02:00
|
|
|
import Data.String.Conversions (cs)
|
|
|
|
import Data.Text (Text)
|
2020-07-27 16:22:22 +02:00
|
|
|
import Database.SQLite.Simple
|
2020-07-24 23:46:54 +02:00
|
|
|
import Network.Wai.Handler.Warp as Warp
|
|
|
|
import Servant
|
|
|
|
|
|
|
|
import API
|
2020-07-25 00:35:49 +02:00
|
|
|
import qualified Types as T
|
2020-07-24 23:46:54 +02:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-07-27 16:22:22 +02:00
|
|
|
server :: FilePath -> Server API
|
2020-07-28 10:10:54 +02:00
|
|
|
server dbFile = userAddH
|
|
|
|
:<|> userGetH
|
|
|
|
:<|> createTripH
|
2020-07-28 11:13:38 +02:00
|
|
|
:<|> listTripsH
|
2020-07-24 23:46:54 +02:00
|
|
|
where
|
|
|
|
userAddH newUser = liftIO $ userAdd newUser
|
|
|
|
userGetH name = liftIO $ userGet name
|
2020-07-28 10:10:54 +02:00
|
|
|
createTripH trip = liftIO $ createTrip trip
|
2020-07-28 11:13:38 +02:00
|
|
|
listTripsH = liftIO $ listTrips
|
2020-07-24 23:46:54 +02:00
|
|
|
|
2020-07-27 16:22:22 +02:00
|
|
|
-- TODO(wpcarro): Handle failed CONSTRAINTs instead of sending 500s
|
2020-07-25 19:18:59 +02:00
|
|
|
userAdd :: T.Account -> IO (Maybe T.Session)
|
2020-07-27 16:22:22 +02:00
|
|
|
userAdd account = withConnection dbFile $ \conn -> do
|
|
|
|
execute conn "INSERT INTO Accounts (username,password,email,role,profilePicture) VALUES (?,?,?,?,?)"
|
|
|
|
(account & T.accountFields)
|
|
|
|
T.Session{ T.username = T.accountUsername account
|
|
|
|
, T.password = T.accountPassword account
|
|
|
|
, T.role = T.accountRole account
|
|
|
|
} & Just & pure
|
2020-07-25 00:35:49 +02:00
|
|
|
|
2020-07-25 19:18:59 +02:00
|
|
|
userGet :: Text -> IO (Maybe T.Account)
|
2020-07-27 16:22:22 +02:00
|
|
|
userGet name = withConnection dbFile $ \conn -> do
|
|
|
|
res <- query conn "SELECT * FROM Accounts WHERE username = ?" (Only name)
|
|
|
|
case res of
|
|
|
|
[x] -> pure (Just x)
|
|
|
|
_ -> pure Nothing
|
2020-07-24 23:46:54 +02:00
|
|
|
|
2020-07-28 11:12:25 +02:00
|
|
|
createTrip :: T.Trip -> IO NoContent
|
2020-07-28 10:10:54 +02:00
|
|
|
createTrip trip = withConnection dbFile $ \conn -> do
|
|
|
|
execute conn "INSERT INTO Trips (username,destination,startDate,endDate,comment) VALUES (?,?,?,?,?)"
|
|
|
|
(trip & T.tripFields)
|
2020-07-28 11:12:25 +02:00
|
|
|
pure NoContent
|
2020-07-28 10:10:54 +02:00
|
|
|
|
2020-07-28 11:13:38 +02:00
|
|
|
listTrips :: IO [T.Trip]
|
|
|
|
listTrips = withConnection dbFile $ \conn -> do
|
|
|
|
query_ conn "SELECT * FROM Trips"
|
2020-07-24 23:46:54 +02:00
|
|
|
mkApp :: FilePath -> IO Application
|
2020-07-27 16:22:22 +02:00
|
|
|
mkApp dbFile = do
|
|
|
|
pure $ serve (Proxy @ API) $ server dbFile
|
2020-07-24 23:46:54 +02:00
|
|
|
|
|
|
|
run :: FilePath -> IO ()
|
|
|
|
run sqliteFile =
|
|
|
|
Warp.run 3000 =<< mkApp sqliteFile
|