Skip to content

Commit

Permalink
[PS|SS] TAV-302 - Tweak apply method for SearchParameters to accept n…
Browse files Browse the repository at this point in the history
…ew lookuprequest model
  • Loading branch information
ssaleem-ee committed Jun 29, 2021
1 parent 9f6dd0a commit 424e892
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 191 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ dist
/RUNNING_PID
/.settings
*.iws


/.bsp
/null

6 changes: 3 additions & 3 deletions app/osgb/AddressSearchController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ class AddressSearchController @Inject()(addressSearch: AddressSearcher, response
ec: ExecutionContext, cc: ControllerComponents)
extends AddressController(cc) {

implicit private val xec = ec
implicit private val xec: ExecutionContext = ec

import SearchParameters._

def search(): Action[LookupRequest] = Action.async(parse.json[LookupRequest]) {
request =>
val lookupRequest = request.body
val sp = SearchParameters.fromRequest(lookupRequest).clean
val sp = SearchParameters(lookupRequest).clean
processSearch(request, sp, Marshall.marshallV2List)
}

@deprecated
def searchWithGet(): Action[AnyContent] = Action.async {
request =>
val sp = SearchParameters.fromRequest(request.queryString).clean
val sp = SearchParameters.fromQueryParameters(request.queryString).clean
processSearch(request, sp, Marshall.marshallV2List)
}

Expand Down
13 changes: 0 additions & 13 deletions app/osgb/PostcodesController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,6 @@ class PostcodesController @Inject()(addressSearch: AddressSearcher, responseProc
}
}

@deprecated
def lookup(): Action[LookupPostcode] = Action.async(parse.json[LookupPostcode]) {
(request: Request[LookupPostcode]) =>
val origin = getOriginHeaderIfSatisfactory(request.headers)
if (request.rawQueryString.nonEmpty) {
Future.successful {
badRequest(request.uri, "Query String supplied but not supported")
}
} else {
processPostcode(origin, request.uri, request.body.postcode)
}
}

private def processPostcode(origin: String, requestUri: String, postcode: String): Future[Result] = {
val cleanPostcode = Postcode.cleanupPostcode(URLDecoder.decode(postcode, "UTF-8"))
if (cleanPostcode.isDefined) {
Expand Down
25 changes: 8 additions & 17 deletions app/osgb/SearchParameters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,17 @@ object SearchParameters {
private[osgb] val LINE4 = "line4"
private[osgb] val LIMIT = "limit"

def fromRequest(queryString: Map[String, Seq[String]]): SearchParameters = {
def fromQueryParameters(queryString: Map[String, Seq[String]]): SearchParameters = {
apply(queryString.map(kv => kv._1 -> kv._2.head))
}

def fromRequest(lookupRequest: LookupRequest): SearchParameters = {
val lookupRequestMap: Map[String, String] = Seq[(String, Option[String])](
POSTCODE -> lookupRequest.postcode,
LINE1 -> lookupRequest.line1,
LINE2 -> lookupRequest.line2,
LINE3 -> lookupRequest.line3,
LINE4 -> lookupRequest.line4,
UPRN -> lookupRequest.uprn,
OUTCODE -> lookupRequest.outcode,
FUZZY -> lookupRequest.fuzzy,
FILTER -> lookupRequest.filter,
TOWN -> lookupRequest.town,
LIMIT -> lookupRequest.limit.map(_.toString)
).collect { case (k, Some(v)) => k -> v }.toMap

apply(lookupRequestMap)
def apply(lookupRequest: LookupRequest): SearchParameters = {
new SearchParameters(
postcode = Postcode.cleanupPostcode(lookupRequest.postcode),
filter = lookupRequest.filter match {
case Some(f) if f.trim.isEmpty => None
case o => o
})
}

def apply(queryString: Map[String, String]): SearchParameters = {
Expand Down
5 changes: 1 addition & 4 deletions app/osgb/inmodel/LookupRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ package osgb.inmodel

import play.api.libs.json.{Format, Json}

case class LookupRequest(postcode: Option[String] = None, line1: Option[String] = None, line2: Option[String] = None,
line3: Option[String] = None, line4: Option[String] = None, uprn: Option[String] = None,
outcode: Option[String] = None, fuzzy: Option[String] = None, town: Option[String] = None,
filter: Option[String] = None, limit: Option[Int] = None)
case class LookupRequest(postcode: String, filter: Option[String] = None)

object LookupRequest {
implicit val formats: Format[LookupRequest] = Json.format[LookupRequest]
Expand Down
2 changes: 0 additions & 2 deletions conf/app.routes
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ GET /admin/metrics com.kenshoo.play.metrics.MetricsControll
# Note that these resources take up to four query parameters: postcode, filter, fuzzy, uprn
# and the tests are more explicit as a result.
GET /v2/uk/addresses osgb.AddressSearchController.searchWithGet()
POST /v2/uk/addresses osgb.AddressSearchController.search()

GET /lookup osgb.AddressSearchController.searchWithGet()
POST /lookup osgb.AddressSearchController.search()
Expand All @@ -20,6 +19,5 @@ GET /v2/uk/addresses/:id osgb.AddressLookupIdController.findById(

# ----- Postcodes endppint -----
GET /v1/uk/postcodes/:pc osgb.PostcodesController.lookupWithGet(pc)
POST /v1/uk/postcodes osgb.PostcodesController.lookup

GET /v1/uk/postcodes osgb.PostcodesController.missing
Loading

0 comments on commit 424e892

Please sign in to comment.