{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}

module Foundation where

import Data.Text (Text)
import Data.Time (UTCTime)
import Database.Persist.Quasi (lowerCaseSettings)
import Database.Persist.Sql (ConnectionPool, SqlBackend, runSqlPool)
import Database.Persist.TH
  ( mkMigrate
  , mkPersist
  , persistFileWith
  , share
  , sqlSettings
  )
import Yesod
  ( Yesod
  , YesodPersist(YesodPersistBackend, runDB)
  , getYesod
  , mkYesodData
  , parseRoutesFile
  , renderRoute
  )

share
  [mkPersist sqlSettings, mkMigrate "migrateAll"]
  $(persistFileWith lowerCaseSettings "config/models.persistentmodels")

data App =
  App
    { App -> ConnectionPool
connectionPool :: ConnectionPool
    }

mkYesodData "App" $(parseRoutesFile "config/routes.yesodroutes")

instance Yesod App

instance YesodPersist App where
  type YesodPersistBackend App = SqlBackend
  runDB :: forall a. YesodDB App a -> HandlerFor App a
runDB YesodDB App a
action = forall (m :: * -> *). MonadHandler m => m (HandlerSite m)
getYesod forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall backend (m :: * -> *) a.
(MonadUnliftIO m, BackendCompatible SqlBackend backend) =>
ReaderT backend m a -> Pool backend -> m a
runSqlPool YesodDB App a
action forall b c a. (b -> c) -> (a -> b) -> a -> c
. App -> ConnectionPool
connectionPool