From 794b87ffa46cfa6965d084bc9fa2c74cc540f898 Mon Sep 17 00:00:00 2001 From: duonglq-tsdv Date: Mon, 30 Sep 2024 17:03:22 +0700 Subject: [PATCH] feat(Component): Add new endpoint that allows user to subscribe and unsubscribe to a component Signed-off-by: hoangnt2 --- .../component/ComponentController.java | 25 +++++++++++++++++++ .../component/Sw360ComponentService.java | 10 ++++++++ .../restdocs/ComponentSpecTest.java | 9 ++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/component/ComponentController.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/component/ComponentController.java index 772b631d34..99567ebecc 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/component/ComponentController.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/component/ComponentController.java @@ -318,6 +318,31 @@ public ResponseEntity>> getMySubscription return new ResponseEntity<>(finalResources, status); } + @Operation( + summary = "Toggle user subscription to a component", + description = "Subscribes or unsubscribes the user to a specified component based on their current subscription status.", + tags = {"Components"} + ) + @RequestMapping(value = COMPONENTS_URL + "/{id}/subscriptions", method = RequestMethod.POST) + public ResponseEntity toggleComponentSubscription( + @Parameter(description = "The ID of the component.") + @PathVariable("id") String componentId + ) throws TException { + User user = restControllerHelper.getSw360UserFromAuthentication(); + Component componentById = componentService.getComponentForUserById(componentId, user); + Set subscribers = componentById.getSubscribers(); + + boolean isSubscribed = subscribers.contains(user.getEmail()); + + if (isSubscribed) { + componentService.unsubscribeComponent(componentId, user); + return new ResponseEntity<>("Successfully unsubscribed from the component.", HttpStatus.OK); + } else { + componentService.subscribeComponent(componentId, user); + return new ResponseEntity<>("Successfully subscribed to the component.", HttpStatus.OK); + } + } + @Operation( summary = "Get components by external ID.", description = "Get components by external ID.", diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/component/Sw360ComponentService.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/component/Sw360ComponentService.java index c32b7cbf63..6662ac2a6b 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/component/Sw360ComponentService.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/component/Sw360ComponentService.java @@ -102,6 +102,16 @@ public List getComponentSubscriptions(User sw360User) throws TExcepti return sw360ComponentClient.getSubscribedComponents(sw360User); } + public RequestStatus subscribeComponent(String componentId, User sw360User) throws TException { + ComponentService.Iface sw360ComponentClient = getThriftComponentClient(); + return sw360ComponentClient.subscribeComponent(componentId, sw360User); + } + + public RequestStatus unsubscribeComponent(String componentId, User sw360User) throws TException { + ComponentService.Iface sw360ComponentClient = getThriftComponentClient(); + return sw360ComponentClient.unsubscribeComponent(componentId, sw360User); + } + public List getRecentComponents(User sw360User) throws TException { ComponentService.Iface sw360ComponentClient = getThriftComponentClient(); return sw360ComponentClient.getRecentComponentsSummary(5, sw360User); diff --git a/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ComponentSpecTest.java b/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ComponentSpecTest.java index db9c985125..384f3ea114 100644 --- a/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ComponentSpecTest.java +++ b/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ComponentSpecTest.java @@ -34,7 +34,6 @@ import org.eclipse.sw360.rest.resourceserver.attachment.Sw360AttachmentService; import org.eclipse.sw360.rest.resourceserver.component.Sw360ComponentService; import org.eclipse.sw360.rest.resourceserver.report.SW360ReportService; -import org.eclipse.sw360.rest.resourceserver.user.Sw360UserService; import org.eclipse.sw360.rest.resourceserver.vulnerability.Sw360VulnerabilityService; import org.eclipse.sw360.rest.resourceserver.vendor.Sw360VendorService; import org.hamcrest.Matchers; @@ -1345,4 +1344,12 @@ public void should_document_get_component_report() throws Exception{ + "Possible values are ``") ))); } + + @Test + public void should_subscribe_user_to_component() throws Exception { + mockMvc.perform(post("/api/components/" + angularComponent.getId() + "/subscriptions") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", TestHelper.generateAuthHeader(testUserId, testUserPassword))) + .andExpect(status().isOk()); + } }