Skip to content

Commit

Permalink
bump versions + reactivemongo fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
oen9 committed Nov 23, 2020
1 parent 66099cd commit 2db1aa4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.idea/
.bloop/
.metals/
.bsp/
target/
cache/
sess.vim
Expand Down
22 changes: 11 additions & 11 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
scalaVersion := "2.13.3"

val Ver = new {
val http4s = "0.21.7"
val http4s = "0.21.11"
val slinky = "0.6.6"
val logback = "1.2.3"
val zio = "1.0.1"
val zio = "1.0.3"
val circe = "0.13.0"
val tapir = "0.16.10"
val doobie = "0.9.0"
val caliban = "0.9.2"
val doobie = "0.9.2"
val caliban = "0.9.3"
}

lazy val sharedSettings = Seq(
Expand All @@ -22,8 +22,8 @@ lazy val sharedSettings = Seq(
"io.circe" %%% "circe-generic-extras" % Ver.circe,
"io.circe" %%% "circe-generic" % Ver.circe,
"io.circe" %%% "circe-literal" % Ver.circe,
"com.softwaremill.quicklens" %%% "quicklens" % "1.6.0",
"io.scalaland" %%% "chimney" % "0.5.2"
"com.softwaremill.quicklens" %%% "quicklens" % "1.6.1",
"io.scalaland" %%% "chimney" % "0.6.1"
),
scalacOptions ++= Seq(
"-Xlint",
Expand Down Expand Up @@ -68,8 +68,8 @@ lazy val jsSettings = Seq(
lazy val jvmSettings = Seq(
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % Ver.zio,
"dev.zio" %% "zio-interop-cats" % "2.1.4.0",
"dev.zio" %% "zio-logging-slf4j" % "0.4.0",
"dev.zio" %% "zio-interop-cats" % "2.2.0.1",
"dev.zio" %% "zio-logging-slf4j" % "0.5.3",

"org.http4s" %% "http4s-blaze-server" % Ver.http4s,
"org.http4s" %% "http4s-circe" % Ver.http4s,
Expand All @@ -84,10 +84,10 @@ lazy val jvmSettings = Seq(
"com.softwaremill.sttp.tapir" %% "tapir-openapi-circe-yaml" % Ver.tapir,
"com.softwaremill.sttp.tapir" %% "tapir-json-circe" % Ver.tapir,
"ch.qos.logback" % "logback-classic" % Ver.logback,
"com.github.pureconfig" %% "pureconfig" % "0.13.0",
"com.github.pureconfig" %% "pureconfig" % "0.14.0",

"org.reactivemongo" %% "reactivemongo" % "0.20.3",
"org.flywaydb" % "flyway-core" % "6.3.3",
"org.reactivemongo" %% "reactivemongo" % "1.0.1",
"org.flywaydb" % "flyway-core" % "7.2.1",
"org.postgresql" % "postgresql" % "42.2.12",
"org.tpolecat" %% "doobie-core" % Ver.doobie,
"org.tpolecat" %% "doobie-h2" % Ver.doobie,
Expand Down
6 changes: 3 additions & 3 deletions jvm/src/main/scala/example/modules/db/MongoConn.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package example.modules.db
import example.modules.appConfig.AppConfig
import example.modules.AppConfigData
import reactivemongo.api.AsyncDriver
import reactivemongo.api.DefaultDB
import reactivemongo.api.DB
import reactivemongo.api.FailoverStrategy
import reactivemongo.api.MongoConnection
import scala.concurrent.duration._
import zio._

object MongoConn {
case class MongoConn(conn: MongoConnection, defaultDb: DefaultDB)
case class MongoConn(conn: MongoConnection, defaultDb: DB)

def createDriver() = ZIO.effect(new AsyncDriver)
def makeConnection(mongoUri: String) =
Expand All @@ -19,7 +19,7 @@ object MongoConn {
mongoConnection <- ZIO.fromFuture(_ => driver.connect(mongoUri))
} yield mongoConnection

def loadDefaultDb(mongoConnection: MongoConnection, appConfigData: AppConfigData): ZIO[Any, Throwable, DefaultDB] =
def loadDefaultDb(mongoConnection: MongoConnection, appConfigData: AppConfigData): ZIO[Any, Throwable, DB] =
for {
uri <- ZIO.fromFuture(implicit ec => MongoConnection.fromString(appConfigData.mongo.uri))
dbName <- ZIO.fromOption(uri.db).mapError(_ => new Exception("Can't read default db name"))
Expand Down
40 changes: 18 additions & 22 deletions jvm/src/main/scala/example/modules/db/TodoRepository.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package example.modules.db

import cats.implicits._
import example.model.Errors.TodoTaskNotFound
import example.model.MongoData._
import example.modules.db.MongoConn.MongoConn
import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.BSONObjectID
import reactivemongo.api.bson.collection.BSONCollection
import reactivemongo.api.commands.WriteResult
import reactivemongo.api.Cursor
import zio._
import zio.logging.Logger
Expand All @@ -18,10 +18,10 @@ object todoRepository {
object TodoRepository {
trait Service {
def getAll: Task[List[TodoTask]]
def insert(todoTask: TodoTask): Task[WriteResult]
def insert(todoTask: TodoTask): Task[Option[Int]]
def findById(id: BSONObjectID): Task[TodoTask]
def updateStatus(id: BSONObjectID, newStatus: TodoStatus): Task[WriteResult]
def deleteById(id: BSONObjectID): Task[WriteResult]
def updateStatus(id: BSONObjectID, newStatus: TodoStatus): Task[Option[Int]]
def deleteById(id: BSONObjectID): Task[Option[Int]]
}

def createNotFoundMsg(id: BSONObjectID) = s"TodoTask '${id.stringify}' not found"
Expand All @@ -37,8 +37,8 @@ object todoRepository {
.collect[List](-1, Cursor.FailOnError[List[TodoTask]]())
}

def insert(todoTask: TodoTask): Task[WriteResult] = ZIO.fromFuture { implicit ec =>
collection.insert.one(todoTask)
def insert(todoTask: TodoTask): Task[Option[Int]] = ZIO.fromFuture { implicit ec =>
collection.insert.one(todoTask).map(_.code)
}

def findById(id: BSONObjectID): Task[TodoTask] = {
Expand All @@ -53,41 +53,37 @@ object todoRepository {
} yield found
}

def updateStatus(id: BSONObjectID, newStatus: TodoStatus): Task[WriteResult] = {
def updateStatus(id: BSONObjectID, newStatus: TodoStatus): Task[Option[Int]] = {
val query = BSONDocument("_id" -> id)
val update = BSONDocument("$set" -> BSONDocument("status" -> newStatus))
for {
updateResult <- ZIO.fromFuture(implicit ec => collection.update.one(q = query, u = update))
_ <- logger.log(LogLevel.Trace)(s"updateStatus '$id' to $newStatus: $updateResult")
} yield updateResult
} yield updateResult.code
}

def deleteById(id: BSONObjectID): Task[WriteResult] = {
def deleteById(id: BSONObjectID): Task[Option[Int]] = {
val query = BSONDocument("_id" -> id)
for {
removeResult <- ZIO.fromFuture(implicit ec => collection.delete.one(query))
_ <- logger.log(LogLevel.Trace)(s"deleteById '$id' delete: $removeResult")
} yield removeResult
} yield removeResult.code
}
}
)

def test(initData: Vector[TodoTask] = Vector()) = ZLayer.fromEffect {
import com.softwaremill.quicklens._
import reactivemongo.api.commands.DefaultWriteResult
import reactivemongo.api.commands.UpdateWriteResult

//If this gets removed we have to change results in Service.
val defaultResult = ZIO.succeed(DefaultWriteResult(true, 1, Seq(), None, None, None))
val updateResult = ZIO.succeed(UpdateWriteResult(true, 1, 1, Seq(), Seq(), None, None, None))
val defaultResult = ZIO.succeed(none[Int])

for {
ref <- Ref.make(initData)
} yield new Service {
def getAll: Task[List[TodoTask]] =
ref.get.map(_.toList)

def insert(todoTask: TodoTask): Task[WriteResult] =
def insert(todoTask: TodoTask): Task[Option[Int]] =
ref.update(_ :+ todoTask) *> defaultResult

def findById(id: BSONObjectID): Task[TodoTask] =
Expand All @@ -96,26 +92,26 @@ object todoRepository {
found <- ZIO.fromOption(maybeFound).mapError(_ => TodoTaskNotFound(createNotFoundMsg(id)))
} yield found

def updateStatus(id: BSONObjectID, newStatus: TodoStatus): Task[WriteResult] =
def updateStatus(id: BSONObjectID, newStatus: TodoStatus): Task[Option[Int]] =
ref.update(_.map { tt =>
if (tt.id == id) tt.modify(_.status).setTo(newStatus)
else tt
}) *> updateResult
}) *> defaultResult

def deleteById(id: BSONObjectID): Task[WriteResult] =
def deleteById(id: BSONObjectID): Task[Option[Int]] =
ref.update(_.filter(_.id != id)) *> defaultResult
}
}
}

def getAll: ZIO[TodoRepository, Throwable, List[TodoTask]] =
ZIO.accessM[TodoRepository](_.get.getAll)
def insert(todoTask: TodoTask): ZIO[TodoRepository, Throwable, WriteResult] =
def insert(todoTask: TodoTask): ZIO[TodoRepository, Throwable, Option[Int]] =
ZIO.accessM[TodoRepository](_.get.insert(todoTask))
def findById(id: BSONObjectID): ZIO[TodoRepository, Throwable, TodoTask] =
ZIO.accessM[TodoRepository](_.get.findById(id))
def updateStatus(id: BSONObjectID, newStatus: TodoStatus): ZIO[TodoRepository, Throwable, WriteResult] =
def updateStatus(id: BSONObjectID, newStatus: TodoStatus): ZIO[TodoRepository, Throwable, Option[Int]] =
ZIO.accessM[TodoRepository](_.get.updateStatus(id, newStatus))
def deleteById(id: BSONObjectID): ZIO[TodoRepository, Throwable, WriteResult] =
def deleteById(id: BSONObjectID): ZIO[TodoRepository, Throwable, Option[Int]] =
ZIO.accessM[TodoRepository](_.get.deleteById(id))
}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.4.2
sbt.version=1.4.3

0 comments on commit 2db1aa4

Please sign in to comment.