Last active
June 23, 2016 14:55
-
-
Save rtpg/480c020c31cc2722fbf81348616b7001 to your computer and use it in GitHub Desktop.
Example of useful effects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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