Skip to content

Commit

Permalink
MongoDB with Panache deleteById()
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Mar 17, 2020
1 parent f415210 commit 0e65e41
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 8 deletions.
9 changes: 9 additions & 0 deletions docs/src/main/asciidoc/mongodb-panache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ Person.delete("status", Status.Alive);
// delete all persons
Person.deleteAll();
// delete by id
boolean deleted = Person.deleteById(personId);
----

All `list` methods have equivalent `stream` versions.
Expand Down Expand Up @@ -378,6 +381,9 @@ personRepository.delete("status", Status.Alive);
// delete all persons
personRepository.deleteAll();
// delete by id
boolean deleted = personRepository.deleteById(personId);
----

All `list` methods have equivalent `stream` versions.
Expand Down Expand Up @@ -771,6 +777,9 @@ Uni<Long> deleteCount = ReactivePerson.delete("status", Status.Alive);
// delete all persons
deleteCount = ReactivePerson.deleteAll();
// delete by id
Uni<Boolean> deleted = ReactivePerson.deleteById(personId);
----

TIP: If you use MongoDB with Panache in conjunction with RESTEasy, you can directly return a reactive type inside your JAX-RS resource endpoint as long as you include the `quarkus-resteasy-mutiny` extension.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ public static long deleteAll() {
* Delete an entity of this type by ID.
*
* @param id the ID of the entity to delete.
* @return false if the entity is not delete (not found).
* @return false if the entity was not deleted (not found).
*/
@GenerateBridge
public static boolean deleteById(Object id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ public default long deleteAll() {
* Delete an entity of this type by ID.
*
* @param id the ID of the entity to delete.
* @return false if the entity is not deleted (not found).
* @return false if the entity was not deleted (not found).
*/
@GenerateBridge
public default boolean deleteById(Id id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,17 @@ public static long deleteAll() {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Delete an entity of this type by ID.
*
* @param id the ID of the entity to delete.
* @return false if the entity was not deleted (not found).
*/
@GenerateBridge
public static boolean deleteById(Object id) {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Delete all entities of this type matching the given query, with optional indexed parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,17 @@ public default long deleteAll() {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Delete an entity of this type by ID.
*
* @param id the ID of the entity to delete.
* @return false if the entity was not deleted (not found).
*/
@GenerateBridge
public default boolean deleteById(Id id) {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Delete all entities of this type matching the given query, with optional indexed parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bson.Document;

import io.quarkus.mongodb.panache.reactive.runtime.ReactiveMongoOperations;
import io.quarkus.mongodb.panache.runtime.MongoOperations;
import io.quarkus.mongodb.reactive.ReactiveMongoCollection;
import io.quarkus.mongodb.reactive.ReactiveMongoDatabase;
import io.quarkus.panache.common.Parameters;
Expand Down Expand Up @@ -707,6 +708,17 @@ public static Uni<Long> deleteAll() {
throw ReactiveMongoOperations.implementationInjectionMissing();
}

/**
* Delete an entity of this type by ID.
*
* @param id the ID of the entity to delete.
* @return false if the entity was not deleted (not found).
*/
@GenerateBridge
public static Uni<Boolean> deleteById(Object id) {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Delete all entities of this type matching the given query, with optional indexed parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bson.Document;

import io.quarkus.mongodb.panache.reactive.runtime.ReactiveMongoOperations;
import io.quarkus.mongodb.panache.runtime.MongoOperations;
import io.quarkus.mongodb.reactive.ReactiveMongoCollection;
import io.quarkus.mongodb.reactive.ReactiveMongoDatabase;
import io.quarkus.panache.common.Parameters;
Expand Down Expand Up @@ -703,6 +704,17 @@ public default Uni<Long> deleteAll() {
throw ReactiveMongoOperations.implementationInjectionMissing();
}

/**
* Delete an entity of this type by ID.
*
* @param id the ID of the entity to delete.
* @return false if the entity was not deleted (not found).
*/
@GenerateBridge
public default Uni<Boolean> deleteById(Id id) {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Delete all entities of this type matching the given query, with optional indexed parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,12 @@ public static Uni<Long> deleteAll(Class<?> entityClass) {
return collection.deleteMany(new Document()).map(deleteResult -> deleteResult.getDeletedCount());
}

public static Uni<Boolean> deleteById(Class<?> entityClass, Object id) {
ReactiveMongoCollection<?> collection = mongoCollection(entityClass);
Document query = new Document().append(ID, id);
return collection.deleteOne(query).map(results -> results.getDeletedCount() == 1);
}

public static Uni<Long> delete(Class<?> entityClass, String query, Object... params) {
String bindQuery = bindQuery(entityClass, query, params);
Document docQuery = Document.parse(bindQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.mongodb.client.model.ReplaceOptions;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.WriteModel;
import com.mongodb.client.result.DeleteResult;

import io.quarkus.arc.Arc;
import io.quarkus.mongodb.panache.MongoEntity;
Expand Down Expand Up @@ -522,6 +523,13 @@ public static long deleteAll(Class<?> entityClass) {
return collection.deleteMany(new Document()).getDeletedCount();
}

public static boolean deleteById(Class<?> entityClass, Object id) {
MongoCollection collection = mongoCollection(entityClass);
Document query = new Document().append(ID, id);
DeleteResult results = collection.deleteOne(query);
return results.getDeletedCount() == 1;
}

public static long delete(Class<?> entityClass, String query, Object... params) {
String bindQuery = bindQuery(entityClass, query, params);
Document docQuery = Document.parse(bindQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ public Response upsertBook(BookEntity book) {
@DELETE
@Path("/{id}")
public void deleteBook(@PathParam("id") String id) {
BookEntity theBook = BookEntity.findById(new ObjectId(id));
theBook.delete();
boolean deleted = BookEntity.deleteById(new ObjectId(id));
if (!deleted) {
throw new NotFoundException();
}
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ public Response upsertBook(Book book) {
@DELETE
@Path("/{id}")
public void deleteBook(@PathParam("id") String id) {
Book theBook = bookRepository.findById(new ObjectId(id));
bookRepository.delete(theBook);
boolean deleted = bookRepository.deleteById(new ObjectId(id));
if (!deleted) {
throw new NotFoundException();
}
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ public Uni<Response> upsertBook(ReactiveBookEntity book) {
@DELETE
@Path("/{id}")
public Uni<Void> deleteBook(@PathParam("id") String id) {
return ReactiveBookEntity.findById(new ObjectId(id)).flatMap(book -> book.delete());
return ReactiveBookEntity.deleteById(new ObjectId(id))
.map(d -> {
if (d) {
return null;
}
throw new NotFoundException();
});
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ public Uni<Response> upsertBook(Book book) {
@DELETE
@Path("/{id}")
public Uni<Void> deleteBook(@PathParam("id") String id) {
return reactiveBookRepository.findById(new ObjectId(id)).flatMap(book -> reactiveBookRepository.delete(book));
return reactiveBookRepository.deleteById(new ObjectId(id))
.map(d -> {
if (d) {
return null;
}
throw new NotFoundException();
});
}

@GET
Expand Down

0 comments on commit 0e65e41

Please sign in to comment.