Skip to content

Instantly share code, notes, and snippets.

@manuelbernhardt
Created June 2, 2015 16:35
Show Gist options
  • Save manuelbernhardt/55db0b2b7f997d69368a to your computer and use it in GitHub Desktop.
Save manuelbernhardt/55db0b2b7f997d69368a to your computer and use it in GitHub Desktop.
CQRS & jOOQ example in Scala
package actors
import java.sql.Timestamp
import akka.actor.{Actor, ActorLogging}
import helpers.Database
import generated.Tables._
import org.jooq.impl.DSL._
class CQRSEventHandler extends Actor with ActorLogging {
override def preStart(): Unit = {
context.system.eventStream.subscribe(self, classOf[Event])
}
def receive = {
case UserRegistered(phoneNumber, userName, timestamp) =>
Database.withTransaction { sql =>
sql.insertInto(TWITTER_USER)
.columns(TWITTER_USER.CREATED_ON, TWITTER_USER.PHONE_NUMBER, TWITTER_USER.TWITTER_USER_NAME)
.values(new Timestamp(timestamp.getMillis), phoneNumber, userName)
.execute()
}
case ClientEvent(phoneNumber, userName, MentionsSubscribed(timestamp), _) =>
Database.withTransaction { sql =>
sql.insertInto(MENTION_SUBSCRIPTIONS)
.columns(MENTION_SUBSCRIPTIONS.USER_ID, MENTION_SUBSCRIPTIONS.CREATED_ON)
.select(
select(TWITTER_USER.ID, value(new Timestamp(timestamp.getMillis)))
.from(TWITTER_USER)
.where(
TWITTER_USER.PHONE_NUMBER.equal(phoneNumber)
.and(
TWITTER_USER.TWITTER_USER_NAME.equal(userName)
)
)
).execute()
}
case ClientEvent(phoneNumber, userName, MentionReceived(id, created_on, from, text, timestamp), _) =>
Database.withTransaction { sql =>
sql.insertInto(MENTIONS)
.columns(
MENTIONS.USER_ID,
MENTIONS.CREATED_ON,
MENTIONS.TWEET_ID,
MENTIONS.AUTHOR_USER_NAME,
MENTIONS.TEXT
)
.select(
select(
TWITTER_USER.ID,
value(new Timestamp(timestamp.getMillis)),
value(id),
value(from),
value(text)
)
.from(TWITTER_USER)
.where(
TWITTER_USER.PHONE_NUMBER.equal(phoneNumber)
.and(
TWITTER_USER.TWITTER_USER_NAME.equal(userName)
)
)
).execute()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment