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-14 23:48:54 +03:00
|
|
|
import Database.Tag (attachTag, clearTags)
|
|
|
|
import Database.Channel (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
|
2018-08-14 23:48:54 +03:00
|
|
|
, tags :: [Text]
|
|
|
|
, channels :: [Text] }
|
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)
|
|
|
|
updateBook book@UpdateBook{..} = do
|
2018-08-14 23:48:54 +03:00
|
|
|
clearTags identifier >> connectTags
|
|
|
|
clearChannels identifier >> connectChannels
|
2018-08-14 22:19:55 +03:00
|
|
|
updateBook'
|
2018-08-08 22:21:15 +03:00
|
|
|
where
|
2018-08-14 22:19:55 +03:00
|
|
|
connectTags = mapM_ (attachTag owner identifier) tags
|
2018-08-14 23:48:54 +03:00
|
|
|
connectChannels = mapM_ (attachChannel owner identifier) channels
|
2018-08-14 22:19:55 +03:00
|
|
|
updateBook' = do
|
|
|
|
mUserId <- query (bookOwner' identifier owner)
|
|
|
|
forM (listToMaybe mUserId) $ \_userId -> do
|
|
|
|
update_ (gen books) predicate (\b -> b `with` [ pContentType := literal contentType
|
|
|
|
, pTitle := literal title
|
|
|
|
, pDescription := literal description ])
|
|
|
|
return book
|
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
|
|
|
|
|
|
|
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
|