Skip to content

Instantly share code, notes, and snippets.

@paulosuzart
Created May 14, 2019 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulosuzart/0ec236e5d14a696a9ce6ebc14ec0847f to your computer and use it in GitHub Desktop.
Save paulosuzart/0ec236e5d14a696a9ce6ebc14ec0847f to your computer and use it in GitHub Desktop.
package io.github.pauljamescleary.petstore.domain.destination
import scala.language.higherKinds
trait DestinationRepositoryAlgebra[F[_]] {
def destinations(departureId: Long): F[List[Long]]
}
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)
}
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