-
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.
Signed-off-by: Otavio Santana <[email protected]>
- Loading branch information
1 parent
1bd3e21
commit b16a8e0
Showing
2 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
71 changes: 70 additions & 1 deletion
71
src/main/java/expert/os/samples/helidon/mongodb/ToolResource.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 |
---|---|---|
@@ -1,2 +1,71 @@ | ||
package expert.os.samples.helidon.mongodb;public class ToolResource { | ||
package expert.os.samples.helidon.mongodb; | ||
|
||
import jakarta.data.Direction; | ||
import jakarta.data.Order; | ||
import jakarta.data.Sort; | ||
import jakarta.data.page.PageRequest; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.DELETE; | ||
import jakarta.ws.rs.DefaultValue; | ||
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.QueryParam; | ||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
@Path("/tools") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public class ToolResource { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(ToolResource.class.getName()); | ||
|
||
private final ToolRepository toolRepository; | ||
|
||
@Inject | ||
public ToolResource(ToolRepository toolRepository) { | ||
this.toolRepository = toolRepository; | ||
} | ||
|
||
|
||
@GET | ||
public List<Tool> findAllTools(@QueryParam("page") @DefaultValue("1") long page, | ||
@QueryParam("size") @DefaultValue("2") int size, | ||
@QueryParam("order") @DefaultValue("sku") String order, | ||
@QueryParam("direction") @DefaultValue("ASC") Direction direction) { | ||
|
||
LOGGER.info("Find all tools, page: " + page + ", size: " + size + ", order: " + order + ", direction: " + direction); | ||
var pageRequest = PageRequest.ofPage(page).size(size); | ||
return toolRepository.findAll(pageRequest, Order.by(Sort.of(order, direction, false))).content(); | ||
} | ||
|
||
@POST | ||
public void save(Tool tool) { | ||
LOGGER.info("Save tool: " + tool); | ||
toolRepository.save(tool); | ||
} | ||
|
||
@GET | ||
@Path("/{id}") | ||
public Tool findById(@PathParam("id") String id) { | ||
LOGGER.info("Find tool by id: " + id); | ||
return toolRepository.findById(id).orElseThrow(() -> new WebApplicationException("Tool not found", Response.Status.NOT_FOUND)); | ||
} | ||
|
||
@DELETE | ||
@Path("/{id}") | ||
public void deleteById(@PathParam("id") String id) { | ||
LOGGER.info("Delete tool by id: " + id); | ||
toolRepository.deleteById(id); | ||
} | ||
|
||
|
||
} |
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