Skip to content

Commit

Permalink
feat: create quarkus jax-rs services
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <[email protected]>
  • Loading branch information
otaviojava committed Aug 26, 2023
1 parent 802b0e5 commit 3fb5b78
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
6 changes: 1 addition & 5 deletions chapter-10/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jsonb</artifactId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand All @@ -48,10 +48,6 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions chapter-10/src/main/java/expert/os/books/Fish.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package expert.os.books;

public record Fish(String id, String name, String color) {
}
37 changes: 37 additions & 0 deletions chapter-10/src/main/java/expert/os/books/FishDatabase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package expert.os.books;

import jakarta.enterprise.context.ApplicationScoped;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@ApplicationScoped
public class FishDatabase {

private Map<String, Fish> fishes;

public FishDatabase() {
this.fishes = new HashMap<>();
}

public void save(Fish fish) {
this.fishes.put(fish.id(), fish);
}

public void delete(String id) {
this.fishes.remove(id);
}

public Optional<Fish> findById(String id) {
return Optional.ofNullable(this.fishes.get(id));
}

public void clear() {
this.fishes.clear();
}
public List<Fish> findAll() {
return List.copyOf(this.fishes.values());
}
}
48 changes: 48 additions & 0 deletions chapter-10/src/main/java/expert/os/books/FishResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package expert.os.books;


import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import java.util.List;

@Path("/fishes")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class FishResource {

@Inject
FishDatabase database;

@GET
public List<Fish> findAll() {
return this.database.findAll();
}

@POST
public void insert(Fish fish) {
this.database.save(fish);
}

@GET
@Path("/{id}")
public Fish findById(@PathParam("id") String id) {
return this.database.findById(id)
.orElseThrow(() -> new WebApplicationException("Fish not found", Response.Status.NOT_FOUND));
}

@DELETE
@Path("/{id}")
public void deleteById(@PathParam("id") String id) {
this.database.delete(id);
}
}

0 comments on commit 3fb5b78

Please sign in to comment.