Skip to content

Commit

Permalink
Tweaks to addNewEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
RawToast committed Mar 27, 2018
1 parent 6bd93ea commit 6d9e486
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
4 changes: 2 additions & 2 deletions dokusho-server/dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ FROM hseeberger/scala-sbt:8u151-2.12.4-1.1.1

COPY build.sbt ./

COPY project/build.properties ./
COPY project/build.properties ./project/

RUN sbt update

COPY src ./
COPY src ./src

RUN sbt clean compile

Expand Down
3 changes: 0 additions & 3 deletions dokusho-server/src/main/scala/dokusho/MongoRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import scala.concurrent.ExecutionContext

class MongoRepository(connectionString: String, databaseName: String, collectionName: String) {

private lazy val daysLens = GenLens[UserReadingHistory](_.readingHistory.days)
private lazy val entriesLens = GenLens[Day](_.entries)

implicit val executionContext: ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global

private lazy val collection: MongoCollection[Document] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import java.time.LocalDate
import cats.data.OptionT
import cats.effect.IO
import monocle.macros.GenLens
import org.bson.Document
import org.mongodb.scala.model
import org.mongodb.scala.model.Filters.equal

class ReadingHistoryService(mongoRepository: MongoRepository) {

Expand All @@ -17,8 +14,8 @@ class ReadingHistoryService(mongoRepository: MongoRepository) {
def getReadingHistory(userId: String): IO[Option[UserReadingHistory]] =
mongoRepository.get(userId)

def addNewEntry(userId: String, entry: NewEntry): IO[Option[UserReadingHistory]] = {
lazy val update = daysLens.modify(updateDay(entry))
def addNewEntry(userId: String, newEntry: NewEntry): IO[Option[UserReadingHistory]] = {
lazy val update = daysLens.modify(updateDay(newEntry))
OptionT(getReadingHistory(userId))
.map(update)
.semiflatMap(upsert)
Expand All @@ -33,15 +30,16 @@ class ReadingHistoryService(mongoRepository: MongoRepository) {
upsert(emptyHistory)
}

private def updateDay(entry: NewEntry)(days: Seq[Day]) = {
private def updateDay(entry: NewEntry)(days: Seq[Day])= {
val currentDay = Day(LocalDate.now().atStartOfDay().toString, Seq.empty)

val updateDayWithEntry = addEntry(entry)
val daysWithUpdatedDay =
if (days.exists(_.date == currentDay.date)) days
else currentDay +: days

daysWithUpdatedDay
.map(d => if (d.date == currentDay.date) addEntry(entry) else d).seq
.map(d => if (d.date == currentDay.date) updateDayWithEntry(d) else d)
}

private def addEntry(entry: NewEntry) = entriesLens
Expand Down

0 comments on commit 6d9e486

Please sign in to comment.