mirror of
https://github.com/qfpl/applied-fp-course.git
synced 2024-11-30 12:23:25 +03:00
More comments in DB.hs
Some clean up to replace use of `either` when `first` would do.
This commit is contained in:
parent
bb6572f434
commit
3a6ebcb01d
@ -89,6 +89,11 @@ runDb
|
|||||||
-> AppM a
|
-> AppM a
|
||||||
runDb a = do
|
runDb a = do
|
||||||
db <- asks ( dbConn . envDb )
|
db <- asks ( dbConn . envDb )
|
||||||
|
-- We use the liftDb function to take the IO (Either SQLiteResponse a) and
|
||||||
|
-- convert it into an `m (Either Error a)` so that it matches the requirements
|
||||||
|
-- to be in our AppM, we then lean on the ExceptT functionality and use our
|
||||||
|
-- helper to either `throwError` with any DB errors that have made it this far
|
||||||
|
-- or simply return the desired value.
|
||||||
liftDb db >>= throwL
|
liftDb db >>= throwL
|
||||||
-- Or alternatively, if you hate variables...
|
-- Or alternatively, if you hate variables...
|
||||||
-- asks (dbConn.envDb) >>= ( liftDb >=> throwL )
|
-- asks (dbConn.envDb) >>= ( liftDb >=> throwL )
|
||||||
|
@ -6,12 +6,13 @@ module FirstApp.Main
|
|||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Monad.Except (ExceptT (ExceptT),
|
import Control.Monad.Except (ExceptT (ExceptT),
|
||||||
runExceptT, withExceptT)
|
runExceptT)
|
||||||
import Control.Monad.IO.Class (liftIO)
|
import Control.Monad.IO.Class (liftIO)
|
||||||
|
|
||||||
import Network.Wai
|
import Network.Wai
|
||||||
import Network.Wai.Handler.Warp (run)
|
import Network.Wai.Handler.Warp (run)
|
||||||
|
|
||||||
|
import Data.Bifunctor (first)
|
||||||
import Data.Either (Either (..), either)
|
import Data.Either (Either (..), either)
|
||||||
|
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
@ -68,8 +69,7 @@ prepareAppReqs = runExceptT $ do
|
|||||||
pure $ Env cfg db
|
pure $ Env cfg db
|
||||||
where
|
where
|
||||||
toStartUpErr e =
|
toStartUpErr e =
|
||||||
withExceptT e -- apply our error constructor to unify the error types to StartUpError
|
ExceptT . fmap (first e)
|
||||||
. ExceptT -- convert our function to an ExceptT with the constructor
|
|
||||||
|
|
||||||
-- Take our possibly failing configuration/db functions with their unique
|
-- Take our possibly failing configuration/db functions with their unique
|
||||||
-- error types and turn them into a consistently typed ExceptT. We can then
|
-- error types and turn them into a consistently typed ExceptT. We can then
|
||||||
@ -134,6 +134,7 @@ mkAddRequest
|
|||||||
-> Either Error RqType
|
-> Either Error RqType
|
||||||
mkAddRequest ti c = AddRq
|
mkAddRequest ti c = AddRq
|
||||||
<$> mkTopic ti
|
<$> mkTopic ti
|
||||||
|
-- Got string types...
|
||||||
<*> (mkCommentText . decodeUtf8 $ LBS.toStrict c)
|
<*> (mkCommentText . decodeUtf8 $ LBS.toStrict c)
|
||||||
|
|
||||||
mkViewRequest
|
mkViewRequest
|
||||||
|
Loading…
Reference in New Issue
Block a user