Intailly working app.

This commit is contained in:
Dhananjay Balan
2023-02-27 23:46:29 +05:30
parent 6921c98014
commit ff7cfdc796
4 changed files with 102 additions and 28 deletions

View File

@@ -1,34 +1,17 @@
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeOperators #-}
module Main where
import Data.Text (Text)
import Data.Aeson
import Database.SQLite.Simple
import Database.SQLite.Simple.QQ
import Network.Wai.Handler.Warp
import Deriving.Aeson
import Data.Proxy
import Servant
import Control.Monad.IO.Class
data Quote = Quote { qQuote :: Text
, qAuthor :: Text
, qTitle :: Text
-- , qPage :: Text
-- , qChapter :: Text
-- , qTime :: UnixTime
} deriving (Show, Eq, Ord, Generic)
deriving (FromJSON,ToJSON)
via CustomJSON '[OmitNothingFields, FieldLabelModifier '[StripPrefix "q", CamelToSnake]] Quote
instance FromRow Quote where
fromRow = Quote <$> field <*> field <*> field
import Api.Types
import qualified Parsers.KOReader as KO
main :: IO ()
main = do
@@ -38,7 +21,9 @@ main = do
runApp dbfile
type API = Get '[JSON] [Quote]
type API = "quotes" :> Get '[JSON] [Quote]
:<|> "quote" :> "random" :> Get '[JSON] Quote
:<|> "koreader" :> ReqBody '[JSON] KO.KoHighlight :> Post '[JSON] NoContent
api :: Proxy API
api = Proxy
@@ -46,14 +31,37 @@ api = Proxy
initDb :: FilePath -> IO ()
initDb dbFile = withConnection dbFile $ \conn ->
execute_ conn
[sql|CREATE TABLE IF NOT EXISTS quotes (quote text non null, author text, book text)|]
[sql|CREATE TABLE IF NOT EXISTS quotes ( quote text non null
, author text
, title text
, page text
, chapter text
, created_on integer);|]
-- | TODO: readerT
server :: FilePath -> Server API
server dbf = listQuotes dbf
server dbf = listQuotes dbf :<|> randomQuote dbf :<|> addKoReader dbf
-- | API begins here
randomQuote :: FilePath -> Handler Quote
randomQuote db = do
qts <- (liftIO $ withConnection db $ \c -> query_ c qry)
case (length qts) of
0 -> undefined
_ -> pure (head qts)
where
qry = [sql|SELECT * FROM quotes ORDER BY RANDOM();|]
listQuotes :: FilePath -> Handler [Quote]
listQuotes db = liftIO $ withConnection db $ \conn -> query_ conn [sql|SELECT * FROM quotes;|]
addKoReader :: FilePath -> KO.KoHighlight -> Handler NoContent
addKoReader db hl = do
liftIO $ withConnection db $ \c ->
executeMany c qry qts
pure NoContent
where
qry = [sql|INSERT INTO quotes VALUES (?,?,?,?,?,?);|]
qts = KO.parse hl
runApp :: FilePath -> IO ()
runApp dbfile = run 8081 (serve api $ server dbfile)