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.

105 lines
3.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package wow.doge.http4sdemo.dto
  2. import java.time.LocalDateTime
  3. import cats.syntax.either._
  4. import enumeratum.EnumEntry
  5. import enumeratum._
  6. import io.circe.generic.semiauto._
  7. import io.scalaland.chimney.dsl._
  8. import org.http4s.ParseFailure
  9. import org.http4s.QueryParamDecoder
  10. import org.http4s.dsl.impl.QueryParamDecoderMatcher
  11. import slick.jdbc.JdbcProfile
  12. import wow.doge.http4sdemo.slickcodegen.Tables
  13. final case class Book(
  14. bookId: Int,
  15. bookTitle: String,
  16. isbn: String,
  17. authorId: Int,
  18. createdAt: LocalDateTime
  19. )
  20. object Book {
  21. def tupled = (apply _).tupled
  22. implicit val codec = deriveCodec[Book]
  23. def fromBooksRow(row: Tables.BooksRow) = row.transformInto[Book]
  24. def fromBooksTableFn(implicit profile: JdbcProfile) = {
  25. import profile.api._
  26. (b: Tables.Books) =>
  27. (b.bookId, b.bookTitle, b.isbn, b.authorId, b.createdAt).mapTo[Book]
  28. }
  29. def fromBooksTable(implicit profile: JdbcProfile) =
  30. Tables.Books.map(fromBooksTableFn)
  31. }
  32. final case class NewBook(bookTitle: String, isbn: String, authorId: Int)
  33. object NewBook {
  34. def tupled = (apply _).tupled
  35. implicit val decoder = deriveDecoder[NewBook]
  36. def fromBooksTable(implicit profile: JdbcProfile) = {
  37. import profile.api._
  38. Tables.Books.map(b => (b.bookTitle, b.isbn, b.authorId).mapTo[NewBook])
  39. }
  40. }
  41. final case class BookUpdate(title: Option[String], authorId: Option[Int]) {
  42. import com.softwaremill.quicklens._
  43. def update(row: Tables.BooksRow): Tables.BooksRow =
  44. row
  45. .modify(_.bookTitle)
  46. .setToIfDefined(title)
  47. .modify(_.authorId)
  48. .setToIfDefined(authorId)
  49. }
  50. object BookUpdate {
  51. implicit val codec = deriveCodec[BookUpdate]
  52. }
  53. final case class Author(authorId: Int, authorName: String)
  54. object Author {
  55. def tupled = (apply _).tupled
  56. implicit val codec = deriveCodec[Author]
  57. def fromAuthorsRow(row: Tables.AuthorsRow) = row.transformInto[Author]
  58. def fromAuthorsTableFn(implicit profile: JdbcProfile) = {
  59. import profile.api._
  60. (a: Tables.Authors) => (a.authorId, a.authorName).mapTo[Author]
  61. }
  62. }
  63. final case class NewAuthor(name: String)
  64. object NewAuthor {
  65. // def fromAuthorsTable(implicit profile: JdbcProfile) = {
  66. // import profile.api._
  67. // Tables.Authors.map(a => (a.authorName).mapTo[NewAuthor])
  68. // }
  69. }
  70. final case class BookWithAuthor(
  71. id: Int,
  72. title: String,
  73. isbn: String,
  74. author: Author,
  75. createdAt: LocalDateTime
  76. )
  77. object BookWithAuthor {
  78. def tupled = (apply _).tupled
  79. implicit val codec = deriveCodec[BookWithAuthor]
  80. }
  81. sealed trait BookSearchMode extends EnumEntry
  82. object BookSearchMode extends Enum[BookSearchMode] {
  83. val values = findValues
  84. case object BookTitle extends BookSearchMode
  85. case object AuthorName extends BookSearchMode
  86. implicit val yearQueryParamDecoder: QueryParamDecoder[BookSearchMode] =
  87. QueryParamDecoder[String].emap(s =>
  88. withNameEither(s).leftMap(e => ParseFailure(e.getMessage, e.getMessage))
  89. )
  90. object Matcher extends QueryParamDecoderMatcher[BookSearchMode]("mode")
  91. }