diff --git a/dokusho-server/src/main/scala/Main.scala b/dokusho-server/src/main/scala/Main.scala index f64f975..56b03b1 100644 --- a/dokusho-server/src/main/scala/Main.scala +++ b/dokusho-server/src/main/scala/Main.scala @@ -1,10 +1,9 @@ import cats.effect._ -import dokusho.{MongoRepository, MongoService} -import org.http4s.dsl.io._ -import org.http4s.server.blaze.BlazeBuilder -import fs2.{Stream, StreamApp} +import dokusho.{MongoRepository, ReadingHistoryEndpoint, ReadingHistoryService} import fs2.StreamApp.ExitCode +import fs2.{Stream, StreamApp} import org.http4s.server.ServerBuilder +import org.http4s.server.blaze.BlazeBuilder import scala.concurrent.ExecutionContext.Implicits.global @@ -16,13 +15,14 @@ object Main extends StreamApp[IO] { "test", "dokusho") - val mongoService = new MongoService(mongo) + val readingHistoryService = new ReadingHistoryService(mongo) + val historyService = new ReadingHistoryEndpoint(readingHistoryService) override def stream(args: List[String], requestShutdown: IO[Unit]): Stream[IO, ExitCode] = BlazeBuilder[IO] .bindHttp(8080, "0.0.0.0") - .mountService(mongoService.routes, "/") + .mountService(historyService.routes, "/") .withBanner(ServerBuilder.DefaultBanner) .serve } \ No newline at end of file diff --git a/dokusho-server/src/main/scala/dokusho/MongoRepository.scala b/dokusho-server/src/main/scala/dokusho/MongoRepository.scala index 345ac35..a6353e7 100644 --- a/dokusho-server/src/main/scala/dokusho/MongoRepository.scala +++ b/dokusho-server/src/main/scala/dokusho/MongoRepository.scala @@ -1,6 +1,5 @@ package dokusho - import cats.data.OptionT import cats.effect.IO import io.circe.generic.auto._ diff --git a/dokusho-server/src/main/scala/dokusho/ReadingHistoryEndpoint.scala b/dokusho-server/src/main/scala/dokusho/ReadingHistoryEndpoint.scala index 5bac040..8e56e18 100644 --- a/dokusho-server/src/main/scala/dokusho/ReadingHistoryEndpoint.scala +++ b/dokusho-server/src/main/scala/dokusho/ReadingHistoryEndpoint.scala @@ -27,5 +27,13 @@ class ReadingHistoryEndpoint(readingHistoryService: ReadingHistoryService) { json: Json = SuccessfulPut(storedHistory.userId).asJson response <- Ok(json) } yield response + case req@PUT -> Root / "history" / userId / "add" => + implicit val entryDecoder: EntityDecoder[IO, Entry] = jsonOf[IO, Entry] + for { + entry <- req.as[Entry] + storedHistory <- readingHistoryService.addNewEntry(userId, entry ) + json = storedHistory.map(_.asJson) + result <- json.fold(NotFound())(j => Ok(j)) + } yield result } } \ No newline at end of file diff --git a/dokusho-server/src/main/scala/dokusho/ReadingHistoryService.scala b/dokusho-server/src/main/scala/dokusho/ReadingHistoryService.scala index 3174314..2af94b1 100644 --- a/dokusho-server/src/main/scala/dokusho/ReadingHistoryService.scala +++ b/dokusho-server/src/main/scala/dokusho/ReadingHistoryService.scala @@ -1,5 +1,34 @@ package dokusho -class ReadingHistoryService { +import java.time.LocalDate -} +import cats.data.OptionT +import cats.effect.IO +import monocle.macros.GenLens + +class ReadingHistoryService(mongoRepository: MongoRepository) { + + def getReadingHistory(userId: String): IO[Option[UserReadingHistory]] = + mongoRepository.get(userId) + + def addNewEntry(userId: String, entry: Entry): IO[Option[UserReadingHistory]] = { + lazy val update = daysLens.modify(updateDay(entry)) + OptionT(getReadingHistory(userId)) + .map(update) + .semiflatMap(upsert) + .value + } + + def upsert(userReadingHistory: UserReadingHistory): IO[UserReadingHistory] = + mongoRepository.put(userReadingHistory) + + private lazy val daysLens = GenLens[UserReadingHistory](_.readingHistory.days) + + private def updateDay(entry: Entry)(days: Seq[Day]) = { + val newDay = Day(LocalDate.now().atStartOfDay().toString, Seq.empty) + val ndays = if (days.exists(_.date == newDay.date)) days else newDay +: days + + ndays.withFilter(d => d.date == LocalDate.now().atStartOfDay().toString) + .map(d => d.copy(entries = entry +: d.entries)) + } +} \ No newline at end of file diff --git a/dokusho/src/app/Client.re b/dokusho/src/app/Client.re index 7a0c94d..9066495 100644 --- a/dokusho/src/app/Client.re +++ b/dokusho/src/app/Client.re @@ -12,11 +12,11 @@ module Client = { userId: json |> field("userId", string), readingHistory: json |> field("readingHistory", Decoders.parseHistory) }; - let backendURI = "http://192.168.64.4:30908"; - let internalURI = "http://backend:30908"; + let backendURI = "http://192.168.64.3:30181"; + /* let internalURI = "http://backend:30908"; */ let userHistory = userId => Js.Promise.( - Fetch.fetch(internalURI ++ "/history/safe/" ++ userId) + Fetch.fetch(backendURI ++ "/history/safe/" ++ userId) |> then_(Fetch.Response.json) |> then_(resp => resp |> parseResponse |> resolve) ); @@ -24,7 +24,7 @@ module Client = { let putReadingData = (userId, readingData) => Js.Promise.( Fetch.fetchWithInit( - internalURI ++ "/history/safe/" ++ userId, + backendURI ++ "/history/safe/" ++ userId, Fetch.RequestInit.make( ~method_=Put, ~body= @@ -40,7 +40,7 @@ module Client = { ); let newEntry = (userId, {kind, value}) => { Js.Promise.( - Fetch.fetchWithInit(internalURI ++ "/history/" ++ userId ++ "/entries", + Fetch.fetchWithInit(backendURI ++ "/history/" ++ userId ++ "/entries", Fetch.RequestInit.make(~method_=Post, ~body=Fetch.BodyInit.make(Encoders.endcodeInput(kind, value) |> Js.Json.stringify), ~headers=jsonHeader,