Skip to content

Commit

Permalink
Create or update has been wrong implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejjozwik committed Jul 30, 2019
1 parent b32bba9 commit 913f10d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
Expand Up @@ -19,16 +19,16 @@ class QuillCrudMacro(val c: MacroContext) {
def createAndGenerateIdOrUpdate(entity: Tree)(dSchema: Tree): Tree =
q"""
import ${c.prefix}._
val id = $entity.id
util.Try {
transaction{
val result = run(
$dSchema.filter(_.id == entity.id).updateValue($entity)
$dSchema.filter(_.id == lift(id))
)
if (result == 0) {
run(
$dSchema.insertValue($entity).returning(_.id)
)
if (result.isEmpty) {
run($dSchema.insertValue($entity).returningGenerated(_.id))
} else {
run($dSchema.updateValue($entity))
$entity.id
}
}
Expand All @@ -38,18 +38,19 @@ class QuillCrudMacro(val c: MacroContext) {
def createOrUpdate(entity: Tree)(dSchema: Tree): Tree =
q"""
import ${c.prefix}._
val id = $entity.id
util.Try {
transaction{
val result = run(
$dSchema.filter(_.id == entity.id).updateValue($entity)
val result = run(
$dSchema.filter(_.id == lift(id))
)
if (result == 0) {
run(
$dSchema.insertValue($entity)
)
if(result.isEmpty){
run($dSchema.insertValue($entity))
} else {
run($dSchema.updateValue($entity))
}
$entity.id
}
$entity.id
}
"""

Expand Down
Expand Up @@ -80,7 +80,8 @@ class QuillCrudSpec extends AbstractSpec {
logger.debug("generated id")
val person = Person(PersonId.empty, "firstName", "lastName", today)
repository.all shouldBe Try(Seq())
val personId = repository.create(person)
val personId = repository.createOrUpdate(person)
personId shouldBe 'success
val personIdProvided = personId.success.value
val createdPatron = repository.read(personIdProvided).success.value.getOrElse(fail())
repository.update(createdPatron) shouldBe 'success
Expand All @@ -96,19 +97,21 @@ class QuillCrudSpec extends AbstractSpec {
repository.max shouldBe Success(None)
}

"Configuration " in {
"Handle simple Configuration with custom id" in {
val repository = new ConfigurationRepository(ctx)
logger.debug("configuration")
val entity = Configuration(ConfigurationId("firstName"), "lastName")
repository.all shouldBe Try(Seq())
val entityId = repository.create(entity)
val entityId = repository.createOrUpdate(entity)
entityId shouldBe 'success
val entityIdProvided = entityId.success.value
val createdEntity = repository.read(entityIdProvided).success.value.getOrElse(fail())
repository.update(createdEntity) shouldBe 'success
repository.all shouldBe Success(Seq(createdEntity))
val newValue = "newValue"
val modified = createdEntity.copy(value = newValue)
repository.update(modified) shouldBe 'success
repository.createOrUpdate(modified) shouldBe 'success
repository.read(createdEntity.id).success.value.map(_.value) shouldBe Option(newValue)
repository.delete(createdEntity.id) shouldBe 'success
repository.read(createdEntity.id).success.value shouldBe empty
Expand Down
2 changes: 1 addition & 1 deletion version.sbt
@@ -1 +1 @@
ThisBuild / version := "0.3.3-SNAPSHOT"
ThisBuild / version := "0.3.3"

0 comments on commit 913f10d

Please sign in to comment.