Skip to content

Commit

Permalink
Merge pull request #511 from catenax-ng/feat/administrative-areas-level1
Browse files Browse the repository at this point in the history
Feat: Administrative Level 1 endpoint - don't call it "region" in descriptions and properties
  • Loading branch information
nicoprow authored Oct 11, 2023
2 parents 8590379 + 1bd46d8 commit 6b2ee7b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

package org.eclipse.tractusx.bpdm.common.dto.response

import com.neovisionaries.i18n.CountryCode
import io.swagger.v3.oas.annotations.media.Schema

@Schema(description = "Country subdivision")
data class CountrySubdivisionDto(

@get:Schema(description = "Country code")
val countryCode: CountryCode,

@get:Schema(description = "The country subdivision code according to ISO 3166-2")
val code: String,

@get:Schema(description = "The name of the country subdivision according to ISO 3166-2")
val name: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package org.eclipse.tractusx.bpdm.common.dto.response
import com.neovisionaries.i18n.CountryCode
import io.swagger.v3.oas.annotations.media.Schema

@Schema(name = "RegionDto", description = "Region within a country")
@Schema(description = "Region within a country")
data class RegionDto(

@get:Schema(description = "Country code")
Expand All @@ -33,4 +33,4 @@ data class RegionDto(

@get:Schema(description = "Describes the full name of the region within a country according to ISO 3166-214")
val regionName: String
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.eclipse.tractusx.bpdm.common.dto.FieldQualityRuleDto
import org.eclipse.tractusx.bpdm.common.dto.IdentifierBusinessPartnerType
import org.eclipse.tractusx.bpdm.common.dto.IdentifierTypeDto
import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest
import org.eclipse.tractusx.bpdm.common.dto.response.CountrySubdivisionDto
import org.eclipse.tractusx.bpdm.common.dto.response.LegalFormDto
import org.eclipse.tractusx.bpdm.common.dto.response.PageDto
import org.eclipse.tractusx.bpdm.common.dto.response.RegionDto
Expand Down Expand Up @@ -168,17 +169,17 @@ interface PoolMetadataApi {
fun getRegions(@ParameterObject paginationRequest: PaginationRequest): PageDto<RegionDto>

@Operation(
summary = "Get page of regions suitable for the administrativeAreaLevel1 address property",
description = "Lists all currently known regions in a paginated result"
summary = "Get page of country subdivisions suitable for the administrativeAreaLevel1 address property",
description = "Lists all currently known country subdivisions according to ISO 3166-2 in a paginated result"
)
@ApiResponses(
value = [
ApiResponse(responseCode = "200", description = "Page of existing regions, may be empty"),
ApiResponse(responseCode = "200", description = "Page of existing country subdivisions, may be empty"),
ApiResponse(responseCode = "400", description = "On malformed request parameters", content = [Content()])
]
)
@GetMapping("/administrative-areas-level1")
@GetExchange("/administrative-areas-level1")
fun getAdminAreasLevel1(@ParameterObject paginationRequest: PaginationRequest): PageDto<RegionDto>
fun getAdminAreasLevel1(@ParameterObject paginationRequest: PaginationRequest): PageDto<CountrySubdivisionDto>

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.eclipse.tractusx.bpdm.common.dto.FieldQualityRuleDto
import org.eclipse.tractusx.bpdm.common.dto.IdentifierBusinessPartnerType
import org.eclipse.tractusx.bpdm.common.dto.IdentifierTypeDto
import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest
import org.eclipse.tractusx.bpdm.common.dto.response.CountrySubdivisionDto
import org.eclipse.tractusx.bpdm.common.dto.response.LegalFormDto
import org.eclipse.tractusx.bpdm.common.dto.response.PageDto
import org.eclipse.tractusx.bpdm.common.dto.response.RegionDto
Expand Down Expand Up @@ -71,8 +72,8 @@ class MetadataController(
}

@PreAuthorize("hasAuthority(@poolSecurityConfigProperties.getReadMetaDataAsRole())")
override fun getAdminAreasLevel1(paginationRequest: PaginationRequest): PageDto<RegionDto> {
return metadataService.getRegions(paginationRequest)
override fun getAdminAreasLevel1(paginationRequest: PaginationRequest): PageDto<CountrySubdivisionDto> {
return metadataService.getCountrySubdivisions(paginationRequest)
}

@PreAuthorize("hasAuthority(@poolSecurityConfigProperties.getReadMetaDataAsRole())")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.neovisionaries.i18n.CountryCode
import mu.KotlinLogging
import org.eclipse.tractusx.bpdm.common.dto.*
import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest
import org.eclipse.tractusx.bpdm.common.dto.response.CountrySubdivisionDto
import org.eclipse.tractusx.bpdm.common.dto.response.LegalFormDto
import org.eclipse.tractusx.bpdm.common.dto.response.PageDto
import org.eclipse.tractusx.bpdm.common.dto.response.RegionDto
Expand Down Expand Up @@ -178,7 +179,6 @@ class MetadataService(

@Transactional
fun createRegion(request: RegionDto): RegionDto {

logger.info { "Create new Region with key ${request.regionCode} and name ${request.regionName}" }

val region = Region(
Expand All @@ -187,13 +187,18 @@ class MetadataService(
regionName = request.regionName
)

return regionRepository.save(region).toDto()
return regionRepository.save(region).toRegionDto()
}

fun getRegions(paginationRequest: PaginationRequest): PageDto<RegionDto> {
val pageRequest = paginationRequest.toPageRequest(RegionRepository.DEFAULT_SORTING)
val page = regionRepository.findAll(pageRequest)
return page.toDto(page.content.map { it.toDto() })
return page.toDto(page.content.map { it.toRegionDto() })
}

}
fun getCountrySubdivisions(paginationRequest: PaginationRequest): PageDto<CountrySubdivisionDto> {
val pageRequest = paginationRequest.toPageRequest(RegionRepository.DEFAULT_SORTING)
val page = regionRepository.findAll(pageRequest)
return page.toDto(page.content.map { it.toCountrySubdivisionDto() })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ fun PartnerChangelogEntry.toDto(): ChangelogEntryVerboseDto {
return ChangelogEntryVerboseDto(bpn, businessPartnerType, createdAt, changelogType)
}

fun Region.toDto(): RegionDto {
return RegionDto(countryCode, regionCode, regionName)
fun Region.toRegionDto(): RegionDto {
return RegionDto(countryCode = countryCode, regionCode = regionCode, regionName = regionName)
}

fun Region.toCountrySubdivisionDto(): CountrySubdivisionDto {
return CountrySubdivisionDto(countryCode = countryCode, code = regionCode, name = regionName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -437,23 +437,21 @@ class MetadataControllerIT @Autowired constructor(
assertThat(firstPage.totalPages).isEqualTo(348)
assertThat(firstPage.content.size).isEqualTo(10)

// INSERT INTO bpdm.regions (country_code, region_code, region_name) VALUES ('AD', 'AD-02', 'Canillo');
with(firstPage.content.first()) {
assertThat(countryCode).isEqualTo(AD)
assertThat(regionCode).isEqualTo("AD-02")
assertThat(regionName).isEqualTo("Canillo")
assertThat(code).isEqualTo("AD-02")
assertThat(name).isEqualTo("Canillo")
}

val lastPage = poolClient.metadata.getAdminAreasLevel1(PaginationRequest(347))
assertThat(lastPage.totalElements).isEqualTo(3478L)
assertThat(lastPage.totalPages).isEqualTo(348)
assertThat(lastPage.content.size).isEqualTo(8)

// INSERT INTO bpdm.regions (country_code, region_code, region_name) VALUES ('ZW', 'ZW-MW', 'Mashonaland West');
with(lastPage.content.last()) {
assertThat(countryCode).isEqualTo(ZW)
assertThat(regionCode).isEqualTo("ZW-MW")
assertThat(regionName).isEqualTo("Mashonaland West")
assertThat(code).isEqualTo("ZW-MW")
assertThat(name).isEqualTo("Mashonaland West")
}
}
}

0 comments on commit 6b2ee7b

Please sign in to comment.