From 51f860df949ec0b39e0a15067dab02b18ff68534 Mon Sep 17 00:00:00 2001 From: Adrien Piquerez Date: Mon, 12 Aug 2024 15:38:50 +0200 Subject: [PATCH] Fix artifacts page filters --- .../scaladex/core/service/WebDatabase.scala | 7 +------ .../scaladex/core/test/InMemoryDatabase.scala | 5 ++--- .../scala/scaladex/infra/SqlDatabase.scala | 9 ++------- .../scaladex/infra/sql/ArtifactTable.scala | 18 ++++-------------- .../infra/sql/ArtifactTableTests.scala | 6 ++---- .../scaladex/server/route/ProjectPages.scala | 2 +- 6 files changed, 12 insertions(+), 35 deletions(-) diff --git a/modules/core/shared/src/main/scala/scaladex/core/service/WebDatabase.scala b/modules/core/shared/src/main/scala/scaladex/core/service/WebDatabase.scala index 6e5401a58..2abaac473 100644 --- a/modules/core/shared/src/main/scala/scaladex/core/service/WebDatabase.scala +++ b/modules/core/shared/src/main/scala/scaladex/core/service/WebDatabase.scala @@ -6,7 +6,6 @@ import java.util.UUID import scala.concurrent.Future import scaladex.core.model._ -import scaladex.core.web.ArtifactsPageParams trait WebDatabase { // artifacts @@ -14,11 +13,7 @@ trait WebDatabase { def insertArtifact(artifact: Artifact, dependencies: Seq[ArtifactDependency], time: Instant): Future[Boolean] def getArtifacts(groupId: Artifact.GroupId, artifactId: Artifact.ArtifactId): Future[Seq[Artifact]] def getArtifacts(projectRef: Project.Reference): Future[Seq[Artifact]] - def getArtifacts( - ref: Project.Reference, - artifactName: Artifact.Name, - params: ArtifactsPageParams - ): Future[Seq[Artifact]] + def getArtifacts(ref: Project.Reference, artifactName: Artifact.Name, preReleases: Boolean): Future[Seq[Artifact]] def getArtifacts(ref: Project.Reference, artifactName: Artifact.Name, version: SemanticVersion): Future[Seq[Artifact]] def getArtifactsByName(projectRef: Project.Reference, artifactName: Artifact.Name): Future[Seq[Artifact]] def getLatestArtifacts(ref: Project.Reference, preferStableVersions: Boolean): Future[Seq[Artifact]] diff --git a/modules/core/shared/src/test/scala/scaladex/core/test/InMemoryDatabase.scala b/modules/core/shared/src/test/scala/scaladex/core/test/InMemoryDatabase.scala index 26cdceb9a..375cec6ab 100644 --- a/modules/core/shared/src/test/scala/scaladex/core/test/InMemoryDatabase.scala +++ b/modules/core/shared/src/test/scala/scaladex/core/test/InMemoryDatabase.scala @@ -19,7 +19,6 @@ import scaladex.core.model.SemanticVersion import scaladex.core.model.UserInfo import scaladex.core.model.UserState import scaladex.core.service.SchedulerDatabase -import scaladex.core.web.ArtifactsPageParams class InMemoryDatabase extends SchedulerDatabase { private val allProjects = mutable.Map[Project.Reference, Project]() @@ -161,9 +160,9 @@ class InMemoryDatabase extends SchedulerDatabase { override def getArtifacts( ref: Project.Reference, artifactName: Artifact.Name, - params: ArtifactsPageParams + preReleases: Boolean ): Future[Seq[Artifact]] = - // does not filter with params + // TODO: use preReleases to filter Future.successful(allArtifacts.getOrElse(ref, Seq.empty).filter(_.artifactName == artifactName)) override def getProjectDependencies( ref: Project.Reference, diff --git a/modules/infra/src/main/scala/scaladex/infra/SqlDatabase.scala b/modules/infra/src/main/scala/scaladex/infra/SqlDatabase.scala index 59af19dcd..24c8c5d9c 100644 --- a/modules/infra/src/main/scala/scaladex/infra/SqlDatabase.scala +++ b/modules/infra/src/main/scala/scaladex/infra/SqlDatabase.scala @@ -23,7 +23,6 @@ import scaladex.core.model.SemanticVersion import scaladex.core.model.UserInfo import scaladex.core.model.UserState import scaladex.core.service.SchedulerDatabase -import scaladex.core.web.ArtifactsPageParams import scaladex.infra.sql.ArtifactDependencyTable import scaladex.infra.sql.ArtifactTable import scaladex.infra.sql.DoobieUtils @@ -235,13 +234,9 @@ class SqlDatabase(datasource: HikariDataSource, xa: doobie.Transactor[IO]) exten override def getArtifacts( ref: Project.Reference, artifactName: Artifact.Name, - params: ArtifactsPageParams + preReleases: Boolean ): Future[Seq[Artifact]] = - run( - ArtifactTable - .selectArtifactByParams(params.binaryVersions, params.preReleases) - .to[Seq](ref, artifactName) - ) + run(ArtifactTable.selectArtifactByParams(preReleases).to[Seq](ref, artifactName)) override def countVersions(ref: Project.Reference): Future[Long] = run(ArtifactTable.countVersionsByProject.unique(ref)) diff --git a/modules/infra/src/main/scala/scaladex/infra/sql/ArtifactTable.scala b/modules/infra/src/main/scala/scaladex/infra/sql/ArtifactTable.scala index ee001d2ed..f2bedfbce 100644 --- a/modules/infra/src/main/scala/scaladex/infra/sql/ArtifactTable.scala +++ b/modules/infra/src/main/scala/scaladex/infra/sql/ArtifactTable.scala @@ -5,7 +5,6 @@ import java.time.Instant import doobie._ import doobie.util.update.Update import scaladex.core.model.Artifact -import scaladex.core.model.BinaryVersion import scaladex.core.model.Language import scaladex.core.model.Platform import scaladex.core.model.Project @@ -83,21 +82,12 @@ object ArtifactTable { where = Seq("organization=?", "repository=?", "artifact_name=?", "version=?") ) - def selectArtifactByParams( - binaryVersions: Seq[BinaryVersion], - preReleases: Boolean - ): Query[(Project.Reference, Artifact.Name), Artifact] = { - val binaryVersionFilter = - if (binaryVersions.isEmpty) "true" - else - binaryVersions - .map(bv => s"(platform='${bv.platform.label}' AND language_version='${bv.language.label}')") - .mkString("(", " OR ", ")") - val preReleaseFilter = if (preReleases) s"true" else "is_prerelease=false" + def selectArtifactByParams(preReleases: Boolean): Query[(Project.Reference, Artifact.Name), Artifact] = { + val preReleaseFilter = if (preReleases) "true" else "is_prerelease=false" Query[(Project.Reference, Artifact.Name), Artifact]( s"""|SELECT ${fields.mkString(", ")} - |FROM $table WHERE - |organization=? AND repository=? AND artifact_name=? AND $binaryVersionFilter AND $preReleaseFilter + |FROM $table + |WHERE organization=? AND repository=? AND artifact_name=? AND $preReleaseFilter |""".stripMargin ) } diff --git a/modules/infra/src/test/scala/scaladex/infra/sql/ArtifactTableTests.scala b/modules/infra/src/test/scala/scaladex/infra/sql/ArtifactTableTests.scala index 5af529486..8d0af573b 100644 --- a/modules/infra/src/test/scala/scaladex/infra/sql/ArtifactTableTests.scala +++ b/modules/infra/src/test/scala/scaladex/infra/sql/ArtifactTableTests.scala @@ -5,7 +5,6 @@ import org.scalatest.matchers.should.Matchers import scaladex.core.model.Jvm import scaladex.core.model.Scala import scaladex.core.model.ScalaJs -import scaladex.core.test.Values._ import scaladex.infra.BaseDatabaseSuite class ArtifactTableTests extends AnyFunSpec with BaseDatabaseSuite with Matchers { @@ -29,9 +28,8 @@ class ArtifactTableTests extends AnyFunSpec with BaseDatabaseSuite with Matchers it("check getReleasesFromArtifacts")(check(getReleasesFromArtifacts)) it("check countVersionsByProject")(check(countVersionsByProject)) it("check selectArtifactByParams") { - check(selectArtifactByParams(Seq.empty, false)) - check(selectArtifactByParams(Seq.empty, true)) - check(selectArtifactByParams(Seq(`_sjs0.6_2.13`), false)) + check(selectArtifactByParams(false)) + check(selectArtifactByParams(true)) } it("check selectMavenReferenceWithNoReleaseDate")(check(selectMavenReferenceWithNoReleaseDate)) it("check selectLatestArtifacts") { diff --git a/modules/server/src/main/scala/scaladex/server/route/ProjectPages.scala b/modules/server/src/main/scala/scaladex/server/route/ProjectPages.scala index 2ac124001..35ee95295 100644 --- a/modules/server/src/main/scala/scaladex/server/route/ProjectPages.scala +++ b/modules/server/src/main/scala/scaladex/server/route/ProjectPages.scala @@ -41,7 +41,7 @@ class ProjectPages(env: Env, database: WebDatabase, searchEngine: SearchEngine)( path(projectM / "artifacts" / artifactNameM) { (ref, artifactName) => artifactsParams { params => getProjectOrRedirect(ref, user) { project => - val artifactsF = database.getArtifacts(ref, artifactName, params) + val artifactsF = database.getArtifacts(ref, artifactName, params.preReleases) val headerF = service.getProjectHeader(project).map(_.get) for (artifacts <- artifactsF; header <- headerF) yield { val binaryVersions = artifacts