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
|
|
|
|
server dbFile =
|
2020-07-24 23:46:54 +02:00
|
|
|
userAddH :<|> userGetH
|
|
|
|
where
|
|
|
|
userAddH newUser = liftIO $ userAdd newUser
|
|
|
|
userGetH name = liftIO $ userGet name
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|