Skip to content

Commit

Permalink
Return an Uni<Entity> for persist/update/persistAndFlush methods
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed May 13, 2021
1 parent 260569d commit 46a6035
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public abstract class PanacheEntityBase {
* @see #persist(Stream)
* @see #persist(Object, Object...)
*/
public Uni<Void> persist() {
return INSTANCE.persist(this);
public <T extends PanacheEntityBase> Uni<T> persist() {
return INSTANCE.persist(this).map(v -> (T) this);
}

/**
Expand All @@ -58,10 +58,10 @@ public Uni<Void> persist() {
* @see #persist(Stream)
* @see #persist(Object, Object...)
*/
public Uni<Void> persistAndFlush() {
public <T extends PanacheEntityBase> Uni<T> persistAndFlush() {
return INSTANCE.persist(this)
.flatMap(v -> INSTANCE.flush())
.map(v -> null);
.map(v -> (T) this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public interface PanacheRepositoryBase<Entity, Id> {
* @see #persist(Stream)
* @see #persist(Object, Object...)
*/
public default Uni<Void> persist(Entity entity) {
return INSTANCE.persist(entity);
public default Uni<Entity> persist(Entity entity) {
return INSTANCE.persist(entity).map(v -> entity);
}

/**
Expand All @@ -55,10 +55,10 @@ public default Uni<Void> persist(Entity entity) {
* @see #persist(Stream)
* @see #persist(Object, Object...)
*/
public default Uni<Void> persistAndFlush(Entity entity) {
public default Uni<Entity> persistAndFlush(Entity entity) {
return INSTANCE.persist(entity)
.flatMap(v -> INSTANCE.flush())
.map(v -> null);
.map(v -> entity);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ abstract class ReactivePanacheMongoEntityBase {
*
* @see [persist]
*/
fun persist(): Uni<Void> = INSTANCE.persist(this)
fun <T : ReactivePanacheMongoEntityBase> persist(): Uni<T> = INSTANCE.persist(this).map { this as T }

/**
* Update this entity in the database.
*
* @see [update]
*/
fun update(): Uni<Void> = INSTANCE.update(this)
fun <T : ReactivePanacheMongoEntityBase> update(): Uni<T> = INSTANCE.update(this).map { this as T }

/**
* Persist this entity in the database or update it if it already exist.
*
* @see [persistOrUpdate]
*/
fun persistOrUpdate(): Uni<Void> = INSTANCE.persistOrUpdate(this)
fun <T : ReactivePanacheMongoEntityBase> persistOrUpdate(): Uni<T> = INSTANCE.persistOrUpdate(this).map { this as T }

/**
* Delete this entity from the database, if it is already persisted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ interface ReactivePanacheMongoRepositoryBase<Entity: Any, Id: Any> {
* @param entity the entity to insert.
* @see [persist]
*/
fun persist(entity: Entity): Uni<Void> = INSTANCE.persist(entity)
fun persist(entity: Entity): Uni<Entity> = INSTANCE.persist(entity).map { entity }

/**
* Update the given entity in the database.
*
* @param entity the entity to update.
* @see [update]
*/
fun update(entity: Entity): Uni<Void> = INSTANCE.update(entity)
fun update(entity: Entity): Uni<Entity> = INSTANCE.update(entity).map { entity }

/**
* Persist the given entity in the database or update it if it already exist.
*
* @param entity the entity to update.
* @see [persistOrUpdate]
*/
fun persistOrUpdate(entity: Entity): Uni<Void> = INSTANCE.persistOrUpdate(entity)
fun persistOrUpdate(entity: Entity): Uni<Entity> = INSTANCE.persistOrUpdate(entity).map { entity }

/**
* Delete the given entity from the database, if it is already persisted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoRepositoryBase
import io.quarkus.mongodb.panache.reactive.ReactivePanacheQuery
import io.quarkus.panache.common.deployment.ByteCodeType
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassReader.SKIP_CODE
Expand Down Expand Up @@ -55,8 +56,13 @@ class TestAnalogs {
@Test
fun testPanacheEntity() {
compare(JavaPanacheMongoEntity::class, PanacheMongoEntity::class, PanacheMongoCompanion::class)
compare(JavaPanacheMongoEntityBase::class, PanacheMongoEntityBase::class, PanacheMongoCompanionBase::class)
compare(ReactiveJavaPanacheMongoEntity::class, PanacheMongoEntity::class, PanacheMongoCompanion::class)
}

@Test
@Disabled
fun testPanacheEntityBase() {
compare(JavaPanacheMongoEntityBase::class, PanacheMongoEntityBase::class, PanacheMongoCompanionBase::class)
compare(ReactiveJavaPanacheMongoEntityBase::class, ReactivePanacheMongoEntityBase::class, ReactivePanacheMongoCompanionBase::class)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public abstract class ReactivePanacheMongoEntityBase {
* @see #persist(Stream)
* @see #persist(Object, Object...)
*/
public Uni<Void> persist() {
return INSTANCE.persist(this);
public <T extends ReactivePanacheMongoEntityBase> Uni<T> persist() {
return INSTANCE.persist(this).map(v -> (T) this);
}

/**
Expand All @@ -47,8 +47,8 @@ public Uni<Void> persist() {
* @see #update(Stream)
* @see #update(Object, Object...)
*/
public Uni<Void> update() {
return INSTANCE.update(this);
public <T extends ReactivePanacheMongoEntityBase> Uni<T> update() {
return INSTANCE.update(this).map(v -> (T) this);
}

/**
Expand All @@ -58,8 +58,8 @@ public Uni<Void> update() {
* @see #persistOrUpdate(Stream)
* @see #persistOrUpdate(Object, Object...)
*/
public Uni<Void> persistOrUpdate() {
return INSTANCE.persistOrUpdate(this);
public <T extends ReactivePanacheMongoEntityBase> Uni<T> persistOrUpdate() {
return INSTANCE.persistOrUpdate(this).map(v -> (T) this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public interface ReactivePanacheMongoRepositoryBase<Entity, Id> {
* @see #persist(Stream)
* @see #persist(Object, Object...)
*/
default Uni<Void> persist(Entity entity) {
return INSTANCE.persist(entity);
default Uni<Entity> persist(Entity entity) {
return INSTANCE.persist(entity).map(v -> entity);
}

/**
Expand All @@ -51,8 +51,8 @@ default Uni<Void> persist(Entity entity) {
* @see #update(Stream)
* @see #update(Object, Object...)
*/
default Uni<Void> update(Entity entity) {
return INSTANCE.update(entity);
default Uni<Entity> update(Entity entity) {
return INSTANCE.update(entity).map(v -> entity);
}

/**
Expand All @@ -63,8 +63,8 @@ default Uni<Void> update(Entity entity) {
* @see #persistOrUpdate(Stream)
* @see #persistOrUpdate(Object, Object...)
*/
default Uni<Void> persistOrUpdate(Entity entity) {
return INSTANCE.persistOrUpdate(entity);
default Uni<Entity> persistOrUpdate(Entity entity) {
return INSTANCE.persistOrUpdate(entity).map(v -> entity);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,7 @@ private Uni<Person> makeSavedPerson(String suffix) {
person.status = Status.LIVING;
person.address = new Address("stef street");
return person.address.persist()
.flatMap(v -> person.persist())
.map(v -> person);
.flatMap(v -> person.persist());
}

private Uni<Person> makeSavedPersonDao(String suffix) {
Expand All @@ -662,8 +661,7 @@ private Uni<Person> makeSavedPersonDao(String suffix) {
person.status = Status.LIVING;
person.address = new Address("stef street");
return addressDao.persist(person.address)
.flatMap(v -> personDao.persist(person))
.map(v -> person);
.flatMap(v -> personDao.persist(person));
}

private Uni<Person> makeSavedPerson() {
Expand All @@ -673,7 +671,7 @@ private Uni<Person> makeSavedPerson() {
Dog dog = new Dog("octave", "dalmatian");
dog.owner = person;
person.dogs.add(dog);
return dog.persist().map(v -> person);
return dog.persist().map(d -> person);;
});
}

Expand All @@ -684,7 +682,7 @@ private Uni<Person> makeSavedPersonDao() {
Dog dog = new Dog("octave", "dalmatian");
dog.owner = person;
person.dogs.add(dog);
return dog.persist().map(v -> person);
return dog.persist().map(d -> person);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.reactivestreams.Publisher
import java.net.URI
import java.time.LocalDate.parse
import javax.annotation.PostConstruct
import javax.ws.rs.Consumes
import javax.ws.rs.DELETE
import javax.ws.rs.GET
import javax.ws.rs.NotFoundException
Expand Down Expand Up @@ -52,19 +51,19 @@ class ReactiveBookEntityResource {

@POST
fun addBook(book: ReactiveBookEntity): Uni<Response> {
return book.persist().map {
return book.persist< ReactiveBookEntity>().map {
//the ID is populated before sending it to the database
Response.created(URI.create("/books/entity${book.id}")).build()
}
}

@PUT
fun updateBook(book: ReactiveBookEntity): Uni<Response> = book.update().map { _ -> Response.accepted().build() }
fun updateBook(book: ReactiveBookEntity): Uni<Response> = book.update<ReactiveBookEntity>().map { Response.accepted().build() }

// PATCH is not correct here but it allows to test persistOrUpdate without a specific subpath
@PATCH
fun upsertBook(book: ReactiveBookEntity): Uni<Response> =
book.persistOrUpdate().map { v -> Response.accepted().build() }
book.persistOrUpdate<ReactiveBookEntity>().map { v -> Response.accepted().build() }

@DELETE
@Path("/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ReactivePersonEntityResource {

@POST
fun addPerson(person: ReactivePersonEntity): Uni<Response> {
return person.persist()
return person.persist<ReactivePersonEntity>()
.map { Response.created(URI.create("/persons/entity${person.id}")).build() }
}

Expand All @@ -52,12 +52,12 @@ class ReactivePersonEntityResource {

@PUT
fun updatePerson(person: ReactivePersonEntity): Uni<Response> =
person.update().map { Response.accepted().build() }
person.update<ReactivePersonEntity>().map { Response.accepted().build() }

// PATCH is not correct here but it allows to test persistOrUpdate without a specific subpath
@PATCH
fun upsertPerson(person: ReactivePersonEntity): Uni<Response> =
person.persistOrUpdate().map { Response.accepted().build() }
person.persistOrUpdate<ReactivePersonEntity>().map { Response.accepted().build() }

@DELETE
@Path("/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ class TestResource {

// regex
val entityWithUpperCase = TestReactiveEntity("title11", "upperCaseCategory", "desc")
entityWithUpperCase.persist().await().indefinitely()
entityWithUpperCase.persist<TestReactiveEntity>().await().indefinitely()
Assertions.assertEquals(1, TestReactiveEntity.list("category like ?1", "upperCase.*")
.await().indefinitely().size)
Assertions.assertEquals(1, TestReactiveEntity.list("category like ?1", "/uppercase.*/i")
Expand All @@ -432,9 +432,9 @@ class TestResource {

// sort
val entityA = TestReactiveEntity("aaa", "aaa", "aaa")
entityA.persist().await().indefinitely()
entityA.persist<TestReactiveEntity>().await().indefinitely()
val entityZ = TestReactiveEntity("zzz", "zzz", "zzz")
entityZ.persistOrUpdate().await().indefinitely()
entityZ.persistOrUpdate<TestReactiveEntity>().await().indefinitely()
var result: TestReactiveEntity = TestReactiveEntity.listAll(Sort.ascending("title")).await()
.indefinitely()[0]
Assertions.assertEquals("aaa", result.title)
Expand All @@ -445,11 +445,11 @@ class TestResource {

// collation
val entityALower = TestReactiveEntity("aaa", "aaa", "aaa")
entityALower.persist().await().indefinitely()
entityALower.persist<TestReactiveEntity>().await().indefinitely()
val entityAUpper = TestReactiveEntity("AAA", "AAA", "AAA")
entityAUpper.persist().await().indefinitely()
entityAUpper.persist<TestReactiveEntity>().await().indefinitely()
val entityB = TestReactiveEntity("BBB", "BBB", "BBB")
entityB.persistOrUpdate().await().indefinitely()
entityB.persistOrUpdate<TestReactiveEntity>().await().indefinitely()
var results: List<TestReactiveEntity> = TestReactiveEntity.listAll(Sort.ascending("title")).await()
.indefinitely()
Assertions.assertEquals("AAA", results[0].title)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ public Publisher<ReactiveBookEntity> streamBooks(@QueryParam("sort") String sort

@POST
public Uni<Response> addBook(ReactiveBookEntity book) {
return book.persist().map(v -> {
//the ID is populated before sending it to the database
String id = book.id.toString();
return Response.created(URI.create("/books/entity" + id)).build();
});
return book.<ReactiveBookEntity> persist().map(v -> Response.created(URI.create("/books/entity" + v.id)).build());
}

@PUT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ public Publisher<Book> streamBooks(@QueryParam("sort") String sort) {

@POST
public Uni<Response> addBook(Book book) {
return reactiveBookRepository.persist(book).map(v -> {
//the ID is populated before sending it to the database
String id = book.getId().toString();
return Response.created(URI.create("/books/entity" + id)).build();
});
return reactiveBookRepository.persist(book)
.map(v -> Response.created(URI.create("/books/entity" + v.getId())).build());
}

@PUT
Expand Down

0 comments on commit 46a6035

Please sign in to comment.