Skip to content

Commit

Permalink
Working backend service
Browse files Browse the repository at this point in the history
  • Loading branch information
RawToast committed Mar 10, 2018
1 parent a67da7d commit b5e21ec
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode
.idea
*.iml
6 changes: 5 additions & 1 deletion dokusho-server/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ val SCALA_VERSION = "2.12.4"
val CIRCE_VERSION = "0.9.1"
val HTTP4S_VERSION = "0.18.1"
val MONGO_VERSION = "2.2.1"
val MONOCLE_VERSION = "1.5.0"

resolvers ++= Seq(
Resolver.sonatypeRepo("releases"),
Expand All @@ -24,7 +25,10 @@ libraryDependencies ++= Seq(

"org.mongodb.scala" %% "mongo-scala-driver" % MONGO_VERSION,

"ch.qos.logback" % "logback-classic" % "1.2.3"
"ch.qos.logback" % "logback-classic" % "1.2.3",

"com.github.julien-truffaut" %% "monocle-core" % MONOCLE_VERSION,
"com.github.julien-truffaut" %% "monocle-macro" % MONOCLE_VERSION
)

scalacOptions ++= Seq("-Ypartial-unification")
2 changes: 1 addition & 1 deletion dokusho-server/dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ COPY . .

RUN sbt compile

CMD sbt run
CMD ["sbt", "run"]
2 changes: 1 addition & 1 deletion dokusho-server/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object Main extends StreamApp[IO] {

override def stream(args: List[String], requestShutdown: IO[Unit]): Stream[IO, ExitCode] =
BlazeBuilder[IO]
.bindHttp(8080, "localhost")
.bindHttp(8080, "0.0.0.0")
.mountService(mongoService.routes, "/")
.withBanner(ServerBuilder.DefaultBanner)
.serve
Expand Down
16 changes: 15 additions & 1 deletion dokusho-server/src/main/scala/dokusho/MongoRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.circe.generic.auto._
import io.circe.parser._
import io.circe.syntax._
import io.circe.{DecodingFailure, Json, ParsingFailure}
import monocle.macros.GenLens
import org.bson.Document
import org.mongodb.scala.model.Filters._
import org.mongodb.scala.{MongoClient, MongoCollection, Observable, model}
Expand Down Expand Up @@ -37,9 +38,22 @@ class MongoRepository(connectionString: String, databaseName: String, collection
days = urh.readingHistory.days
dayToUpdate = days.find(_.date == userId).getOrElse(Day(date, Seq.empty))
updatedDay = dayToUpdate.copy(entries = dayToUpdate.entries :+ entry)
_ = urh.copy(read)
doc = daysLens.modify(upsertDay(updatedDay))(urh)
} yield doc
}
val daysLens = GenLens[UserReadingHistory](_.readingHistory.days)

def upsertDay(day:Day)(days: Seq[Day]): Seq[Day] =
if(days.exists(d => d.date == day.date)){
days.withFilter(d => d.date == day.date)
.map(d => day)



} else {
days :+ day
}



private def getDocument(id: String): IO[Document] = collection.find(equal("userId", id)).asIO
Expand Down
2 changes: 1 addition & 1 deletion dokusho-server/src/main/scala/dokusho/MongoService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MongoService(mongoRepository: MongoRepository) {
response <- Ok(json)
} yield response
case req@PUT -> Root / "history" / userId =>
implicit val userDecoder: EntityDecoder[IO, UserReadingHistory] = jsonOf[IO, UserReadingHistory]
implicit val userDecoder: EntityDecoder[IO, ReadingHistory] = jsonOf[IO, ReadingHistory]
for {
userReadingHistory <- req.as[ReadingHistory]
storedHistory <- mongoRepository.put(UserReadingHistory(userId, userReadingHistory))
Expand Down
4 changes: 2 additions & 2 deletions kube/backend-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: ReplicationController
metadata:
labels:
name: backend
name: backend-controller
name: backend
spec:
replicas: 2
selector:
Expand All @@ -18,4 +18,4 @@ spec:
name: backend
ports:
- containerPort: 8080
name: backend-server
name: backend-server
2 changes: 1 addition & 1 deletion kube/mongo-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: ReplicationController
metadata:
labels:
name: mongo
name: mongo-controller
name: mongo
spec:
replicas: 1
template:
Expand Down

0 comments on commit b5e21ec

Please sign in to comment.