Making complex apps a little bit easier

Handy is a set of classes that make writing apps in a functional style really quite easy.

How easy? Here’s an example. (We’ll make it even easier in a mo.)

val approval = request.approval
for (
  text <- Ref((request.body \ "text").asOpt[String]) orIfNone UserError("The message contained no text");
  entry <- refContentEntry(entryId);
  approved <- approval ask Permissions.CommentOnEntry(entry.itself);
  updated <- ContentEntry.addComment(entry, approval.who, text);
  j <- updated.toJsonFor(approval)
) yield j

In those few lines, we’ve:

And we’ve done it in an asynchronous non-blocking manner.

handy-appbase-core

If you’re using the Play Framework, then you can also use some code that’s in handy-play and handy-appbase-core.

This is the example above filled out to use handy-appbase-core to define a Play controller action

def commentOnEntry = DataAction.one(parse.json) { implicit request =>
  for (
    text <- Ref((request.body \ "text").asOpt[String]) orIfNone UserError("The message contained no text");
    entry <- refContentEntry(entryId);
    approved <- request.approval ask Permissions.CommentOnEntry(entry.itself);
    updated <- ContentEntry.addComment(entry, approval.who, text)
  ) yield updated
}

Here, we’ve defined a controller action that has some helpful propertes:

handy-reactivemongo

Many of my projects use ReactiveMongo as the database driver, so there’s also a couple of classes provided for that.