Created
September 13, 2016 01:44
-
-
Save ctataryn/7d1a903c2f352cf492606b68af6503c5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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