Compare commits

...

3 Commits

Author SHA1 Message Date
d823a678c6 src/Configuration.hs Upserting books 2018-08-14 23:05:22 +03:00
7a86d5da4c Fix variable name 2018-08-14 22:28:35 +03:00
6f016e1283 WIP 2018-08-14 22:21:27 +03:00
2 changed files with 36 additions and 7 deletions

View File

@ -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

View File

@ -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