From 325f03a2c7b13993e5659287963d8b69605fbb04 Mon Sep 17 00:00:00 2001 From: nova Date: Wed, 3 Jun 2020 21:45:50 +0530 Subject: [PATCH] Added more join examples --- .../user/slick/dbios/SlickLibraryDbio.scala | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/modules/slick/src/main/scala/com/example/user/slick/dbios/SlickLibraryDbio.scala b/modules/slick/src/main/scala/com/example/user/slick/dbios/SlickLibraryDbio.scala index 7a684be..e66f64d 100644 --- a/modules/slick/src/main/scala/com/example/user/slick/dbios/SlickLibraryDbio.scala +++ b/modules/slick/src/main/scala/com/example/user/slick/dbios/SlickLibraryDbio.scala @@ -95,11 +95,27 @@ class SlickLibraryDbio extends Tables { author <- Authors filter (_.id === authorId) } yield (author, book) - // lazy val authorOfBook3 = (bookId: Long) => - // for { - // authorId <- bookById(bookId).map(_.map(_.authorId)) - // (authors) <- Authors filter (_.id === authorId) - // } yield (authors) + lazy val authorOfBook3 = (bookId: Long) => + for { + (author, book) <- Authors join + Books on (_.id === _.authorId) filter { + case (authors, books) => books.id === bookId + } map { + case (authors, books) => + (authors.id, authors.name).mapTo[Author] -> ( + books.title, + books.authorId, + books.createdAt + ).mapTo[BookDTO] + } + } yield (author, book) + + lazy val authorOfBook4 = (bookId: Long) => + for { + book <- Books + author <- book.authorsFk + } yield (author, book) + } case class BookWithoutId(title: String, authorId: Long) // def test() = {