Return uppercase country name list for country search API
All checks were successful
build-and-deploy / build-deploy (push) Successful in 35s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 35s
This commit is contained in:
@@ -157,7 +157,8 @@ AUTH + SYSTEM APIS
|
||||
|
||||
- Searches countries from local country_reference table.
|
||||
- Case-insensitive match on country name, official name, ISO alpha-2, and ISO alpha-3.
|
||||
- Example: q=IND returns matches like India and related country names.
|
||||
- Returns JSON array of uppercase country names only.
|
||||
- Example: q=IND can return ["INDIA", "INDONESIA", ...].
|
||||
|
||||
Request body:
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.RequestParam
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
import java.util.Locale
|
||||
|
||||
@RestController
|
||||
class CountrySearch(
|
||||
@@ -16,7 +17,7 @@ class CountrySearch(
|
||||
fun searchCountries(
|
||||
@RequestParam("q") q: String,
|
||||
@RequestParam("limit", required = false, defaultValue = "20") limit: Int
|
||||
): List<CountrySearchResponse> {
|
||||
): List<String> {
|
||||
val query = q.trim()
|
||||
if (query.length < 3) {
|
||||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "q must be at least 3 characters")
|
||||
@@ -24,20 +25,6 @@ class CountrySearch(
|
||||
val boundedLimit = limit.coerceIn(1, 100)
|
||||
return countryReferenceRepo
|
||||
.searchCountries(query, PageRequest.of(0, boundedLimit))
|
||||
.map {
|
||||
CountrySearchResponse(
|
||||
name = it.name,
|
||||
officialName = it.officialName,
|
||||
isoAlpha2 = it.isoAlpha2,
|
||||
isoAlpha3 = it.isoAlpha3
|
||||
)
|
||||
}
|
||||
.map { it.name.uppercase(Locale.ROOT) }
|
||||
}
|
||||
}
|
||||
|
||||
data class CountrySearchResponse(
|
||||
val name: String,
|
||||
val officialName: String?,
|
||||
val isoAlpha2: String?,
|
||||
val isoAlpha3: String?
|
||||
)
|
||||
|
||||
@@ -9,10 +9,7 @@ import org.springframework.data.repository.query.Param
|
||||
interface CountryReferenceRepo : JpaRepository<CountryReference, Long> {
|
||||
@Query(
|
||||
"""
|
||||
select c.name as name,
|
||||
c.officialName as officialName,
|
||||
c.isoAlpha2 as isoAlpha2,
|
||||
c.isoAlpha3 as isoAlpha3
|
||||
select c.name as name
|
||||
from CountryReference c
|
||||
where trim(c.name) <> ''
|
||||
and (
|
||||
@@ -35,12 +32,9 @@ interface CountryReferenceRepo : JpaRepository<CountryReference, Long> {
|
||||
fun searchCountries(
|
||||
@Param("query") query: String,
|
||||
pageable: Pageable
|
||||
): List<CountrySearchRow>
|
||||
): List<CountryNameRow>
|
||||
}
|
||||
|
||||
interface CountrySearchRow {
|
||||
interface CountryNameRow {
|
||||
val name: String
|
||||
val officialName: String?
|
||||
val isoAlpha2: String?
|
||||
val isoAlpha3: String?
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user