{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts           #-}

module Json
  ( jsonObject
  , jsonId
  ) where

import Web.Scotty

import qualified Data.Aeson.Types as Json
import qualified Data.HashMap.Strict as M
import Data.Text (Text)

import Database.Persist
import Database.Persist.Sqlite

import Model.Database

jsonObject :: [(Text, Json.Value)] -> ActionM ()
jsonObject = json . Json.Object . M.fromList

jsonId :: (ToBackendKey SqlBackend a) => Key a -> ActionM ()
jsonId key = json . Json.Object . M.fromList $ [("id", Json.Number . fromIntegral . keyToInt64 $ key)]