Skip to content

Instantly share code, notes, and snippets.

@Akii
Last active February 14, 2016 16:24
Show Gist options
  • Save Akii/2d74060fa1f14f5a7d52 to your computer and use it in GitHub Desktop.
Save Akii/2d74060fa1f14f5a7d52 to your computer and use it in GitHub Desktop.
def initiateRegistration(userName: UserName, emailAddress: EmailAddress): Reader[RegistrationRepository, Future[Registration]] = Reader { repo =>
val exists: OptionT[Future, (Registration, Registration)] = for {
e <- repo.findByUserName(userName)
u <- repo.findByEmailAddress(emailAddress)
} yield (e, u)
exists.run.flatMap {
case None =>
val hash = Random.alphanumeric take 32 mkString ""
val reg = Registration(UUID.randomUUID(), userName, emailAddress, hash, DateTime.now())
repo.add(reg).map(_ => reg)
case _ =>
Future.failed(new Exception("Thing already exists."))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment