DBService object to class with logger injection
This commit is contained in:
@@ -2,6 +2,8 @@ package db
|
||||
|
||||
import cats.effect.{IO, Resource}
|
||||
import cats.implicits.toTraverseOps
|
||||
import org.typelevel.log4cats.Logger
|
||||
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
||||
|
||||
import java.io.File
|
||||
import java.nio.file.{Files, Paths}
|
||||
@@ -11,6 +13,12 @@ import scala.io.Source
|
||||
import scala.util.Try
|
||||
|
||||
object DBService {
|
||||
def of: IO[DBService] = {
|
||||
Slf4jLogger.create[IO].map(logger => new DBService(logger))
|
||||
}
|
||||
}
|
||||
|
||||
class DBService(log: Logger[IO]) {
|
||||
private val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
||||
private val dataPath = "./data"
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package db
|
||||
import cats.effect.IO
|
||||
import cats.effect.unsafe.implicits.global
|
||||
import cats.implicits.toTraverseOps
|
||||
import db.DBService
|
||||
|
||||
import java.time.{LocalDate, LocalDateTime}
|
||||
import java.time.format.DateTimeFormatter
|
||||
@@ -16,21 +15,24 @@ object Main {
|
||||
val to = LocalDateTime.parse("20230501_1230", dateFormatter)
|
||||
|
||||
for {
|
||||
lines <- DBService.getInRange(from, to)
|
||||
dbService <- DBService.of
|
||||
lines <- dbService.getInRange(from, to)
|
||||
_ <- lines.traverse(IO.println)
|
||||
} yield ()
|
||||
}
|
||||
|
||||
private def testGetDates: IO[Unit] = {
|
||||
for {
|
||||
dates <- DBService.getDates()
|
||||
dbService <- DBService.of
|
||||
dates <- dbService.getDates()
|
||||
_ <- IO.println(dates)
|
||||
} yield ()
|
||||
}
|
||||
|
||||
private def testGetDate: IO[Unit] = {
|
||||
for {
|
||||
dates <- DBService.getDateFileNames(LocalDate.of(2023, 4, 23))
|
||||
dbService <- DBService.of
|
||||
dates <- dbService.getDateFileNames(LocalDate.of(2023, 4, 23))
|
||||
_ <- IO.println(dates)
|
||||
} yield ()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user