merge entries with existing entries on DB insert
This commit is contained in:
parent
40ea76895e
commit
f43fff233c
6 changed files with 44 additions and 58 deletions
|
|
@ -14,8 +14,6 @@ import scala.scalajs.js
|
||||||
import model.Entry
|
import model.Entry
|
||||||
import model.EntryId
|
import model.EntryId
|
||||||
import rdts.base.Lattice
|
import rdts.base.Lattice
|
||||||
import rdts.datatypes.LastWriterWins
|
|
||||||
import scala.scalajs.js.Date
|
|
||||||
import scala.util.Failure
|
import scala.util.Failure
|
||||||
import scala.util.Success
|
import scala.util.Success
|
||||||
|
|
||||||
|
|
@ -43,56 +41,27 @@ object DexieDB {
|
||||||
.toFuture
|
.toFuture
|
||||||
.map(_.toOption.map(NativeConverter[Entry].fromNative(_)))
|
.map(_.toOption.map(NativeConverter[Entry].fromNative(_)))
|
||||||
|
|
||||||
def insertEntry(entry: Entry): Unit = {
|
/** Inserts an entry into the database and merges it with an existing entry if
|
||||||
val e: Future[Option[Entry]] = getEntry(entry.id)
|
* it exists.
|
||||||
e.flatMap {
|
*
|
||||||
case Some(oldEntry) =>
|
* @param entry
|
||||||
println(s"found old: $oldEntry")
|
* The entry to be inserted or updated.
|
||||||
println(s"found new: $entry")
|
*/
|
||||||
println(oldEntry.id == entry.id)
|
def upsertEntry(entry: Entry): Unit = {
|
||||||
val newEntry = Lattice[Entry].merge(entry, entry)
|
for {
|
||||||
val newEntry2 = Lattice[Entry].merge(oldEntry, oldEntry)
|
oldEntry <- getEntry(entry.id)
|
||||||
println(oldEntry.id)
|
newEntry = oldEntry match
|
||||||
println(entry.id)
|
case Some(old) =>
|
||||||
val test =
|
Lattice[Entry].merge(entry, old)
|
||||||
Lattice[Entry].merge(
|
case _ => entry
|
||||||
Entry(
|
result <- entriesTable.put(newEntry.toNative).toFuture
|
||||||
EntryId("1"),
|
} yield {
|
||||||
LastWriterWins.now(0),
|
result
|
||||||
LastWriterWins.now(2),
|
|
||||||
LastWriterWins.now(""),
|
|
||||||
LastWriterWins.now(false),
|
|
||||||
LastWriterWins.now("Dirk"),
|
|
||||||
LastWriterWins.now(new Date())
|
|
||||||
),
|
|
||||||
Entry(
|
|
||||||
EntryId("1"),
|
|
||||||
LastWriterWins.now(0),
|
|
||||||
LastWriterWins.now(2),
|
|
||||||
LastWriterWins.now(""),
|
|
||||||
LastWriterWins.now(false),
|
|
||||||
LastWriterWins.now("Dirk"),
|
|
||||||
LastWriterWins.now(new Date())
|
|
||||||
)
|
|
||||||
)
|
|
||||||
val newEntry3 =
|
|
||||||
Lattice[Entry].merge(oldEntry, entry.copy(id = oldEntry.id))
|
|
||||||
println("yolo")
|
|
||||||
Future.unit
|
|
||||||
// entriesTable.put(newEntry.toNative).toFuture
|
|
||||||
case _ =>
|
|
||||||
entriesTable.put(entry.toNative).toFuture
|
|
||||||
}.onComplete {
|
|
||||||
case Failure(exception) => println(s"failed with $exception")
|
|
||||||
case Success(value) => ()
|
|
||||||
}
|
}
|
||||||
// .toFuture
|
}.onComplete {
|
||||||
// .map(e =>
|
case Failure(exception) =>
|
||||||
// if e.isUndefined then entriesTable.put(entry.toNative).toFuture
|
println(s"Failed to write entry to db: $exception")
|
||||||
// else
|
case Success(value) => ()
|
||||||
// val dbEntry = NativeConverter[Entry].fromNative(e)
|
|
||||||
// Lattice[Entry].merge(entry, dbEntry)
|
|
||||||
// )
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def getAllEntries(): Future[Seq[Entry]] = {
|
def getAllEntries(): Future[Seq[Entry]] = {
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ object Main {
|
||||||
// track changes to entries
|
// track changes to entries
|
||||||
val entryEditBus = new EventBus[Entry]
|
val entryEditBus = new EventBus[Entry]
|
||||||
val entryDbObserver =
|
val entryDbObserver =
|
||||||
Observer[Entry](onNext = DexieDB.insertEntry(_))
|
Observer[Entry](onNext = DexieDB.upsertEntry(_))
|
||||||
entryEditBus.stream.tapEach(_ => println("lalilu"))
|
entryEditBus.stream.tapEach(_ => println("lalilu"))
|
||||||
println("test")
|
println("test")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ class AppComponent(
|
||||||
.combineWith(editStateSignal)
|
.combineWith(editStateSignal)
|
||||||
.map { case (entries, editState) =>
|
.map { case (entries, editState) =>
|
||||||
entries.toList
|
entries.toList
|
||||||
.sortBy(_.date.payload.getTime())
|
.sortBy(_.date.payload)
|
||||||
.map(entry =>
|
.map(entry =>
|
||||||
EntryComponent(
|
EntryComponent(
|
||||||
entry,
|
entry,
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import org.scalajs.dom.HTMLTableRowElement
|
||||||
import com.raquo.laminar.api.L.*
|
import com.raquo.laminar.api.L.*
|
||||||
import com.raquo.laminar.api.features.unitArrows
|
import com.raquo.laminar.api.features.unitArrows
|
||||||
import scala.util.Try
|
import scala.util.Try
|
||||||
|
import scala.scalajs.js.Date
|
||||||
|
|
||||||
class EntryComponent(
|
class EntryComponent(
|
||||||
entry: Entry,
|
entry: Entry,
|
||||||
|
|
@ -110,7 +111,7 @@ class EntryComponent(
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
tr(
|
tr(
|
||||||
td(entry.date.payload.toISOString()),
|
td(new Date(entry.date.payload).toISOString()),
|
||||||
td(entry.driver.payload),
|
td(entry.driver.payload),
|
||||||
td(entry.startKm.payload),
|
td(entry.startKm.payload),
|
||||||
td(entry.endKm.payload),
|
td(entry.endKm.payload),
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import com.raquo.laminar.api.features.unitArrows
|
||||||
import fahrtenbuch.Main.entryEditBus
|
import fahrtenbuch.Main.entryEditBus
|
||||||
import fahrtenbuch.model.{Entry, EntryId}
|
import fahrtenbuch.model.{Entry, EntryId}
|
||||||
import rdts.datatypes.LastWriterWins
|
import rdts.datatypes.LastWriterWins
|
||||||
import scala.scalajs.js.Date
|
|
||||||
import scala.util.Try
|
import scala.util.Try
|
||||||
|
|
||||||
class NewEntryInput(showNewEntryField: Var[Boolean]):
|
class NewEntryInput(showNewEntryField: Var[Boolean]):
|
||||||
|
|
@ -82,9 +81,8 @@ class NewEntryInput(showNewEntryField: Var[Boolean]):
|
||||||
val endKm = LastWriterWins.now(newEntryEndKm.ref.value.toDouble)
|
val endKm = LastWriterWins.now(newEntryEndKm.ref.value.toDouble)
|
||||||
val animal = LastWriterWins.now(newEntryAnimal.ref.value)
|
val animal = LastWriterWins.now(newEntryAnimal.ref.value)
|
||||||
val paid = LastWriterWins.now(newEntryPaid.ref.checked)
|
val paid = LastWriterWins.now(newEntryPaid.ref.checked)
|
||||||
val date = LastWriterWins.now(new Date(Date.now()))
|
|
||||||
entryEditBus.emit(
|
entryEditBus.emit(
|
||||||
Entry(id, startKm, endKm, animal, paid, driver, date)
|
Entry(id, startKm, endKm, animal, paid, driver)
|
||||||
)
|
)
|
||||||
showNewEntryField.set(false)
|
showNewEntryField.set(false)
|
||||||
newEntryDriver.ref.value = ""
|
newEntryDriver.ref.value = ""
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ case class Entry(
|
||||||
animal: LastWriterWins[String],
|
animal: LastWriterWins[String],
|
||||||
paid: LastWriterWins[Boolean],
|
paid: LastWriterWins[Boolean],
|
||||||
driver: LastWriterWins[String],
|
driver: LastWriterWins[String],
|
||||||
date: LastWriterWins[Date]
|
date: LastWriterWins[Double]
|
||||||
) derives NativeConverter:
|
) derives NativeConverter:
|
||||||
val distance = endKm.payload - startKm.payload
|
val distance = endKm.payload - startKm.payload
|
||||||
|
|
||||||
|
|
@ -42,3 +42,21 @@ case class Entry(
|
||||||
|
|
||||||
object Entry:
|
object Entry:
|
||||||
given Lattice[Entry] = Lattice.derived
|
given Lattice[Entry] = Lattice.derived
|
||||||
|
|
||||||
|
def apply(
|
||||||
|
id: EntryId,
|
||||||
|
startKm: LastWriterWins[Double],
|
||||||
|
endKm: LastWriterWins[Double],
|
||||||
|
animal: LastWriterWins[String],
|
||||||
|
paid: LastWriterWins[Boolean],
|
||||||
|
driver: LastWriterWins[String]
|
||||||
|
): Entry =
|
||||||
|
Entry(
|
||||||
|
id,
|
||||||
|
startKm,
|
||||||
|
endKm,
|
||||||
|
animal,
|
||||||
|
paid,
|
||||||
|
driver,
|
||||||
|
LastWriterWins.now(Date.now())
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue