Skip to content

Instantly share code, notes, and snippets.

@ctataryn
Created September 13, 2016 01:44
Show Gist options
  • Save ctataryn/7d1a903c2f352cf492606b68af6503c5 to your computer and use it in GitHub Desktop.
Save ctataryn/7d1a903c2f352cf492606b68af6503c5 to your computer and use it in GitHub Desktop.
def getContentsByUser(userId: UUID, adopted: Boolean = false): Either[UserNotFoundError, List[Content]] = {
getDatabase withDynSession {
val subQuery =
adopted match {
case false => {
for {
uc <- UserContentTable.query if uc.userId === userId.toString && (!uc.adopted.isDefined || !uc.adopted)
c <- ContentTable.query if uc.contentId === c.id
}
yield (uc, c)
}
case true => {
for {
uc <- UserContentTable.query if uc.userId === userId.toString && (uc.adopted.isDefined && uc.adopted)
c <- ContentTable.query if uc.contentId === c.id
}
yield (uc, c)
}
}
val query =
UserTable.query.filter(_.id === userId.toString)
.leftJoin(subQuery).on { case (u, (uc, c)) => u.id === uc.userId}
.map { case (u, (uc, c)) => (u, c.optionProjection) }
query.list match {
case Nil => Left(UserNotFoundError(userId))
case cs => Right(cs.map { case (u, c) => c }.flatten )
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment