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.
 
 
 
 
 

97 lines
2.7 KiB

package com.example.user.slick.services
import javax.inject._
// import slick.jdbc.JdbcProfile
import slick.jdbc.JdbcBackend.Database
import com.example.models._
import com.example.user.slick.dbios.SlickLibraryDbio
import monix.eval.Task
import com.example.services.TaskLibraryService
@Singleton
class TaskSlickLibraryService @Inject() (
db: Database,
libraryDbio: SlickLibraryDbio
) extends TaskLibraryService {
import libraryDbio.profile.api._
override def findBookById(id: Long): Task[Option[Book]] =
Task
.deferFuture {
db.run(
libraryDbio.findBookById(id)
)
}
.flatMap(record => Task.eval(record.map(libraryDbio.booksRowToBooks)))
override def findBooksWithAuthor: Task[Seq[(Book, Author)]] =
Task.deferFuture(db.run(libraryDbio.findBooksWithAuthor)).flatMap {
records =>
Task.traverse(records) {
case (x, y) =>
Task.eval {
(
libraryDbio.booksRowToBooks(x),
libraryDbio.authorsRowToAuthor(y)
)
}
}
}
// override def storeMulti(aggregates: Seq[AggregateType]): Task[Long] =
// for {
// records <- Task.traverse(aggregates) { aggregate =>
// convertToRecord(aggregate)
// }
// result <- Task.deferFutureAction { implicit ec =>
// import profile.api._
// db.run(DBIO.sequence(records.foldLeft(Seq.empty[DBIO[Long]]) {
// case (result, record) =>
// result :+ dao.insertOrUpdate(record).map(_.toLong)
// }))
// .map(_.sum)
// }
// } yield result
override def insertBookAndAuthor(
book: NewBook,
author: NewAuthor
): Task[(Long, Long)] =
Task.deferFutureAction { implicit scheduler =>
val action = for {
authorId <- libraryDbio.insertAuthor2(author)
bookId <- libraryDbio.insertBook2(book.copy(authorId = authorId))
} yield (bookId, authorId)
db.run(action.transactionally)
}
override def getAuthorsForBook(id: Long): Task[Seq[(Author, Book)]] =
for {
records <- Task.deferFuture(db.run(libraryDbio.getAuthorsForBook((id))))
result <- Task.traverse(records) {
case (authorsRow, booksRow) =>
Task.eval(
(
libraryDbio.authorsRowToAuthor(authorsRow),
libraryDbio.booksRowToBooks(booksRow)
)
)
}
} yield (result)
// Task.deferFutureAction { implicit scheduler =>
// db.run(
// libraryDbio
// .getAuthorsForBook(id)
// .map(_.map {
// case (x, y) => {
// (
// libraryDbio.authorsRowToAuthor(x),
// libraryDbio.booksRowToBooks(y)
// )
// }
// })
// )
// }
}