Skip to content

Instantly share code, notes, and snippets.

@eckyputrady
Last active April 2, 2023 12:07
Show Gist options
  • Save eckyputrady/4cfa4eb0eb8370cb50c7ffaa92d33e2c to your computer and use it in GitHub Desktop.
Save eckyputrady/4cfa4eb0eb8370cb50c7ffaa92d33e2c to your computer and use it in GitHub Desktop.

Revisions

  1. eckyputrady revised this gist Oct 2, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Pseudo.hs
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,10 @@ type UserId = Text
    type User = Text

    class (Monad m) => UserRepo m where
    getUserById :: UserId -> User
    getUserById :: UserId -> m User

    class (Monad m) => SessionRepo m where
    getUserIdBySession :: SessionId -> UserId
    getUserIdBySession :: SessionId -> m UserId

    getUser :: (UserRepo m, SessionRepo m)
    => SessionId -> m User
  2. eckyputrady revised this gist Oct 2, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Pseudo.hs
    Original file line number Diff line number Diff line change
    @@ -37,11 +37,11 @@ acquireConnection :: IO Connection
    acquireConnection = ...

    getUserById :: (Reader Connection m)
    => Domain.UserId -> Domain.User
    => Domain.UserId -> m Domain.User
    getUserById = ...

    getUserIdBySession :: (Reader Connection m)
    => Domain.Session -> Domain.UserId
    => Domain.Session -> m Domain.UserId
    getUserIdBySession = ...

    -----------------------------
  3. eckyputrady created this gist Oct 2, 2017.
    68 changes: 68 additions & 0 deletions Pseudo.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    -----------------------------
    -- Domain.hs

    type SessionId = Text
    type UserId = Text
    type User = Text

    class (Monad m) => UserRepo m where
    getUserById :: UserId -> User

    class (Monad m) => SessionRepo m where
    getUserIdBySession :: SessionId -> UserId

    getUser :: (UserRepo m, SessionRepo m)
    => SessionId -> m User
    getUser sId = getUserIdBySession sId
    >>= getUserById

    -----------------------------
    -- Routes.hs

    import qualified Domain

    routes :: (Domain.UserRepo m, Domain.SessionRepo m) => m ()
    routes =
    get "/user" $ do
    sId <- parseSessionFromCookiesSomehow
    user <- Domain.getUser sId
    displayUserSomeHow user

    -----------------------------
    -- Redis.hs

    import qualified Domain

    acquireConnection :: IO Connection
    acquireConnection = ...

    getUserById :: (Reader Connection m)
    => Domain.UserId -> Domain.User
    getUserById = ...

    getUserIdBySession :: (Reader Connection m)
    => Domain.Session -> Domain.UserId
    getUserIdBySession = ...

    -----------------------------
    -- Main.hs

    import qualified Domain
    import qualified Redis
    import qualified Routes

    newtype App a = App
    { unApp :: ReaderT Connection IO a
    } deriving ( Applicative, Functor, Monad
    , MonadReader Connection, MonadIO
    )

    instance Domain.UserRepo App where
    getUserById = Redis.getUserById

    instance Domain.SessionRepo App where
    getUserIdBySession = Redis.getUserIdBySession

    main = do
    conn <- Redis.acquireConnection
    flip runReaderT conn . unApp $ Routes.routes