From 36d2ead596ad6e44e45954218988efbde658d380 Mon Sep 17 00:00:00 2001 From: Adrien Piquerez Date: Tue, 16 Feb 2021 10:53:17 +0100 Subject: [PATCH 1/4] fix search api --- .../routes/api/SearchApi.scala | 45 ++++++++++++------- .../routes/package.scala | 10 ++++- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala b/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala index 1dd37f72b..20f6381c6 100644 --- a/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala +++ b/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala @@ -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] = @@ -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( ( @@ -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( @@ -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 => @@ -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( @@ -201,9 +205,8 @@ class SearchApi( optionalSession(refreshable, usingCookies) { userId => val user = session.getUser(userId) searchParams(user) { params => - complete { - autocomplete(params) - } + val autoCompletion = autocomplete(params) + complete(autoCompletion) } } } @@ -211,6 +214,16 @@ class SearchApi( } } + 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], diff --git a/server/src/main/scala/ch.epfl.scala.index.server/routes/package.scala b/server/src/main/scala/ch.epfl.scala.index.server/routes/package.scala index 7e75f1049..013edc896 100644 --- a/server/src/main/scala/ch.epfl.scala.index.server/routes/package.scala +++ b/server/src/main/scala/ch.epfl.scala.index.server/routes/package.scala @@ -10,6 +10,7 @@ package object routes { ( "q" ? "*", "page".as[Int] ? 1, + "total".as[Int] ? SearchParams.resultsPerPage, "sort".?, "topics".as[String].*, "targetTypes".as[String].*, @@ -17,13 +18,15 @@ package object routes { "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, @@ -31,8 +34,9 @@ package object routes { scalaJsVersions, scalaNativeVersions, sbtVersions, + contributingSearch, + cli, you, - contributingSearch ) => val userRepos = you .flatMap(_ => user.map(_.repos)) @@ -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, From ab6f480a85112726ca7dbb9f112a0f7e357ca261 Mon Sep 17 00:00:00 2001 From: Adrien Piquerez Date: Tue, 16 Feb 2021 13:49:00 +0100 Subject: [PATCH 2/4] Add search page data in search api result --- .../routes/api/SearchApi.scala | 27 +++++++++++++++---- .../routes/package.scala | 6 ++--- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala b/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala index 20f6381c6..49efb3129 100644 --- a/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala +++ b/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala @@ -20,7 +20,6 @@ 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] = @@ -32,8 +31,16 @@ object SearchApi { case class Project( organization: String, repository: String, - logo: Option[String] = None, - artifacts: List[String] = Nil + logo: Option[String], + stars: Option[Int], + forks: Option[Int], + contributorCount: Option[Int], + topics: Set[String], + artifacts: List[String], + scalaVersion: List[String], + scalaJsVersion: List[String], + scalaNativeVersion: List[String], + sbtVersion: List[String] ) case class ReleaseOptions( @@ -214,13 +221,23 @@ class SearchApi( } } - private def convertProject(cli: Boolean)(project: Project): SearchApi.Project = { + 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 + project.github.flatMap(_.stars), + project.github.flatMap(_.forks), + project.github.map(_.contributorCount), + project.github.map(_.topics).getOrElse(Set.empty), + project.artifacts, + project.scalaVersion, + project.sbtVersion, + project.scalaJsVersion, + project.scalaNativeVersion ) } diff --git a/server/src/main/scala/ch.epfl.scala.index.server/routes/package.scala b/server/src/main/scala/ch.epfl.scala.index.server/routes/package.scala index 013edc896..32aebfc9f 100644 --- a/server/src/main/scala/ch.epfl.scala.index.server/routes/package.scala +++ b/server/src/main/scala/ch.epfl.scala.index.server/routes/package.scala @@ -10,7 +10,7 @@ package object routes { ( "q" ? "*", "page".as[Int] ? 1, - "total".as[Int] ? SearchParams.resultsPerPage, + "total".as[Int] ? SearchParams.resultsPerPage, "sort".?, "topics".as[String].*, "targetTypes".as[String].*, @@ -20,7 +20,7 @@ package object routes { "sbtVersions".as[String].*, "contributingSearch".as[Boolean] ? false, "cli".as[Boolean] ? false, - "you".?, + "you".? ) ).tmap { case ( @@ -36,7 +36,7 @@ package object routes { sbtVersions, contributingSearch, cli, - you, + you ) => val userRepos = you .flatMap(_ => user.map(_.repos)) From 041c971d9860632abf206414b134108962040882 Mon Sep 17 00:00:00 2001 From: Adrien Piquerez Date: Tue, 16 Feb 2021 16:55:43 +0100 Subject: [PATCH 3/4] Add defaultArtifact field in search API --- .../ch.epfl.scala.index.server/routes/api/SearchApi.scala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala b/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala index 49efb3129..664ffc043 100644 --- a/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala +++ b/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala @@ -36,6 +36,7 @@ object SearchApi { forks: Option[Int], contributorCount: Option[Int], topics: Set[String], + defaultArtifact: Option[String], artifacts: List[String], scalaVersion: List[String], scalaJsVersion: List[String], @@ -225,6 +226,7 @@ class SearchApi( cli: Boolean )(project: Project): SearchApi.Project = { val artifacts = if (cli) project.cliArtifacts.toList else project.artifacts + val defaultArtifact = project.defaultArtifact.filter(artifacts.contains) SearchApi.Project( project.organization, project.repository, @@ -233,7 +235,8 @@ class SearchApi( project.github.flatMap(_.forks), project.github.map(_.contributorCount), project.github.map(_.topics).getOrElse(Set.empty), - project.artifacts, + defaultArtifact, + artifacts, project.scalaVersion, project.sbtVersion, project.scalaJsVersion, From 4d564b02d702f501bd032764a4f6c4fd6c534029 Mon Sep 17 00:00:00 2001 From: Adrien Piquerez Date: Tue, 16 Feb 2021 17:32:26 +0100 Subject: [PATCH 4/4] Add paging info in search API --- .../routes/api/SearchApi.scala | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala b/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala index 664ffc043..503752ace 100644 --- a/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala +++ b/server/src/main/scala/ch.epfl.scala.index.server/routes/api/SearchApi.scala @@ -28,6 +28,10 @@ object SearchApi { implicit val formatReleaseOptions: OFormat[ReleaseOptions] = Json.format[ReleaseOptions] + implicit val formatResult: OFormat[Result] = + Json.format[Result] + + case class Result(currentPage: Int, pageTotal: Int, items: Seq[Project]) case class Project( organization: String, repository: String, @@ -164,7 +168,13 @@ class SearchApi( searchParams(user) { params => val result = dataRepository .findProjects(params) - .map(page => page.items.map(convertProject(params.cli))) + .map { page => + SearchApi.Result( + currentPage = page.pagination.current, + pageTotal = page.pagination.pageCount, + items = page.items.map(convertProject(params.cli)) + ) + } complete(result) } }