Skip to content

Commit

Permalink
Merge pull request #20 from jihun315016/develop
Browse files Browse the repository at this point in the history
DB CRUD(Zombie Game)
  • Loading branch information
SHSongs authored Aug 8, 2023
2 parents 0607447 + 4ca5ca2 commit 3ed6635
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
26 changes: 26 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,32 @@ lazy val `forecast-cheese` = project
)
)


lazy val `simple-jh` = project
.settings(sharedSettings)
.settings(
libraryDependencies ++= Seq(
"org.tpolecat" %% "doobie-core" % "1.0.0-RC2",
"io.github.gaelrenoux" %% "tranzactio" % "4.1.0",
"org.xerial" % "sqlite-jdbc" % "3.40.1.0"
)
)


//lazy val `forecast` = project.dependsOn(`read-file`)
// .settings(sharedSettings)
// .settings(
// libraryDependencies ++= Seq(
// "dev.zio" %% "zio" % zioVersion,
// "com.lihaoyi" %% "ujson" % "3.0.0",
// "com.softwaremill.sttp.client3" %% "core" % "3.8.16",
// "com.softwaremill.sttp.client3" %% "zio-json" % "3.3.9",
// "dev.zio" %% "zio-http" % "3.0.0-RC2",
// "org.scalameta" %% "scalafmt-core" % "2.7.5"
// )
// )


lazy val `forecast-subway` = project
.settings(sharedSettings)
.settings(
Expand Down
62 changes: 62 additions & 0 deletions simple-jh/src/main/scala/DBSampleApp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import io.github.gaelrenoux.tranzactio.ConnectionSource
import io.github.gaelrenoux.tranzactio.doobie.{Database, tzio}
import zio._
import doobie._
import doobie.implicits._

case class TestTableRow(name: String, hobby: String)

object DBSampleApp extends ZIOAppDefault {

def insertTableRow(row: TestTableRow): ZIO[Database, Throwable, Int] = {
val insertQuery = tzio {
sql"""|insert into test_table (name, hobby)
|values (${row.name}, ${row.hobby})""".stripMargin.update.run
}

val db = ZIO.service[Database]
db.flatMap(database => database.transactionOrWiden(insertQuery))
}

val prog = for {
_ <- ZIO.unit
database <- ZIO.service[Database]
_ <- insertTableRow(TestTableRow("John", "Skiing"))
rows <- database
.transactionOrWiden(for {
res <- tzio {
sql"""|select name, hobby
|from zombie""".stripMargin
.query[TestTableRow]
.to[List]
}
} yield res)

_ <- zio.Console.printLine(rows)

} yield ()
override def run = prog.provide(
conn >>> ConnectionSource.fromConnection >>> Database.fromConnectionSource
)

val sqlite = locally {
val path = SimpleUtil.path
s"jdbc:sqlite:$path"
}

val postgres = locally {
val path = "localhost:5432"
val name = "postgres"
val user = "postgres"
val password = "1q2w3e4r"
s"jdbc:postgresql://$path/$name?user=$user&password=$password"
}

private val conn = ZLayer(
ZIO.attempt(
java.sql.DriverManager.getConnection(
sqlite
)
)
)
}
3 changes: 3 additions & 0 deletions simple-jh/src/main/scala/SimpleUtil.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object SimpleUtil {

}
69 changes: 69 additions & 0 deletions simple-jh/src/main/scala/ZombieGame.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import io.github.gaelrenoux.tranzactio.ConnectionSource
import io.github.gaelrenoux.tranzactio.doobie.{Database, tzio}
import zio._
import doobie._
import doobie.implicits._

case class Zombie(name: String)

object ZombieGame extends ZIOAppDefault {
//val config = ScalafmtConfig.fromHoconFile("./scalafmt.conf")

def getTargetName(rows: List[Zombie], choice: Integer) = for {
result <- ZIO.attempt(rows(choice).name)
.catchAll(cause => ZIO.succeed("")
)
} yield result

val prog = for {
_ <- ZIO.unit
database <- ZIO.service[Database]
rows <- database
.transactionOrWiden(for {
res <- tzio {
sql"""|select name
|from zombie""".stripMargin
.query[Zombie]
.to[List]
}
} yield res)

_ <- zio.Console.printLine(rows)

input <- zio.Console.readLine(s"좀비 번호 입력(1~${rows.size}) : ")
nInput = Integer.parseInt(input) - 1

target <- getTargetName(rows, nInput)
_ <- target match {
case "" => zio.Console.printLine("좀비 무서워서 도망갔다!")
case _ => for {
_ <- database
.transactionOrWiden(for {
res <- tzio {
sql"""|delete from zombie where name = ${target}""".stripMargin
.update
.run
}
} yield res)
_ <- zio.Console.printLine(s"${target}을 손봐줬다!")
} yield ()
}
} yield ()

override def run = prog.provide(
conn >>> ConnectionSource.fromConnection >>> Database.fromConnectionSource
)

val sqlite = locally {
val path = SimpleUtil.path
s"jdbc:sqlite:$path"
}

private val conn = ZLayer(
ZIO.attempt(
java.sql.DriverManager.getConnection(
sqlite
)
)
)
}

0 comments on commit 3ed6635

Please sign in to comment.