I'm trying to build a simple blog using Haskell and the Framework Scotty. Using a Model.hs I have:
data Post = Post
{ id :: Int
, tipo :: String
, titulo :: String
, conteudo :: String
} deriving (Show, Generic)
I've already create a schema using sqlite and populate with some data, right now I'm trying to get this data using this method in my Storage.hs
selectPosts :: Sql.Connection -> IO [M.Post]
selectPosts conn =
Sql.query_ conn "select * from post" :: IO [M.Post]
My intent is get the data format as json in my Main.hs:
instance ToJSON M.Post
instance FromJSON M.Post
main :: IO ()
main = do
putStrLn "Starting Server..."
scotty 3000 $ do
get "/" $ file "templates/index.html"
get "/posts" $ do
json posts where
posts = withTestConnection $ \conn -> do
S.selectPosts conn
But I'm getting an IO [Model Post] and I don't know how to render this as json, so its keeping getting this error:
No instance for (ToJSON (IO [Post])) arising from a use of ‘json’
My project is in github to run just use stack build and after stack ghci. In the building I'm already getting this error.