Compare commits
3 Commits
c0ee5d338d
...
d823a678c6
Author | SHA1 | Date | |
---|---|---|---|
d823a678c6 | |||
7a86d5da4c | |||
6f016e1283 |
@ -36,11 +36,11 @@ insertChannel username channel = do
|
|||||||
return userId
|
return userId
|
||||||
|
|
||||||
booksChannels :: (MonadMask m, MonadIO m) => BookID -> SeldaT m [Channel]
|
booksChannels :: (MonadMask m, MonadIO m) => BookID -> SeldaT m [Channel]
|
||||||
booksChannels contentHash = fromRels <$> query q
|
booksChannels bookId = fromRels <$> query q
|
||||||
where
|
where
|
||||||
q = do
|
q = do
|
||||||
channelId :*: contentHash' <- select (gen bookChannels)
|
channelId :*: bookId' <- select (gen bookChannels)
|
||||||
ch@(channelId' :*: _) <- select (gen channels)
|
ch@(channelId' :*: _) <- select (gen channels)
|
||||||
restrict (channelId .== channelId')
|
restrict (channelId .== channelId')
|
||||||
restrict (contentHash' .== literal contentHash)
|
restrict (bookId' .== literal bookId)
|
||||||
return ch
|
return ch
|
||||||
|
@ -4,11 +4,40 @@
|
|||||||
{-# Language DuplicateRecordFields #-}
|
{-# Language DuplicateRecordFields #-}
|
||||||
module Database.Tag
|
module Database.Tag
|
||||||
( def
|
( def
|
||||||
, Tag(..)
|
, booksTags
|
||||||
, TagID
|
, upsertTag
|
||||||
) where
|
, Tag(..) ) where
|
||||||
|
|
||||||
import ClassyPrelude
|
import ClassyPrelude
|
||||||
import Database
|
|
||||||
import Database.Schema
|
import Database.Schema
|
||||||
|
import Database
|
||||||
import Database.Selda
|
import Database.Selda
|
||||||
|
|
||||||
|
upsertTag :: (MonadMask m, MonadIO m, MonadSelda m) => Username -> Text -> m Tag
|
||||||
|
upsertTag username tag = transaction $ do
|
||||||
|
-- I want this to error out if some data is invariant is wrong and roll back
|
||||||
|
-- the transaction
|
||||||
|
[userId] <- query userQ
|
||||||
|
void $ upsert (gen tags) (predicate userId) id [toRel (Tag def tag userId)]
|
||||||
|
[t] <- fromRels <$> query (tagQ userId)
|
||||||
|
return t
|
||||||
|
where
|
||||||
|
predicate userId (_ :*: tag' :*: owner) = tag' .== literal tag .&& owner .== literal userId
|
||||||
|
tagQ userId = do
|
||||||
|
t@(_ :*: tag' :*: owner) <- select (gen tags)
|
||||||
|
restrict (tag' .== literal tag .&& owner .== literal userId)
|
||||||
|
return t
|
||||||
|
userQ = do
|
||||||
|
userId :*: _ :*: username' :*: _ <- select (gen users)
|
||||||
|
restrict (username' .== literal username)
|
||||||
|
return userId
|
||||||
|
|
||||||
|
booksTags :: (MonadMask m, MonadIO m, MonadSelda m) => BookID -> m [Tag]
|
||||||
|
booksTags bookId = fromRels <$> query q
|
||||||
|
where
|
||||||
|
q = do
|
||||||
|
tagId :*: bookId' <- select (gen bookTags)
|
||||||
|
tag@(tagId' :*: _) <- select (gen tags)
|
||||||
|
restrict (tagId .== tagId')
|
||||||
|
restrict (bookId' .== literal bookId)
|
||||||
|
return tag
|
||||||
|
Loading…
Reference in New Issue
Block a user