Refactor save return type without Either
This commit is contained in:
@@ -40,14 +40,14 @@ class DBService(log: Logger[IO]) extends DataServiceTrait {
|
||||
}
|
||||
}
|
||||
|
||||
def save(fileName: String, content: String): IO[Either[Throwable, String]] = {
|
||||
def save(fileName: String, content: String): IO[String] = {
|
||||
val path = Paths.get(s"$dataPath/$fileName")
|
||||
IO(Files.writeString(path, content))
|
||||
.redeemWith(
|
||||
error => IO(Left(error)) // <* log.error(s"Write file '$fileName' failed with error: ${error.getMessage}")
|
||||
,
|
||||
_ => IO(Right(fileName)) // <* log.info(s"write: $fileName")
|
||||
)
|
||||
.attempt
|
||||
.flatMap {
|
||||
case Left(error) => log.error(s"Write file '$fileName' failed with error: ${error.getMessage}") *> IO.raiseError(error)
|
||||
case Right(_) => log.info(s"write: $fileName").as(fileName)
|
||||
}
|
||||
}
|
||||
|
||||
def readFile(fileName: String): IO[List[String]] = {
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.typelevel.log4cats.slf4j.Slf4jLogger
|
||||
import java.time.{Instant, LocalDate, LocalDateTime, ZoneId}
|
||||
|
||||
trait DataServiceTrait {
|
||||
def save(fileName: String, content: String): IO[Either[Throwable, String]]
|
||||
def save(fileName: String, content: String): IO[String]
|
||||
def readFile(fileName: String): IO[List[String]]
|
||||
def getInRange(from: LocalDateTime, to: LocalDateTime): IO[List[String]]
|
||||
def getDates: IO[List[LocalDate]]
|
||||
@@ -46,14 +46,15 @@ class DataService private(
|
||||
}
|
||||
}
|
||||
|
||||
def save(fileName: String, content: String): IO[Either[Throwable, String]] = {
|
||||
dbService.save(fileName, content).flatMap {
|
||||
case Right(savedFileName) =>
|
||||
def save(fileName: String, content: String): IO[String] = {
|
||||
dbService.save(fileName, content).redeemWith(
|
||||
error => IO.raiseError(error),
|
||||
savedFileName => {
|
||||
state.update(st => st.updated(savedFileName, content.split("\n").toList)) *>
|
||||
filterState *>
|
||||
logState.as(Right(savedFileName))
|
||||
case e@Left(_) => IO.pure(e)
|
||||
}
|
||||
filterState *>
|
||||
logState.as(savedFileName)
|
||||
}
|
||||
).onError(error => log.info(s"errr... $error"))
|
||||
}
|
||||
|
||||
def readFile(fileName: String): IO[List[String]] = dbService.readFile(fileName)
|
||||
|
||||
Reference in New Issue
Block a user