Skip to content

Commit

Permalink
Add a resource exemple to the MongoDB and Hibernate OMR guides
Browse files Browse the repository at this point in the history
Fixes quarkusio#10927

(cherry picked from commit c3073c7)
  • Loading branch information
loicmathieu authored and gsmet committed Jun 22, 2021
1 parent 4420b9d commit 1ff213a
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
74 changes: 74 additions & 0 deletions docs/src/main/asciidoc/hibernate-orm-panache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,80 @@ NOTE: The rest of the documentation show usages based on the active record patte
but keep in mind that they can be performed with the repository pattern as well.
The repository pattern examples have been omitted for brevity.

== Writing a JAX-RS resource

First, include one of the RESTEasy extensions to enable JAX-RS endpoints, for example, add the `io.quarkus:quarkus-resteasy-jackson` dependency for JAX-RS and JSON support.

Then, you can create the following resource to create/read/update/delete your Person entity:

[source,java]
----
@Path("/persons")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class PersonResource {
@GET
public List<Person> list() {
return Person.listAll();
}
@GET
@Path("/{id}")
public Person get(@PathParam("id") Long id) {
return Person.findById(id);
}
@POST
@Transactional
public Response create(Person person) {
person.persist();
return Response.created(URI.create("/persons/" + person.id)).build();
}
@PUT
@Path("/{id}")
@Transactional
public Person update(@PathParam("id") Long id, Person person) {
Person entity = Person.findById(id);
if(entity == null) {
throw new NotFoundException();
}
// map all fields from the person parameter to the existing entity
entity.name = person.name;
return entity;
}
@DELETE
@Path("/{id}")
@Transactional
public void delete(@PathParam("id") Long id) {
Person entity = Person.findById(id);
if(entity == null) {
throw new NotFoundException();
}
entity.delete();
}
@GET
@Path("/search/{name}")
public Person search(@PathParam("name") String name) {
return Person.findByName(name);
}
@GET
@Path("/count")
public Long count() {
return Person.count();
}
}
----

NOTE: Be careful to use the `@Transactional` annotation on the operations that modify the database,
you can add the annotation at the class level for simplicity purpose.

== Advanced Query

=== Paging
Expand Down
59 changes: 59 additions & 0 deletions docs/src/main/asciidoc/mongodb-panache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,65 @@ NOTE: The rest of the documentation show usages based on the active record patte
but keep in mind that they can be performed with the repository pattern as well.
The repository pattern examples have been omitted for brevity.

== Writing a JAX-RS resource

First, include one of the RESTEasy extensions to enable JAX-RS endpoints, for example, add the `io.quarkus:quarkus-resteasy-jackson` dependency for JAX-RS and JSON support.

Then, you can create the following resource to create/read/update/delete your Person entity:

[source,java]
----
@Path("/persons")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class PersonResource {
@GET
public List<Person> list() {
return Person.listAll();
}
@GET
@Path("/{id}")
public Person get(@PathParam("id") String id) {
return Person.findById(new ObjectId(id));
}
@POST
public Response create(Person person) {
person.persist();
return Response.created(URI.create("/persons/" + person.id)).build();
}
@PUT
@Path("/{id}")
public void update(@PathParam("id") String id, Person person) {
person.update();
}
@DELETE
@Path("/{id}")
public void delete(@PathParam("id") String id) {
Person person = Person.findById(new ObjectId(id));
if(person == null) {
throw new NotFoundException();
}
person.delete();
}
@GET
@Path("/search/{name}")
public Person search(@PathParam("name") String name) {
return Person.findByName(name);
}
@GET
@Path("/count")
public Long count() {
return Person.count();
}
}
----

== Advanced Query

Expand Down

0 comments on commit 1ff213a

Please sign in to comment.