ebook-manager/src/Database/Book.hs

133 lines
5.0 KiB
Haskell
Raw Normal View History

2018-08-05 23:42:37 +03:00
{-# Language TypeApplications #-}
2018-08-08 23:56:16 +03:00
{-# Language TypeOperators #-}
2018-08-05 23:42:37 +03:00
{-# Language DataKinds #-}
2018-08-08 22:21:15 +03:00
{-# Language DuplicateRecordFields #-}
2018-08-07 23:25:21 +03:00
module Database.Book
( def
, insertBook
2018-08-08 23:56:16 +03:00
, getBook
, bookExists
2018-08-08 22:21:15 +03:00
, updateBook
2018-08-08 23:56:16 +03:00
, isBookOwner
, setContent
2018-08-08 21:58:36 +03:00
, InsertBook(..)
2018-08-08 22:21:15 +03:00
, UpdateBook(..)
2018-08-07 23:25:21 +03:00
, usersBooks
, Book(..)
2018-08-08 23:56:16 +03:00
, HashDigest(..)
2018-08-07 23:25:21 +03:00
, BookID) where
2018-08-05 23:42:37 +03:00
import ClassyPrelude
2018-08-14 22:19:55 +03:00
import Database.Schema (books, users, Username, Book(..), BookID(..), UserID, HashDigest(..))
2018-08-05 23:42:37 +03:00
import Database
import Database.Selda
2018-08-07 23:25:21 +03:00
import Database.Selda.Generic
2018-08-05 23:42:37 +03:00
2018-08-15 22:10:15 +03:00
import Control.Lens (view)
import Data.Generics.Product
import Database.Tag (booksTags, attachTag, clearTags)
import Database.Channel (booksChannels, attachChannel, clearChannels)
2018-08-14 22:19:55 +03:00
2018-08-07 23:25:21 +03:00
usersBooks :: (MonadSelda m, MonadMask m, MonadIO m) => Username -> m [Book]
2018-08-05 23:42:37 +03:00
usersBooks username = fromRels <$> query q
where
q = do
userId :*: _ :*: username' :*: _ <- select (gen users)
2018-08-08 23:56:16 +03:00
book@(_ :*: digest :*: _ :*: _ :*: _ :*: owner) <- select (gen books)
2018-08-05 23:42:37 +03:00
restrict (username' .== literal username)
2018-08-08 21:58:36 +03:00
restrict (userId .== owner)
2018-08-08 23:56:16 +03:00
restrict (not_ (isNull digest))
return book
getBook :: (MonadSelda m, MonadMask m, MonadIO m) => BookID -> Username -> m (Maybe Book)
getBook identifier owner = listToMaybe . fromRels <$> query q
where
q = do
_ :*: bookId <- bookOwner' identifier owner
book@(bookId' :*: _) <- select (gen books)
restrict (bookId .== bookId')
2018-08-05 23:42:37 +03:00
return book
2018-08-08 21:58:36 +03:00
data InsertBook = InsertBook { contentType :: Text
, title :: Maybe Text
, description :: Maybe Text
, owner :: Username }
2018-08-07 23:25:21 +03:00
-- Always inserts
2018-08-08 21:58:36 +03:00
insertBook :: (MonadSelda m, MonadMask m, MonadIO m) => InsertBook -> m (Maybe BookID)
insertBook InsertBook{..} = do
2018-08-07 23:25:21 +03:00
mUserId <- query $ do
userId :*: _ :*: username' :*: _ <- select (gen users)
2018-08-08 21:58:36 +03:00
restrict (username' .== literal owner)
2018-08-07 23:25:21 +03:00
return userId
forM (listToMaybe mUserId) $ \userId -> do
2018-08-08 21:58:36 +03:00
let book = Book{owner=userId,identifier=def,contentHash=Nothing,..}
BookID . fromRowId <$> insertGenWithPK books [book]
2018-08-08 22:21:15 +03:00
data UpdateBook = UpdateBook { identifier :: BookID
, contentType :: Text
, title :: Maybe Text
, description :: Maybe Text
2018-08-14 22:19:55 +03:00
, owner :: Username
, tags :: [Text]
, channels :: [Text] }
2018-08-15 22:10:15 +03:00
deriving (Show, Generic)
2018-08-08 22:21:15 +03:00
2018-08-08 23:56:16 +03:00
bookExists :: (MonadSelda m, MonadMask m, MonadIO m) => BookID -> m Bool
bookExists identifier = not . null <$> query q
where
q = do
(bookId :*: _) <- select (gen books)
restrict (bookId .== literal identifier)
return bookId
isBookOwner :: (MonadSelda m, MonadIO m, MonadThrow m) => BookID -> Username -> m Bool
isBookOwner identifier username = not . null <$> query (bookOwner' identifier username)
bookOwner' :: BookID -> Username -> Query s (Col s UserID :*: Col s BookID)
bookOwner' identifier username = do
userId :*: _ :*: username' :*: _ <- select (gen users)
bookId :*: _ :*: _ :*: _ :*: _ :*: bookOwner <- select (gen books)
restrict (userId .== bookOwner)
restrict (username' .== literal username)
restrict (bookId .== literal identifier)
return (userId :*: bookId)
2018-08-08 22:21:15 +03:00
updateBook :: (MonadSelda m, MonadMask m, MonadIO m) => UpdateBook -> m (Maybe UpdateBook)
2018-08-15 22:10:15 +03:00
updateBook UpdateBook{..} = do
clearTags identifier >> connectTags
clearChannels identifier >> connectChannels
2018-08-14 22:19:55 +03:00
updateBook'
2018-08-15 22:10:15 +03:00
getUpdateBook identifier owner
2018-08-08 22:21:15 +03:00
where
2018-08-14 22:19:55 +03:00
connectTags = mapM_ (attachTag owner identifier) tags
connectChannels = mapM_ (attachChannel owner identifier) channels
2018-08-14 22:19:55 +03:00
updateBook' = do
mUserId <- query (bookOwner' identifier owner)
2018-08-15 22:10:15 +03:00
forM_ (listToMaybe mUserId) $ \_userId -> do
2018-08-14 22:19:55 +03:00
update_ (gen books) predicate (\b -> b `with` [ pContentType := literal contentType
, pTitle := literal title
, pDescription := literal description ])
2018-08-08 22:21:15 +03:00
_ :*: _ :*: pContentType :*: pTitle :*: pDescription :*: _ = selectors (gen books)
predicate (bookId :*: _) = bookId .== literal identifier
2018-08-08 23:56:16 +03:00
2018-08-15 22:10:15 +03:00
getUpdateBook :: (MonadMask m, MonadIO m, MonadSelda m) => BookID -> Username -> m (Maybe UpdateBook)
getUpdateBook bookId username = do
mBook <- getBook bookId username
forM mBook $ \Book{..} -> do
channels <- map (view (field @"channel")) <$> booksChannels bookId
tags <- map (view (field @"tag")) <$> booksTags bookId
return UpdateBook{owner=username,..}
2018-08-08 23:56:16 +03:00
setContent :: (MonadSelda m, MonadMask m, MonadIO m) => BookID -> Username -> HashDigest -> m ()
setContent identifier owner digest = do
mOwner <- query (bookOwner' identifier owner)
void $ forM (listToMaybe mOwner) $ \_ ->
update_ (gen books) predicate (\b -> b `with` [ pHash := literal (Just digest)])
where
_ :*: pHash :*: _ = selectors (gen books)
predicate (bookId :*: _) = bookId .== literal identifier