Skip to content

Commit

Permalink
Add entry endpoint, gce
Browse files Browse the repository at this point in the history
  • Loading branch information
RawToast committed Mar 24, 2018
1 parent d5514c8 commit 32fa2df
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
12 changes: 6 additions & 6 deletions dokusho-server/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dokusho


import cats.data.OptionT
import cats.effect.IO
import io.circe.generic.auto._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
33 changes: 31 additions & 2 deletions dokusho-server/src/main/scala/dokusho/ReadingHistoryService.scala
Original file line number Diff line number Diff line change
@@ -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))
}
}
10 changes: 5 additions & 5 deletions dokusho/src/app/Client.re
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ 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)
);
let jsonHeader = Fetch.HeadersInit.make({"Content-Type": "application/json"});
let putReadingData = (userId, readingData) =>
Js.Promise.(
Fetch.fetchWithInit(
internalURI ++ "/history/safe/" ++ userId,
backendURI ++ "/history/safe/" ++ userId,
Fetch.RequestInit.make(
~method_=Put,
~body=
Expand All @@ -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,
Expand Down

0 comments on commit 32fa2df

Please sign in to comment.