Skip to content

Instantly share code, notes, and snippets.

@rtpg
Last active June 23, 2016 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtpg/480c020c31cc2722fbf81348616b7001 to your computer and use it in GitHub Desktop.
Save rtpg/480c020c31cc2722fbf81348616b7001 to your computer and use it in GitHub Desktop.
Example of useful effects
"use strict"
// module App.Models
exports.createUser = function(u){
// you might want to put some real implementations here
return 3;
};
exports.lookupUser = function(u){
return 3;
};
module App.Models where
import Data.Maybe
import Control.Monad.Eff (Eff)
foreign import data DB :: !
type User = { username :: String, email :: String, uid :: String}
foreign import createUser :: forall e. User -> Eff (write::DB | e) User
foreign import lookupUser :: forall e. String -> Eff (read::DB | e) (Maybe User)
module App.Views where
import App.Models (DB, createUser, lookupUser)
import Control.Monad.Eff (Eff)
import Data.Maybe (Maybe(Nothing, Just))
import Prelude (return, bind, (++))
data Method = GET | POST
type Request = {
body :: String,
header :: String,
method :: Method
}
type Response = {
body :: String,
status :: Int
}
signupPage :: forall e. Request -> Eff (read::DB, write::DB | e) Response
signupPage req = do
mUser <- lookupUser req.header
case mUser of
Just user -> return {
body : "User with this username already exists!",
status : 400
}
Nothing -> do
createUser {
username : req.header,
email: req.body,
uid: "999" -- TODO make more unique
}
return {
body : "Created User with name " ++ req.header,
status: 200
}
lookupEmail :: forall e. Request -> Eff (read::DB | e) Response
lookupEmail req = do
mUser <- lookupUser req.header
case mUser of
Just u -> return {
body: u.email,
status: 200
}
Nothing -> do
return { status: 400, body: "Not Found"}
data Route =
GETroute String (forall e. Request -> Eff (read::DB | e) Response)
| POSTroute String (forall e. Request -> Eff (read::DB,write::DB | e) Response)
routes :: Array Route
routes = [
GETroute "/findEmail" lookupEmail,
POSTroute "/signup" signupPage
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment