-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create quarkus jax-rs services
Signed-off-by: Otavio Santana <[email protected]>
- Loading branch information
1 parent
802b0e5
commit 3fb5b78
Showing
4 changed files
with
90 additions
and
5 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
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
37
chapter-10/src/main/java/expert/os/books/FishDatabase.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,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
48
chapter-10/src/main/java/expert/os/books/FishResource.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,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); | ||
} | ||
} |