Created
May 14, 2019 20:45
-
-
Save paulosuzart/0ec236e5d14a696a9ce6ebc14ec0847f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.pauljamescleary.petstore.domain.destination | |
import scala.language.higherKinds | |
trait DestinationRepositoryAlgebra[F[_]] { | |
def destinations(departureId: Long): F[List[Long]] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.pauljamescleary.petstore.domain | |
package destination | |
import scala.language.higherKinds | |
class DestinationService[F[_]](destinationRepo: DestinationRepositoryAlgebra[F]) { | |
def departingFrom(positionId: Long): F[List[Long]] = destinationRepo.destinations(positionId) | |
} | |
object DestinationService { | |
def apply[F[_]](destinationRepo: DestinationRepositoryAlgebra[F]): DestinationService[F] = | |
new DestinationService(destinationRepo) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.pauljamescleary.petstore.infrastructure.repository.inmemory | |
import cats.{Applicative} | |
import io.github.pauljamescleary.petstore.domain.destination.{DestinationRepositoryAlgebra, PopularDestination} | |
import cats.implicits._ | |
import scala.collection.mutable | |
class PopularDestinationRepositoryInMemoryInterpreter[F[_] : Applicative] extends DestinationRepositoryAlgebra[F] { | |
val database: mutable.Map[Long, PopularDestination] = mutable.HashMap( | |
1L -> PopularDestination(1L, List(2, 3)), | |
2L -> PopularDestination(2L, List(1, 3, 4)) | |
) | |
override def destinations(departureId: Long): F[List[Long]] = { | |
val result = database.get(departureId) match { | |
case Some(PopularDestination(_, l)) => l | |
case None => List.empty | |
} | |
result.pure[F] | |
} | |
} | |
object PopularDestinationRepositoryInMemoryInterpreter { | |
def apply[F[_]: Applicative]() = new PopularDestinationRepositoryInMemoryInterpreter[F]() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment