Skip to content

Commit

Permalink
Created guice modules for BookService and BookRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
VassilisSoum committed Feb 13, 2021
1 parent 0c783b6 commit 02e7180
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
14 changes: 5 additions & 9 deletions src/main/scala/com/bookworm/application/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.bookworm.application

import cats.effect.{ExitCode, IO, IOApp}
import com.bookworm.application.books.adapter.api.BookRestApi
import com.bookworm.application.books.adapter.repository.BookRepositoryImpl
import com.bookworm.application.books.adapter.repository.{BookRepositoryImpl, BookRepositoryModule}
import com.bookworm.application.books.adapter.repository.dao.BookDao
import com.bookworm.application.books.adapter.service.BookServiceImpl
import com.bookworm.application.books.adapter.service.{BookServiceImpl, BookServiceModule}
import com.bookworm.application.books.domain.port.inbound.BookService
import com.bookworm.application.books.domain.port.outbound.BookRepository
import com.bookworm.application.init.BookwormServer
Expand All @@ -26,7 +26,9 @@ object Main extends IOApp {
.createTransactor[IO]
.use { resources =>
val injector = Guice.createInjector(
new Module(resources._1)
new Module(resources._1),
new BookServiceModule[IO],
new BookRepositoryModule[IO]
)
val httpApp = Logger.httpApp(logHeaders = true, logBody = true)(
injector.getInstance(Key.get(scalaguice.typeLiteral[BookRestApi[IO]])).getBooks /*<+>*/ .orNotFound
Expand All @@ -50,12 +52,6 @@ object Main extends IOApp {
bind(new TypeLiteral[Sync[IO]] {}).toInstance(implicitly[Sync[IO]])
bind(classOf[BookDao]).in(Scopes.SINGLETON)
bind(new TypeLiteral[Transactor[IO]] {}).toInstance(transactor)
bind(new TypeLiteral[BookRepository[IO]]() {})
.to(new TypeLiteral[BookRepositoryImpl[IO]]() {})
.in(Scopes.SINGLETON)
bind(new TypeLiteral[BookService[IO]]() {})
.to(new TypeLiteral[BookServiceImpl[IO]]() {})
.in(Scopes.SINGLETON)
bind(new TypeLiteral[BookRestApi[IO]] {}).in(Scopes.SINGLETON)
bind(classOf[java.time.Clock]).toInstance(java.time.Clock.systemUTC())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import doobie.implicits._
import javax.inject.Inject
import scala.util.{Failure, Success, Try}

//TODO: Create guice module and make this class package private to use only trait
class BookRepositoryImpl[F[_]: Sync] @Inject() (bookDao: BookDao, transactor: Transactor[F]) extends BookRepository[F] {
private[repository] class BookRepositoryImpl[F[_]: Sync] @Inject() (bookDao: BookDao, transactor: Transactor[F])
extends BookRepository[F] {

override def getBooksForGenre(
genreId: GenreId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.bookworm.application.books.adapter.repository

import cats.effect.Sync
import com.bookworm.application.books.domain.port.outbound.BookRepository
import com.google.inject.{AbstractModule, Scopes, TypeLiteral}
import net.codingwell.scalaguice.ScalaModule

class BookRepositoryModule[F[_]: Sync] extends AbstractModule with ScalaModule {

override def configure(): Unit =
bind(new TypeLiteral[BookRepository[F]]() {})
.to(new TypeLiteral[BookRepositoryImpl[F]]() {})
.in(Scopes.SINGLETON)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import com.bookworm.application.books.domain.port.outbound.BookRepository

import javax.inject.Inject

class BookServiceImpl[F[_]: Sync] @Inject() (bookRepository: BookRepository[F]) extends BookService[F] {
private[service] class BookServiceImpl[F[_]: Sync] @Inject() (bookRepository: BookRepository[F])
extends BookService[F] {

def retrieveBooksByGenre(
genre: GenreId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.bookworm.application.books.adapter.service

import cats.effect.Sync
import com.bookworm.application.books.domain.port.inbound.BookService
import com.google.inject.{AbstractModule, Scopes, TypeLiteral}
import net.codingwell.scalaguice.ScalaModule

class BookServiceModule[F[_]: Sync] extends AbstractModule with ScalaModule {

override def configure(): Unit =
bind(new TypeLiteral[BookService[F]]() {})
.to(new TypeLiteral[BookServiceImpl[F]]() {})
.in(Scopes.SINGLETON)

}

0 comments on commit 02e7180

Please sign in to comment.