Skip to content

Commit b16a8e0

Browse files
committed
feat: create resource for tool
Signed-off-by: Otavio Santana <[email protected]>
1 parent 1bd3e21 commit b16a8e0

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed
Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,71 @@
1-
package expert.os.samples.helidon.mongodb;public class ToolResource {
1+
package expert.os.samples.helidon.mongodb;
2+
3+
import jakarta.data.Direction;
4+
import jakarta.data.Order;
5+
import jakarta.data.Sort;
6+
import jakarta.data.page.PageRequest;
7+
import jakarta.inject.Inject;
8+
import jakarta.ws.rs.Consumes;
9+
import jakarta.ws.rs.DELETE;
10+
import jakarta.ws.rs.DefaultValue;
11+
import jakarta.ws.rs.GET;
12+
import jakarta.ws.rs.POST;
13+
import jakarta.ws.rs.Path;
14+
import jakarta.ws.rs.PathParam;
15+
import jakarta.ws.rs.Produces;
16+
import jakarta.ws.rs.QueryParam;
17+
import jakarta.ws.rs.WebApplicationException;
18+
import jakarta.ws.rs.core.MediaType;
19+
import jakarta.ws.rs.core.Response;
20+
21+
import java.util.List;
22+
import java.util.logging.Logger;
23+
24+
@Path("/tools")
25+
@Produces(MediaType.APPLICATION_JSON)
26+
@Consumes(MediaType.APPLICATION_JSON)
27+
public class ToolResource {
28+
29+
private static final Logger LOGGER = Logger.getLogger(ToolResource.class.getName());
30+
31+
private final ToolRepository toolRepository;
32+
33+
@Inject
34+
public ToolResource(ToolRepository toolRepository) {
35+
this.toolRepository = toolRepository;
36+
}
37+
38+
39+
@GET
40+
public List<Tool> findAllTools(@QueryParam("page") @DefaultValue("1") long page,
41+
@QueryParam("size") @DefaultValue("2") int size,
42+
@QueryParam("order") @DefaultValue("sku") String order,
43+
@QueryParam("direction") @DefaultValue("ASC") Direction direction) {
44+
45+
LOGGER.info("Find all tools, page: " + page + ", size: " + size + ", order: " + order + ", direction: " + direction);
46+
var pageRequest = PageRequest.ofPage(page).size(size);
47+
return toolRepository.findAll(pageRequest, Order.by(Sort.of(order, direction, false))).content();
48+
}
49+
50+
@POST
51+
public void save(Tool tool) {
52+
LOGGER.info("Save tool: " + tool);
53+
toolRepository.save(tool);
54+
}
55+
56+
@GET
57+
@Path("/{id}")
58+
public Tool findById(@PathParam("id") String id) {
59+
LOGGER.info("Find tool by id: " + id);
60+
return toolRepository.findById(id).orElseThrow(() -> new WebApplicationException("Tool not found", Response.Status.NOT_FOUND));
61+
}
62+
63+
@DELETE
64+
@Path("/{id}")
65+
public void deleteById(@PathParam("id") String id) {
66+
LOGGER.info("Delete tool by id: " + id);
67+
toolRepository.deleteById(id);
68+
}
69+
70+
271
}

src/main/resources/META-INF/microprofile-config.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ app.greeting=Hello
1212
# configure the MongoDB client for a replica set of two nodes
1313
jnosql.mongodb.url=mongodb://localhost:27017
1414
# mandatory define the database name
15-
jnosql.document.database = fruits
15+
jnosql.document.database =tools

0 commit comments

Comments
 (0)