2018-08-05 23:42:37 +03:00
|
|
|
{-# Language DataKinds #-}
|
|
|
|
{-# Language TypeFamilies #-}
|
|
|
|
{-# Language TypeOperators #-}
|
|
|
|
{-# Language NoImplicitPrelude #-}
|
|
|
|
{-# Language MultiParamTypeClasses #-}
|
|
|
|
{-# Language OverloadedStrings #-}
|
|
|
|
{-# Language TemplateHaskell #-}
|
|
|
|
{-# Language QuasiQuotes #-}
|
|
|
|
{-# Language RecordWildCards #-}
|
|
|
|
{-# Language DeriveGeneric #-}
|
|
|
|
{-# Language FlexibleInstances #-}
|
|
|
|
{-# Language TypeApplications #-}
|
|
|
|
{-# Language DataKinds #-}
|
|
|
|
module API.Books where
|
|
|
|
|
|
|
|
import Servant hiding (contentType)
|
|
|
|
import Types
|
|
|
|
import ClassyPrelude
|
|
|
|
import Server.Auth
|
|
|
|
import Servant.Auth as SA
|
|
|
|
import Data.Aeson
|
|
|
|
import API.Channels (JsonChannel(..))
|
|
|
|
import Database.Book
|
|
|
|
import Database.Channel
|
|
|
|
import Database
|
|
|
|
import Control.Lens
|
|
|
|
import Data.Generics.Product
|
|
|
|
|
|
|
|
data JsonBook = JsonBook { contentType :: Text
|
|
|
|
, title :: Maybe Text
|
|
|
|
, description :: Maybe Text
|
|
|
|
, channels :: [JsonChannel] }
|
|
|
|
deriving (Generic, Show)
|
|
|
|
|
2018-08-06 00:09:41 +03:00
|
|
|
|
2018-08-05 23:42:37 +03:00
|
|
|
instance ToJSON JsonBook
|
|
|
|
instance FromJSON JsonBook
|
|
|
|
|
|
|
|
type API = Auth '[SA.BasicAuth, SA.JWT] SafeUser :> BaseAPI
|
|
|
|
|
|
|
|
type BaseAPI = "books" :> Get '[JSON] [JsonBook]
|
2018-08-06 00:09:41 +03:00
|
|
|
-- :<|> "books" :> ReqBody '[JSON] JsonBook :> PUT JsonBook
|
|
|
|
-- :<|> "books" :> Param "book_id" BookID :> ReqBody '[JSON] JsonBook :> PUT JsonBook
|
2018-08-05 23:42:37 +03:00
|
|
|
|
|
|
|
handler :: ServerT API AppM
|
|
|
|
handler user = listBooksHandler user
|
|
|
|
|
|
|
|
listBooksHandler :: AuthResult SafeUser -> AppM [JsonBook]
|
|
|
|
listBooksHandler = requireLoggedIn $ \user -> do
|
|
|
|
runDB (usersBooks (view (field @"username") user) >>= mapM augment)
|
|
|
|
where
|
|
|
|
augment Book{..} = do
|
|
|
|
channels <- fmap (\Channel{..} -> JsonChannel{..}) <$> booksChannels contentHash
|
|
|
|
pure JsonBook{..}
|