Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
Minor refactor for DBService File System implementation (#44)
Browse files Browse the repository at this point in the history
This PR refactors DBService implementation using the file system.
- [x] Rename to `FileDBService` to homogenize for future implementations (i.e.: `PGDBService`).
- [x] Do not persist the protocol in `FileDBService`.
  • Loading branch information
JesusMtnez committed Feb 15, 2019
1 parent 5ef294d commit d6f38ec
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import cats.effect._
import cats.syntax.functor._
import fs2.Stream
import higherkindness.compendium.core.{CompendiumService, ProtocolUtils}
import higherkindness.compendium.db.{DBService, DBServiceStorage}
import higherkindness.compendium.db.{DBService, FileDBService}
import higherkindness.compendium.http.RootService
import higherkindness.compendium.models.CompendiumConfig
import higherkindness.compendium.storage.{FileStorage, Storage}
Expand All @@ -37,7 +37,7 @@ object CompendiumStreamApp {
for {
conf <- Stream.eval(Effect[F].delay(pureconfig.loadConfigOrThrow[CompendiumConfig]))
implicit0(storage: Storage[F]) = FileStorage.impl[F](conf.storage)
implicit0(dbService: DBService[F]) = DBServiceStorage.impl[F]
implicit0(dbService: DBService[F]) = FileDBService.impl[F]
implicit0(utils: ProtocolUtils[F]) = ProtocolUtils.impl[F]()
implicit0(compendiumService: CompendiumService[F]) = CompendiumService.impl[F]
service = RootService.rootRouteService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ import higherkindness.compendium.models.Protocol
import higherkindness.compendium.models.ProtocolAlreadyExists
import higherkindness.compendium.storage.Storage

object DBServiceStorage {
object FileDBService {

def impl[F[_]: Sync: Storage]: DBService[F] =
new DBService[F] {

override def addProtocol(id: String, protocol: Protocol): F[Unit] =
for {
exists <- Storage[F].checkIfExists(id)
_ <- exists.fold(
Sync[F].raiseError(new ProtocolAlreadyExists(s"Protocol with id ${id} already exists")),
Storage[F].store(id, protocol))
Sync[F].unit)
} yield ()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import higherkindness.compendium.storage.StorageStub
import org.specs2.ScalaCheck
import org.specs2.mutable.Specification

object DBServiceStorageSpec extends Specification with ScalaCheck {
object FileDBServiceSPec extends Specification with ScalaCheck {

sequential

Expand All @@ -32,15 +32,15 @@ object DBServiceStorageSpec extends Specification with ScalaCheck {
"If the protocol doesn't exists we store it" >> prop { id: String =>
implicit val storage = new StorageStub(Some(dummyProtocol), id)

DBServiceStorage.impl[IO].addProtocol(id, dummyProtocol).map(_ => success).unsafeRunSync()
FileDBService.impl[IO].addProtocol(id, dummyProtocol).map(_ => success).unsafeRunSync()
}

"If the protocol exists we raised an error" >> prop { id: String =>
implicit val storage = new StorageStub(Some(dummyProtocol), id) {
override def checkIfExists(id: String): IO[Boolean] = IO(true)
}

DBServiceStorage.impl[IO].addProtocol(id, dummyProtocol).unsafeRunSync must
FileDBService.impl[IO].addProtocol(id, dummyProtocol).unsafeRunSync must
throwA[ProtocolAlreadyExists]
}
}
Expand Down

0 comments on commit d6f38ec

Please sign in to comment.