Skip to content

Commit

Permalink
feat(Component): Add new endpoint that allows user to subscribe and u…
Browse files Browse the repository at this point in the history
…nsubscribe to a component

Signed-off-by: hoangnt2 <[email protected]>
  • Loading branch information
duonglq-tsdv authored and hoangnt2 committed Nov 22, 2024
1 parent c4b75cf commit 505ede8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,31 @@ public ResponseEntity<CollectionModel<EntityModel<Component>>> 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<String> 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<String> 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.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ public List<Component> 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<Component> getRecentComponents(User sw360User) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
return sw360ComponentClient.getRecentComponentsSummary(5, sw360User);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1345,4 +1344,12 @@ public void should_document_get_component_report() throws Exception{
+ "Possible values are `<true|false>`")
)));
}

@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());
}
}

0 comments on commit 505ede8

Please sign in to comment.