diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateChangelogApi.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateChangelogApi.kt index 7b553819f..5b3bcddbe 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateChangelogApi.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateChangelogApi.kt @@ -58,7 +58,7 @@ interface GateChangelogApi { fun getChangelogEntriesExternalId( @ParameterObject @Valid paginationRequest: PaginationRequest, @Parameter(description = "From Time", example = "2023-03-20T10:23:28.194Z") @RequestParam(required = false) fromTime: Instant?, - @RequestBody(required = true) @NotEmpty(message = "Input externalIds list cannot be empty.") externalIds: Collection + @RequestBody(required = true) @NotEmpty(message = "Input externalIds list cannot be empty.") externalIds: Set ): PageChangeLogResponse @Operation( diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/ChangelogController.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/ChangelogController.kt index deb2391d8..5658836f8 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/ChangelogController.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/ChangelogController.kt @@ -37,7 +37,7 @@ class ChangelogController( ) : GateChangelogApi { override fun getChangelogEntriesExternalId( - paginationRequest: PaginationRequest, fromTime: Instant?, externalIds: Collection + paginationRequest: PaginationRequest, fromTime: Instant?, externalIds: Set ): PageChangeLogResponse { return changelogService.getChangeLogByExternalId(externalIds, fromTime, paginationRequest.page, paginationRequest.size) } diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/ChangelogRepository.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/ChangelogRepository.kt index aade6279e..9f2a39964 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/ChangelogRepository.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/ChangelogRepository.kt @@ -19,11 +19,13 @@ package org.eclipse.tractusx.bpdm.gate.repository +import io.swagger.v3.oas.annotations.Parameter import org.eclipse.tractusx.bpdm.gate.api.model.response.LsaType import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntity import org.springframework.data.jpa.domain.Specification import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.JpaSpecificationExecutor +import org.springframework.data.jpa.repository.Query import java.time.Instant @@ -64,5 +66,8 @@ interface ChangelogRepository : JpaRepository, JpaSpecifi } } + @Query("select distinct u.externalId from ChangelogEntity u where u.externalId in :externalIdList") + fun findExternalIdsInListDistinct(externalIdList: Collection): Set + } diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ChangelogService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ChangelogService.kt index 7eac1845d..fbf954423 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ChangelogService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ChangelogService.kt @@ -29,42 +29,45 @@ import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository.Specs.byCreatedAtGreaterThan import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository.Specs.byExternalIdsIn import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository.Specs.byLsaType +import org.springframework.data.domain.PageImpl import org.springframework.data.domain.PageRequest import org.springframework.data.jpa.domain.Specification import org.springframework.stereotype.Service + import java.time.Instant +import kotlin.math.ceil @Service class ChangelogService(private val changelogRepository: ChangelogRepository) { - fun getChangeLogByExternalId(externalIds: Collection, createdAt: Instant?, page: Int, pageSize: Int): PageChangeLogResponse { + fun getChangeLogByExternalId(externalIds: Set, createdAt: Instant?, page: Int, pageSize: Int): PageChangeLogResponse { val spec = Specification.allOf(byExternalIdsIn(externalIds = externalIds), byCreatedAtGreaterThan(createdAt = createdAt)) val pageable = PageRequest.of(page, pageSize) val pageResponse = changelogRepository.findAll(spec, pageable) + val setDistinctList = changelogRepository.findExternalIdsInListDistinct(externalIds) val pageDto = pageResponse.map { it.toGateDto() } - val errorInfoSet = externalIds.filterNot { id -> - pageDto.content.any { it.externalId == id } - }.map { + val errorList = (externalIds - setDistinctList).map { ErrorInfo( ChangeLogOutputError.ExternalIdNotFound, "$it not found", it ) - }.toSet() + } + return PageChangeLogResponse( page = page, totalElements = pageDto.totalElements, totalPages = pageDto.totalPages, contentSize = pageDto.content.size, content = pageDto.content, - invalidEntries = errorInfoSet.size, - errors = errorInfoSet + invalidEntries = errorList.size, + errors = errorList ) } @@ -86,4 +89,5 @@ class ChangelogService(private val changelogRepository: ChangelogRepository) { content = pageDto.content, ) } + } \ No newline at end of file diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/ChangeLogControllerIT.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/ChangeLogControllerIT.kt index bcbd4a915..c394214c2 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/ChangeLogControllerIT.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/ChangeLogControllerIT.kt @@ -116,7 +116,7 @@ internal class ChangeLogControllerIT @Autowired constructor( @Test fun `get changeLog by external id`() { - val searchResult = gateClient.changelog().getChangelogEntriesExternalId(PaginationRequest(), null, listOf(CommonValues.externalIdAddress1)) + val searchResult = gateClient.changelog().getChangelogEntriesExternalId(PaginationRequest(), null, setOf(CommonValues.externalIdAddress1)) assertRecursively(searchResult.content) .ignoringFieldsMatchingRegexes(".*${ChangelogResponse::modifiedAt.name}") @@ -133,7 +133,7 @@ internal class ChangeLogControllerIT @Autowired constructor( @Test fun `get changeLog by external id not found`() { - val searchResult = gateClient.changelog().getChangelogEntriesExternalId(PaginationRequest(), null, listOf("NONEXIST")) + val searchResult = gateClient.changelog().getChangelogEntriesExternalId(PaginationRequest(), null, setOf("NONEXIST")) assertThat(searchResult.content) .usingRecursiveComparison() @@ -164,7 +164,7 @@ internal class ChangeLogControllerIT @Autowired constructor( @Test fun `get changeLog by external id and timeStamp`() { - val searchResult = gateClient.changelog().getChangelogEntriesExternalId(PaginationRequest(), instant, listOf(CommonValues.externalIdAddress1)) + val searchResult = gateClient.changelog().getChangelogEntriesExternalId(PaginationRequest(), instant, setOf(CommonValues.externalIdAddress1)) assertRecursively(searchResult.content).ignoringFieldsMatchingRegexes(".*${ChangelogResponse::modifiedAt.name}")