Last active
November 3, 2019 19:59
-
-
Save tdantas/1daa70b780d80e51e86416eb7dcf95ac to your computer and use it in GitHub Desktop.
Port simple JS function to Clojure ( make it simple to test and isolating side-effects )
This file contains 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
/* | |
Goal: | |
1. create user database | |
2. send confirmation email | |
3. commit or rollback transaction | |
- 1 and 2 must be somehow transactional | |
( if sendgrid rejects the email "http response status 400 - 500 range", you must rollback the transaction ) | |
Context: | |
express handler is calling the UserCreation function passing the http body as argument | |
(we are assuming here the schema validation was made at handler level ) | |
app.post('/users', function (req, res) { | |
user = userService.userCreation(req.body) ; | |
res.send(user) | |
}) | |
*/ | |
const db = require('./db') | |
const email = require('./email') | |
const bugTracker = require('./bugTracker') | |
/* user schema | |
{name: 'name', | |
email: 'user@email.com' | |
password: 'secret'} | |
*/ | |
function userCreation(user) { | |
try { | |
const tx = db.beginTransaction(); | |
user.password = bcryptPassword(user.password) | |
const userSaved = db.insert(tx, user); | |
email.sendConfirmation(userSaved.email); | |
tx.commit(); | |
} catch (exception) { | |
tx.rollback(); | |
bugTracker.notify("userCreation", e.message) | |
throw UserCreationFailed(e.message) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
;; using compojure + components
;; no protocols
compojure handler
routes.clj
user-service.clj