|
4 | 4 | import org.eclipse.microprofile.metrics.annotation.Counted; |
5 | 5 | import org.eclipse.microprofile.metrics.annotation.Timed; |
6 | 6 | import org.eclipse.microprofile.openapi.annotations.Operation; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
7 | 9 |
|
8 | 10 | import javax.annotation.PostConstruct; |
9 | 11 | import javax.transaction.Transactional; |
|
15 | 17 | import javax.ws.rs.Path; |
16 | 18 | import javax.ws.rs.PathParam; |
17 | 19 | import javax.ws.rs.Produces; |
18 | | -import javax.ws.rs.core.Context; |
19 | 20 | import javax.ws.rs.core.MediaType; |
20 | 21 | import javax.ws.rs.core.Response; |
21 | | -import java.net.URI; |
22 | 22 | import java.util.List; |
23 | 23 |
|
24 | 24 | @Path("/bookmarks") |
25 | 25 | @Produces(MediaType.APPLICATION_JSON) |
26 | 26 | @Consumes(MediaType.APPLICATION_JSON) |
27 | 27 | public class BookmarkResource { |
28 | | - @ConfigProperty(name="greeting") String greeting; |
| 28 | + |
| 29 | + private static final Logger LOGGER = LoggerFactory.getLogger(BookmarkResource.class); |
| 30 | + |
| 31 | + @ConfigProperty(name = "greeting") |
| 32 | + private String greeting; |
29 | 33 |
|
30 | 34 | @PostConstruct |
31 | | - void init(){ |
32 | | - System.out.println("Hello " + greeting); |
| 35 | + void init() { |
| 36 | + LOGGER.info("Hello {}", greeting); |
33 | 37 | } |
34 | 38 |
|
| 39 | + |
35 | 40 | @GET |
36 | 41 | @Operation(summary = "List all bookmarks") |
37 | | - @Counted(name = "listAll.count") |
38 | | - @Timed(name="listAll.time") |
39 | | - public List<Bookmark> listAll(){ |
| 42 | + @Counted(name = "listBookmarks.count") |
| 43 | + @Timed(name = "listBookmarks.time") |
| 44 | + public List<Bookmark> listBookmarks() { |
40 | 45 | return Bookmark.listAll(); |
41 | 46 | } |
42 | 47 |
|
43 | 48 | @GET |
44 | | - @Path("/{id}") |
| 49 | + @Path("{id}") |
45 | 50 | @Operation(summary = "Get a bookmark") |
46 | | - @Counted(name = "get.count") |
47 | | - @Timed(name="get.time") |
48 | | - public Bookmark get(@PathParam("id") Long id) { |
| 51 | + @Counted(name = "getBookmark.count") |
| 52 | + @Timed(name = "getBookmark.time") |
| 53 | + public Bookmark getBookmark(@PathParam("id") Long id) { |
49 | 54 | return Bookmark.findById(id); |
50 | 55 | } |
51 | 56 |
|
52 | 57 | @POST |
53 | 58 | @Transactional |
54 | 59 | @Operation(summary = "Create a bookmark") |
55 | | - @Counted(name = "create.count") |
56 | | - @Timed(name="create.time") |
57 | | - public Response create(Bookmark bookmark){ |
| 60 | + @Counted(name = "createBookmark.count") |
| 61 | + @Timed(name = "createBookmark.time") |
| 62 | + public Response createBookmark(Bookmark bookmark) { |
58 | 63 | bookmark.persist(); |
59 | | - return Response.created(URI.create("/bookmarks/" + bookmark.id)).build(); |
| 64 | + return Response.status(Response.Status.CREATED).entity(bookmark).build(); |
60 | 65 | } |
61 | 66 |
|
62 | 67 | @PUT |
63 | 68 | @Path("/{id}") |
64 | 69 | @Transactional |
65 | 70 | @Operation(summary = "Update a bookmark") |
66 | | - @Counted(name = "update.count") |
67 | | - @Timed(name="update.time") |
68 | | - public void update(Bookmark bookmark){ |
69 | | - Bookmark existing = Bookmark.findById(bookmark.id); |
70 | | - existing.url = bookmark.url; |
71 | | - existing.description = bookmark.description; |
72 | | - existing.title = bookmark.title; |
| 71 | + @Counted(name = "updateBookmark.count") |
| 72 | + @Timed(name = "updateBookmark.time") |
| 73 | + public void updateBookmark(Bookmark bookmark, @PathParam("id") Long id) { |
| 74 | + Bookmark entity = Bookmark.findById(id); |
| 75 | + entity.description = bookmark.description; |
| 76 | + entity.location = bookmark.location; |
| 77 | + entity.title = bookmark.title; |
| 78 | + entity.url = bookmark.url; |
73 | 79 | } |
74 | 80 |
|
75 | 81 | @DELETE |
76 | 82 | @Path("/{id}") |
77 | 83 | @Transactional |
78 | 84 | @Operation(summary = "Delete a bookmark") |
79 | | - @Counted(name = "delete.count") |
80 | | - @Timed(name="delete.time") |
81 | | - public void delete(@PathParam("id")Long id){ |
82 | | - Bookmark existing = Bookmark.findById(id); |
83 | | - existing.delete(); |
| 85 | + @Counted(name = "deleteBookmark.count") |
| 86 | + @Timed(name = "deleteBookmark.time") |
| 87 | + public Response deleteBookmark(@PathParam("id") Long id) { |
| 88 | + Bookmark bookmark = Bookmark.findById(id); |
| 89 | + if (bookmark != null) { |
| 90 | + bookmark.delete(); |
| 91 | + } |
| 92 | + return Response.noContent().build(); |
84 | 93 | } |
85 | 94 | } |
0 commit comments