Guest User

Untitled

a guest
Jul 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Posts.Models where
  3. import Prelude hiding (lookup)
  4. import Data.Time.Clock (UTCTime)
  5. import Data.ByteString (ByteString)
  6. import qualified Data.ByteString as B
  7. import Control.Monad (mapM)
  8. import Control.Monad.Trans.Reader
  9. import Control.Applicative (Applicative(..), (<$>), (<*>))
  10. import Database.MongoDB
  11. import Database.MongoDB.Extensions
  12.  
  13. data Post = Post {
  14. title :: String
  15. , content :: String
  16. , date :: Maybe UTCTime
  17. , author :: Maybe String
  18. } deriving (Show)
  19.  
  20. main :: IO ()
  21. main = do
  22. pool <- newConnPool 1 (host "127.0.0.1")
  23. result <- access safe Master pool dbExec
  24. print result
  25.  
  26. dbExec = use (Database "blog") postsListedByDate
  27.  
  28. documentToPost :: (Monad m, Applicative m) => Document -> m Post
  29. documentToPost doc =
  30. Post <$> (lookup "title" doc)
  31. <*> (lookup "content" doc)
  32. <*> (lookup "date" doc)
  33. <*> (lookup "author" doc)
  34.  
  35. postsListedByDate :: ReaderT Database (Action IO) [Post]
  36. postsListedByDate = do
  37. cursor <- find (select [] "posts") { sort = ["createdAt" =: (1 :: Int)] }
  38. collection <- rest cursor
  39. mapM documentToPost collection
Add Comment
Please, Sign In to add comment