You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package com.example.user.slick.dbios
  2. import slick.jdbc.JdbcProfile
  3. import com.example.models._
  4. import io.scalaland.chimney.dsl._
  5. import com.example.user.slick.Tables
  6. import javax.inject.Singleton
  7. // import slick.jdbc.H2Profile.api._
  8. // import scala.concurrent.ExecutionContext
  9. @Singleton
  10. class SlickLibraryDbio extends Tables {
  11. override val profile: JdbcProfile = _root_.slick.jdbc.H2Profile
  12. import profile.api._
  13. def findBookById(id: Long): DBIO[Option[BooksRow]] =
  14. Query.bookById(id).result.headOption
  15. def findBookById2(id: Long): DBIO[Option[BookWithoutId]] =
  16. Query.test(id).result.headOption
  17. def findBooksWithAuthor: DBIO[Seq[(BooksRow, AuthorsRow)]] =
  18. Query.booksWithAuthor.result
  19. def insertBook(book: Book): DBIO[BooksRow] =
  20. Query.writeBooks += bookToRow(book)
  21. def insertBook2(book: NewBook): DBIO[Long] =
  22. Query.writeBooks3 += book
  23. //
  24. def insertAuthor(author: Author): DBIO[AuthorsRow] =
  25. Query.writeAuthors += authorToRow(author)
  26. def insertAuthor2(author: NewAuthor): DBIO[Long] =
  27. Query.writeAuthors2 += author
  28. def getAuthorsForBook(bookId: Long): DBIO[Seq[(AuthorsRow, BooksRow)]] = {
  29. Query.authorOfBook(bookId).result
  30. }
  31. def authorToRow(author: Author) = author.transformInto[AuthorsRow]
  32. def bookToRow(book: Book) = book.transformInto[BooksRow]
  33. def authorsRowToAuthor(author: AuthorsRow) = author.transformInto[Author]
  34. def booksRowToBooks(book: BooksRow) = book.transformInto[Book]
  35. def booksRowToBooks2(book: BooksRow) = book.transformInto[BookWithoutId]
  36. // As mentioned under #2, we do encapsulate our queries
  37. object Query {
  38. // Return the book / author with it's auto incremented
  39. // id instead of an insert count
  40. lazy val writeBooks = Books returning Books
  41. .map(_.id) into ((book, id) => book.copy(id))
  42. lazy val writeBooks2 =
  43. Books.map(b => (b.title, b.authorId).mapTo[BookWithoutId])
  44. lazy val writeBooks3 = Books
  45. .map(b => (b.title, b.authorId).mapTo[NewBook])
  46. .returning(Books.map(_.id))
  47. lazy val writeAuthors = Authors returning Authors
  48. .map(_.id) into ((author, id) => author.copy(id))
  49. lazy val writeAuthors2 =
  50. Authors.map(a => (a.name).mapTo[NewAuthor]) returning Authors.map(_.id)
  51. lazy val test = (givenId: Long) =>
  52. Books
  53. .filter(_.id === givenId)
  54. .map(toBooksWithoutID)
  55. lazy val bookById = Books.findBy(_.id)
  56. lazy val toBooksWithoutID = (table: Books) =>
  57. (table.title, table.authorId).mapTo[BookWithoutId]
  58. lazy val booksWithAuthor = for {
  59. b <- Books
  60. a <- Authors if b.authorId === a.id
  61. } yield (b, a)
  62. lazy val authorOfBook = (bookId: Long) =>
  63. for {
  64. (author, book) <- Authors join
  65. Books on (_.id === _.authorId) filter {
  66. case (authors, books) => books.id === bookId
  67. }
  68. } yield (author, book)
  69. lazy val authorOfBook2 = (bookId: Long) =>
  70. for {
  71. (authorId, book) <- Books
  72. .filter(_.id === bookId)
  73. .take(1)
  74. .map(b => (b.authorId, b))
  75. author <- Authors filter (_.id === authorId)
  76. } yield (author, book)
  77. lazy val authorOfBook3 = (bookId: Long) =>
  78. for {
  79. (author, book) <- Authors join
  80. Books on (_.id === _.authorId) filter {
  81. case (authors, books) => books.id === bookId
  82. } map {
  83. case (authors, books) =>
  84. (authors.id, authors.name).mapTo[Author] -> (
  85. books.title,
  86. books.authorId,
  87. books.createdAt
  88. ).mapTo[BookDTO]
  89. }
  90. } yield (author, book)
  91. lazy val authorOfBook4 = (bookId: Long) =>
  92. for {
  93. book <- Books
  94. author <- book.authorsFk
  95. } yield (author, book)
  96. }
  97. case class BookWithoutId(title: String, authorId: Long)
  98. // def test() = {
  99. // val maybeBook = findBookById(1)
  100. // val x = maybeBook.map(_.map(_.title))
  101. // db.run(x)
  102. // }
  103. }