Skip to content

Commit

Permalink
fix search api
Browse files Browse the repository at this point in the history
  • Loading branch information
adpi2 committed Feb 16, 2021
1 parent a4d0498 commit 36d2ead
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.softwaremill.session.SessionOptions.{refreshable, usingCookies}
import play.api.libs.json._

import scala.concurrent.ExecutionContext
import akka.http.scaladsl.model.HttpHeader

object SearchApi {
implicit val formatProject: OFormat[Project] =
Expand Down Expand Up @@ -91,7 +92,8 @@ class SearchApi(
val routes: Route =
pathPrefix("api") {
cors() {
path("search") {
// deprecated endpoint replaced by api/search endpoint
path("search-old") {
get {
parameters(
(
Expand Down Expand Up @@ -125,17 +127,6 @@ class SearchApi(
sbtVersion
)

def convert(project: Project): SearchApi.Project = {
import project._
val artifacts0 = if (cli) cliArtifacts.toList else artifacts
SearchApi.Project(
organization,
repository,
project.github.flatMap(_.logo.map(_.target)),
artifacts0
)
}

scalaTarget match {
case Some(_) =>
val searchParams = SearchParams(
Expand All @@ -147,7 +138,7 @@ class SearchApi(
)
val result = dataRepository
.findProjects(searchParams)
.map(page => page.items.map(p => convert(p)))
.map(page => page.items.map(convertProject(cli)))
complete(OK, result)

case None =>
Expand All @@ -158,6 +149,19 @@ class SearchApi(
}
}
} ~
path("search") {
get {
optionalSession(refreshable, usingCookies) { userId =>
val user = session.getUser(userId)
searchParams(user) { params =>
val result = dataRepository
.findProjects(params)
.map(page => page.items.map(convertProject(params.cli)))
complete(result)
}
}
}
} ~
path("project") {
get {
parameters(
Expand Down Expand Up @@ -201,16 +205,25 @@ class SearchApi(
optionalSession(refreshable, usingCookies) { userId =>
val user = session.getUser(userId)
searchParams(user) { params =>
complete {
autocomplete(params)
}
val autoCompletion = autocomplete(params)
complete(autoCompletion)
}
}
}
}
}
}

private def convertProject(cli: Boolean)(project: Project): SearchApi.Project = {
val artifacts = if (cli) project.cliArtifacts.toList else project.artifacts
SearchApi.Project(
project.organization,
project.repository,
project.github.flatMap(_.logo.map(_.target)),
project.artifacts
)
}

private def getReleaseOptions(
projectRef: Project.Reference,
scalaTarget: Option[ScalaTarget],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,33 @@ package object routes {
(
"q" ? "*",
"page".as[Int] ? 1,
"total".as[Int] ? SearchParams.resultsPerPage,
"sort".?,
"topics".as[String].*,
"targetTypes".as[String].*,
"scalaVersions".as[String].*,
"scalaJsVersions".as[String].*,
"scalaNativeVersions".as[String].*,
"sbtVersions".as[String].*,
"contributingSearch".as[Boolean] ? false,
"cli".as[Boolean] ? false,
"you".?,
"contributingSearch".as[Boolean] ? false
)
).tmap {
case (
q,
page,
total,
sort,
topics,
targetTypes,
scalaVersions,
scalaJsVersions,
scalaNativeVersions,
sbtVersions,
contributingSearch,
cli,
you,
contributingSearch
) =>
val userRepos = you
.flatMap(_ => user.map(_.repos))
Expand All @@ -42,8 +46,10 @@ package object routes {
page,
sort,
userRepos,
total = total,
topics = topics.toList,
targetTypes = targetTypes.toList,
cli = cli,
scalaVersions = scalaVersions.toList,
scalaJsVersions = scalaJsVersions.toList,
scalaNativeVersions = scalaNativeVersions.toList,
Expand Down

0 comments on commit 36d2ead

Please sign in to comment.