From 669d6f98bf26252086d42961d2efa583ed4f5eca Mon Sep 17 00:00:00 2001 From: Keerthi B L Date: Tue, 30 May 2023 14:50:55 +0530 Subject: [PATCH] feat(rest) : api to get count of projects Signed-off-by: Keerthi B L --- .../src/docs/asciidoc/projects.adoc | 14 ++++++++++++++ .../project/ProjectController.java | 18 ++++++++++++++++++ .../restdocs/ProjectSpecTest.java | 15 +++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/rest/resource-server/src/docs/asciidoc/projects.adoc b/rest/resource-server/src/docs/asciidoc/projects.adoc index ebe757be91..000ca7f2e2 100644 --- a/rest/resource-server/src/docs/asciidoc/projects.adoc +++ b/rest/resource-server/src/docs/asciidoc/projects.adoc @@ -650,3 +650,17 @@ include::{snippets}/should_document_import_sbom/curl-request.adoc[] ===== Example response include::{snippets}/should_document_import_sbom/http-response.adoc[] + +[[resources-project-getprojectcount]] +==== Get project count of a user + +A `GET` request will get project count of a user. + +===== Example request +include::{snippets}/should_document_get_project_count/curl-request.adoc[] + +===== Response structure +include::{snippets}/should_document_get_project_count/response-fields.adoc[] + +===== Example response +include::{snippets}/should_document_get_project_count/http-response.adoc[] diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java index 4529e50140..f2048fcf94 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java @@ -17,6 +17,8 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import com.google.gson.JsonObject; + import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.apache.commons.lang.StringUtils; @@ -39,7 +41,9 @@ import org.eclipse.sw360.datahandler.thrift.ReleaseRelationship; import org.eclipse.sw360.datahandler.thrift.RequestStatus; import org.eclipse.sw360.datahandler.thrift.RequestSummary; +import org.eclipse.sw360.datahandler.thrift.SW360Exception; import org.eclipse.sw360.datahandler.thrift.Source; +import org.eclipse.sw360.datahandler.thrift.ThriftClients; import org.eclipse.sw360.datahandler.thrift.attachments.Attachment; import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent; import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentType; @@ -58,6 +62,7 @@ import org.eclipse.sw360.datahandler.thrift.projects.ProjectClearingState; import org.eclipse.sw360.datahandler.thrift.projects.ProjectLink; import org.eclipse.sw360.datahandler.thrift.projects.ProjectProjectRelationship; +import org.eclipse.sw360.datahandler.thrift.projects.ProjectService; import org.eclipse.sw360.datahandler.thrift.users.User; import org.eclipse.sw360.datahandler.thrift.vendors.Vendor; import org.eclipse.sw360.datahandler.thrift.vulnerabilities.ProjectVulnerabilityRating; @@ -1209,4 +1214,17 @@ public static TSerializer getJsonSerializer() { } return null; } + + @RequestMapping(value = PROJECTS_URL + "/projectcount", method = RequestMethod.GET) + public void getUserProjectCount(HttpServletResponse response) throws TException { + User sw360User = restControllerHelper.getSw360UserFromAuthentication(); + try { + JsonObject resultJson = new JsonObject(); + resultJson.addProperty("status", "success"); + resultJson.addProperty("count", projectService.getMyAccessibleProjectCounts(sw360User)); + response.getWriter().write(resultJson.toString()); + }catch (IOException e) { + throw new SW360Exception(e.getMessage()); + } + } } diff --git a/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ProjectSpecTest.java b/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ProjectSpecTest.java index 5f8c6bb200..87aff92c57 100644 --- a/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ProjectSpecTest.java +++ b/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ProjectSpecTest.java @@ -1666,4 +1666,19 @@ public void should_document_import_sbom() throws Exception { .queryParam("type", "SPDX"); this.mockMvc.perform(builder).andExpect(status().isOk()).andDo(this.documentationHandler.document()); } + + @Test + public void should_document_get_project_count() throws Exception { + String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword); + this.mockMvc.perform(get("/api/projects/projectcount") + .header("Authorization", "Bearer " + accessToken) + .accept(MediaTypes.HAL_JSON) + .contentType(MediaTypes.HAL_JSON)) + .andExpect(status().isOk()) + .andDo(this.documentationHandler.document( + responseFields( + fieldWithPath("status").description("status of the API. Possible values are ``").optional(), + fieldWithPath("count").description("Count of projects for a user.").optional() + ))); + } }