-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend ITs to cover the Mutiny repositories and entities
- Loading branch information
1 parent
12616d1
commit badf83a
Showing
12 changed files
with
883 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...panache/src/main/java/io/quarkus/it/mongodb/panache/reactive/book/ReactiveBookEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package io.quarkus.it.mongodb.panache.reactive.book; | ||
|
||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.json.bind.annotation.JsonbDateFormat; | ||
|
||
import org.bson.codecs.pojo.annotations.BsonIgnore; | ||
import org.bson.codecs.pojo.annotations.BsonProperty; | ||
|
||
import io.quarkus.it.mongodb.panache.book.BookDetail; | ||
import io.quarkus.mongodb.panache.MongoEntity; | ||
import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntity; | ||
|
||
@MongoEntity(collection = "TheBookEntity") | ||
public class ReactiveBookEntity extends ReactivePanacheMongoEntity { | ||
@BsonProperty("bookTitle") | ||
private String title; | ||
private String author; | ||
@BsonIgnore | ||
private String transientDescription; | ||
@JsonbDateFormat("yyyy-MM-dd") | ||
private LocalDate creationDate; | ||
|
||
private List<String> categories = new ArrayList<>(); | ||
|
||
private BookDetail details; | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public ReactiveBookEntity setTitle(String title) { | ||
this.title = title; | ||
return this; | ||
} | ||
|
||
public String getAuthor() { | ||
return author; | ||
} | ||
|
||
public ReactiveBookEntity setAuthor(String author) { | ||
this.author = author; | ||
return this; | ||
} | ||
|
||
public List<String> getCategories() { | ||
return categories; | ||
} | ||
|
||
public ReactiveBookEntity setCategories(List<String> categories) { | ||
this.categories = categories; | ||
return this; | ||
} | ||
|
||
public BookDetail getDetails() { | ||
return details; | ||
} | ||
|
||
public ReactiveBookEntity setDetails(BookDetail details) { | ||
this.details = details; | ||
return this; | ||
} | ||
|
||
public String getTransientDescription() { | ||
return transientDescription; | ||
} | ||
|
||
public void setTransientDescription(String transientDescription) { | ||
this.transientDescription = transientDescription; | ||
} | ||
|
||
public LocalDate getCreationDate() { | ||
return creationDate; | ||
} | ||
|
||
public void setCreationDate(LocalDate creationDate) { | ||
this.creationDate = creationDate; | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
...src/main/java/io/quarkus/it/mongodb/panache/reactive/book/ReactiveBookEntityResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package io.quarkus.it.mongodb.panache.reactive.book; | ||
|
||
import java.net.URI; | ||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.DELETE; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.PATCH; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.PUT; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.bson.types.ObjectId; | ||
import org.jboss.logging.Logger; | ||
import org.jboss.resteasy.annotations.SseElementType; | ||
import org.reactivestreams.Publisher; | ||
|
||
import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntityBase; | ||
import io.quarkus.panache.common.Parameters; | ||
import io.quarkus.panache.common.Sort; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
@Path("/reactive/books/entity") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public class ReactiveBookEntityResource { | ||
private static final Logger LOGGER = Logger.getLogger( | ||
ReactiveBookEntityResource.class); | ||
|
||
@PostConstruct | ||
void init() { | ||
String databaseName = ReactiveBookEntity.mongoDatabase().getName(); | ||
String collectionName = ReactiveBookEntity.mongoCollection().getNamespace().getCollectionName(); | ||
LOGGER.infov("Using BookEntity[database={0}, collection={1}]", databaseName, collectionName); | ||
} | ||
|
||
@GET | ||
public Uni<List<ReactiveBookEntity>> getBooks(@QueryParam("sort") String sort) { | ||
if (sort != null) { | ||
return ReactiveBookEntity.listAll(Sort.ascending(sort)); | ||
} | ||
return ReactiveBookEntity.listAll(); | ||
} | ||
|
||
@GET | ||
@Path("/stream") | ||
@Produces(MediaType.SERVER_SENT_EVENTS) | ||
@SseElementType(MediaType.APPLICATION_JSON) | ||
public Publisher<ReactiveBookEntity> streamBooks(@QueryParam("sort") String sort) { | ||
if (sort != null) { | ||
return ReactiveBookEntity.streamAll(Sort.ascending(sort)); | ||
} | ||
return ReactiveBookEntity.streamAll(); | ||
} | ||
|
||
@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(); | ||
}); | ||
} | ||
|
||
@PUT | ||
public Uni<Response> updateBook(ReactiveBookEntity book) { | ||
return book.update().map(v -> Response.accepted().build()); | ||
} | ||
|
||
// PATCH is not correct here but it allows to test persistOrUpdate without a specific subpath | ||
@PATCH | ||
public Uni<Response> upsertBook(ReactiveBookEntity book) { | ||
return book.persistOrUpdate().map(v -> Response.accepted().build()); | ||
} | ||
|
||
@DELETE | ||
@Path("/{id}") | ||
public Uni<Void> deleteBook(@PathParam("id") String id) { | ||
return ReactiveBookEntity.findById(new ObjectId(id)).flatMap(ReactivePanacheMongoEntityBase::delete); | ||
} | ||
|
||
@GET | ||
@Path("/{id}") | ||
public Uni<ReactiveBookEntity> getBook(@PathParam("id") String id) { | ||
return ReactiveBookEntity.findById(new ObjectId(id)); | ||
} | ||
|
||
@GET | ||
@Path("/search/{author}") | ||
public Uni<List<ReactiveBookEntity>> getBooksByAuthor(@PathParam("author") String author) { | ||
return ReactiveBookEntity.list("author", author); | ||
} | ||
|
||
@GET | ||
@Path("/search") | ||
public Uni<ReactiveBookEntity> search(@QueryParam("author") String author, @QueryParam("title") String title, | ||
@QueryParam("dateFrom") String dateFrom, @QueryParam("dateTo") String dateTo) { | ||
if (author != null) { | ||
return ReactiveBookEntity.find("{'author': ?1,'bookTitle': ?2}", author, title).firstResult(); | ||
} | ||
|
||
return ReactiveBookEntity | ||
.find("{'creationDate': {$gte: ?1}, 'creationDate': {$lte: ?2}}", LocalDate.parse(dateFrom), | ||
LocalDate.parse(dateTo)) | ||
.firstResult(); | ||
} | ||
|
||
@GET | ||
@Path("/search2") | ||
public Uni<ReactiveBookEntity> search2(@QueryParam("author") String author, @QueryParam("title") String title, | ||
@QueryParam("dateFrom") String dateFrom, @QueryParam("dateTo") String dateTo) { | ||
if (author != null) { | ||
return ReactiveBookEntity.find("{'author': :author,'bookTitle': :title}", | ||
Parameters.with("author", author).and("title", title)).firstResult(); | ||
} | ||
|
||
return ReactiveBookEntity.find("{'creationDate': {$gte: :dateFrom}, 'creationDate': {$lte: :dateTo}}", | ||
Parameters.with("dateFrom", LocalDate.parse(dateFrom)).and("dateTo", LocalDate.parse(dateTo))) | ||
.firstResult(); | ||
} | ||
|
||
@DELETE | ||
public Uni<Void> deleteAll() { | ||
return ReactiveBookEntity.deleteAll().map(l -> null); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...che/src/main/java/io/quarkus/it/mongodb/panache/reactive/book/ReactiveBookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.quarkus.it.mongodb.panache.reactive.book; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
import io.quarkus.it.mongodb.panache.book.Book; | ||
import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoRepository; | ||
|
||
@ApplicationScoped | ||
public class ReactiveBookRepository implements ReactivePanacheMongoRepository<Book> { | ||
} |
129 changes: 129 additions & 0 deletions
129
...main/java/io/quarkus/it/mongodb/panache/reactive/book/ReactiveBookRepositoryResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package io.quarkus.it.mongodb.panache.reactive.book; | ||
|
||
import java.net.URI; | ||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.inject.Inject; | ||
import javax.ws.rs.*; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.bson.types.ObjectId; | ||
import org.jboss.logging.Logger; | ||
import org.jboss.resteasy.annotations.SseElementType; | ||
import org.reactivestreams.Publisher; | ||
|
||
import io.quarkus.it.mongodb.panache.book.Book; | ||
import io.quarkus.panache.common.Parameters; | ||
import io.quarkus.panache.common.Sort; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
@Path("/reactive/books/repository") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public class ReactiveBookRepositoryResource { | ||
private static final Logger LOGGER = Logger.getLogger( | ||
ReactiveBookRepositoryResource.class); | ||
@Inject | ||
ReactiveBookRepository reactiveBookRepository; | ||
|
||
@PostConstruct | ||
void init() { | ||
String databaseName = reactiveBookRepository.mongoDatabase().getName(); | ||
String collectionName = reactiveBookRepository.mongoCollection().getNamespace().getCollectionName(); | ||
LOGGER.infov("Using BookRepository[database={0}, collection={1}]", databaseName, collectionName); | ||
} | ||
|
||
@GET | ||
public Uni<List<Book>> getBooks(@QueryParam("sort") String sort) { | ||
if (sort != null) { | ||
return reactiveBookRepository.listAll(Sort.ascending(sort)); | ||
} | ||
return reactiveBookRepository.listAll(); | ||
} | ||
|
||
@GET | ||
@Path("/stream") | ||
@Produces(MediaType.SERVER_SENT_EVENTS) | ||
@SseElementType(MediaType.APPLICATION_JSON) | ||
public Publisher<Book> streamBooks(@QueryParam("sort") String sort) { | ||
if (sort != null) { | ||
return reactiveBookRepository.streamAll(Sort.ascending(sort)); | ||
} | ||
return reactiveBookRepository.streamAll(); | ||
} | ||
|
||
@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(); | ||
}); | ||
} | ||
|
||
@PUT | ||
public Uni<Response> updateBook(Book book) { | ||
return reactiveBookRepository.update(book).map(v -> Response.accepted().build()); | ||
} | ||
|
||
// PATCH is not correct here but it allows to test persistOrUpdate without a specific subpath | ||
@PATCH | ||
public Uni<Response> upsertBook(Book book) { | ||
return reactiveBookRepository.persistOrUpdate(book).map(v -> Response.accepted().build()); | ||
} | ||
|
||
@DELETE | ||
@Path("/{id}") | ||
public Uni<Void> deleteBook(@PathParam("id") String id) { | ||
return reactiveBookRepository.findById(new ObjectId(id)) | ||
.flatMap(book -> reactiveBookRepository.delete(book)); | ||
} | ||
|
||
@GET | ||
@Path("/{id}") | ||
public Uni<Book> getBook(@PathParam("id") String id) { | ||
return reactiveBookRepository.findById(new ObjectId(id)); | ||
} | ||
|
||
@GET | ||
@Path("/search/{author}") | ||
public Uni<List<Book>> getBooksByAuthor(@PathParam("author") String author) { | ||
return reactiveBookRepository.list("author", author); | ||
} | ||
|
||
@GET | ||
@Path("/search") | ||
public Uni<Book> search(@QueryParam("author") String author, @QueryParam("title") String title, | ||
@QueryParam("dateFrom") String dateFrom, @QueryParam("dateTo") String dateTo) { | ||
if (author != null) { | ||
return reactiveBookRepository.find("{'author': ?1,'bookTitle': ?2}", author, title).firstResult(); | ||
} | ||
|
||
return reactiveBookRepository | ||
.find("{'creationDate': {$gte: ?1}, 'creationDate': {$lte: ?2}}", LocalDate.parse(dateFrom), | ||
LocalDate.parse(dateTo)) | ||
.firstResult(); | ||
} | ||
|
||
@GET | ||
@Path("/search2") | ||
public Uni<Book> search2(@QueryParam("author") String author, @QueryParam("title") String title, | ||
@QueryParam("dateFrom") String dateFrom, @QueryParam("dateTo") String dateTo) { | ||
if (author != null) { | ||
return reactiveBookRepository.find("{'author': :author,'bookTitle': :title}", | ||
Parameters.with("author", author).and("title", title)).firstResult(); | ||
} | ||
|
||
return reactiveBookRepository.find("{'creationDate': {$gte: :dateFrom}, 'creationDate': {$lte: :dateTo}}", | ||
Parameters.with("dateFrom", LocalDate.parse(dateFrom)).and("dateTo", LocalDate.parse(dateTo))) | ||
.firstResult(); | ||
} | ||
|
||
@DELETE | ||
public Uni<Void> deleteAll() { | ||
return reactiveBookRepository.deleteAll().map(l -> null); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...che/src/main/java/io/quarkus/it/mongodb/panache/reactive/person/ReactivePersonEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.quarkus.it.mongodb.panache.reactive.person; | ||
|
||
import org.bson.codecs.pojo.annotations.BsonId; | ||
|
||
import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntityBase; | ||
|
||
public class ReactivePersonEntity extends ReactivePanacheMongoEntityBase { | ||
@BsonId | ||
public Long id; | ||
public String firstname; | ||
public String lastname; | ||
} |
Oops, something went wrong.