Skip to content

Commit

Permalink
Cleanup queries
Browse files Browse the repository at this point in the history
Signed-off-by: Walker Crouse <[email protected]>
  • Loading branch information
windy1 committed Apr 23, 2016
1 parent af0ecd2 commit fed63cf
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 28 deletions.
16 changes: 8 additions & 8 deletions app/db/query/ProjectQueries.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ProjectQueries extends Queries[ProjectTable, Project](TableQuery(tag => ne
* @return Projects matching criteria
*/
def collect(categories: Array[Category]): Future[Seq[Project]] = {
collect(categories, -1, -1)
collect(categories, -1)
}

/**
Expand All @@ -97,7 +97,7 @@ class ProjectQueries extends Queries[ProjectTable, Project](TableQuery(tag => ne
* @return Projects matching criteria
*/
def collect(limit: Int): Future[Seq[Project]] = {
collect(null.asInstanceOf[Array[Category]], limit, -1)
collect(null.asInstanceOf[Array[Category]], limit)
}

/**
Expand All @@ -107,7 +107,7 @@ class ProjectQueries extends Queries[ProjectTable, Project](TableQuery(tag => ne
* @return Projects by owner
*/
def by(ownerName: String): Future[Seq[Project]] = {
run(this.models.filter(p => p.ownerName === ownerName).result)
run(this.models.filter(_.ownerName === ownerName).result)
}

/**
Expand Down Expand Up @@ -161,7 +161,7 @@ class ProjectQueries extends Queries[ProjectTable, Project](TableQuery(tag => ne
* @return True if cookie is found
*/
def hasBeenViewedBy(projectId: Int, cookie: String): Future[Boolean] = {
val query = this.views.filter(pv => pv.projectId === projectId && pv.cookie === cookie).size > 0
val query = this.views.filter(pv => pv.projectId === projectId && pv.cookie === cookie).length > 0
run(query.result)
}

Expand All @@ -186,7 +186,7 @@ class ProjectQueries extends Queries[ProjectTable, Project](TableQuery(tag => ne
* @return True if user is found
*/
def hasBeenViewedBy(projectId: Int, userId: Int): Future[Boolean] = {
val query = this.views.filter(pv => pv.projectId === projectId && pv.userId === userId).size > 0
val query = this.views.filter(pv => pv.projectId === projectId && pv.userId === userId).length > 0
run(query.result)
}

Expand All @@ -210,7 +210,7 @@ class ProjectQueries extends Queries[ProjectTable, Project](TableQuery(tag => ne
* @return True if Project is starred by user
*/
def isStarredBy(projectId: Int, userId: Int): Future[Boolean] = {
val query = this.stars.filter(sp => sp.userId === userId && sp.projectId === projectId).size > 0
val query = this.stars.filter(sp => sp.userId === userId && sp.projectId === projectId).length > 0
run(query.result)
}

Expand Down Expand Up @@ -246,12 +246,12 @@ class ProjectQueries extends Queries[ProjectTable, Project](TableQuery(tag => ne
*/
def starredBy(userId: Int, limit: Int = -1, offset: Int = -1): Future[Seq[Project]] = {
val promise = Promise[Seq[Project]]
val starQuery = this.stars.filter(sp => sp.userId === userId)
val starQuery = this.stars.filter(_.userId === userId)
run(starQuery.result).andThen {
case Failure(thrown) => promise.failure(thrown)
case Success(userStars) =>
val projectIds = userStars.map(_._2)
var projectsQuery = this.models.filter(p => p.id inSetBind projectIds)
var projectsQuery = this.models.filter(_.id inSetBind projectIds)
if (offset > -1) projectsQuery = projectsQuery.drop(offset)
if (limit > -1) projectsQuery = projectsQuery.take(limit)
promise.completeWith(run(projectsQuery.result))
Expand Down
4 changes: 2 additions & 2 deletions app/db/query/Queries.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ abstract class Queries[T <: ModelTable[M], M <: Model](val models: TableQuery[T]
* @param model Model to delete
*/
def delete(model: M): Future[Int] = {
val query = this.models.filter(m => m.id === model.id.get)
val query = this.models.filter(_.id === model.id.get)
run(query.delete)
}

Expand All @@ -99,7 +99,7 @@ abstract class Queries[T <: ModelTable[M], M <: Model](val models: TableQuery[T]
* @return Model if present, None otherwise
*/
def get(id: Int): Future[Option[M]] = {
?(m => m.id === id)
?(_.id === id)
}

/**
Expand Down
12 changes: 10 additions & 2 deletions app/db/query/VersionQueries.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ class VersionQueries extends Queries[VersionTable, Version](TableQuery(tag => ne

private val downloads = TableQuery[VersionDownloadsTable]

/**
* Returns true if the specified hash is found in the specified
* [[models.project.Project]]'s [[Version]]s.
*
* @param projectId Project ID
* @param hash Version hash
* @return True if found
*/
def hashExists(projectId: Int, hash: String): Future[Boolean] = {
val query = (for {
model <- this.models
Expand Down Expand Up @@ -48,7 +56,7 @@ class VersionQueries extends Queries[VersionTable, Version](TableQuery(tag => ne
* @return True if downloaded
*/
def hasBeenDownloadedBy(versionId: Int, cookie: String): Future[Boolean] = {
val query = this.downloads.filter(vd => vd.versionId === versionId && vd.cookie === cookie).size > 0
val query = this.downloads.filter(vd => vd.versionId === versionId && vd.cookie === cookie).length > 0
run(query.result)
}

Expand All @@ -73,7 +81,7 @@ class VersionQueries extends Queries[VersionTable, Version](TableQuery(tag => ne
* @return True if downloaded
*/
def hasBeenDownloadedBy(versionId: Int, userId: Int): Future[Boolean] = {
val query = this.downloads.filter(vd => vd.versionId === versionId && vd.userId === userId).size > 0
val query = this.downloads.filter(vd => vd.versionId === versionId && vd.userId === userId).length > 0
run(query.result)
}

Expand Down
7 changes: 0 additions & 7 deletions app/models/project/Channel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ case class Channel(override val id: Option[Int] = None,
update(ModelKeys.Color)
}

/**
* Returns the Project this Channel belongs to.
*
* @return Project the Channel belongs to
*/
def project: Project = Project.withId(this.projectId).get

/**
* Returns all Versions in this channel.
*
Expand Down
3 changes: 1 addition & 2 deletions app/models/project/Page.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models.project

import java.sql.Timestamp

import com.google.common.base.Objects
import com.google.common.base.Preconditions._
import db.orm.dao.ModelDAO
import db.orm.model.ModelKeys._
Expand Down Expand Up @@ -49,8 +50,6 @@ case class Page(override val id: Option[Int] = None,
slug=slugify(name), _contents=content.trim, isDeletable=isDeletable)
}

def project: Project = Project.withId(this.projectId).get

/**
* Returns the Markdown contents of this Page.
*
Expand Down
7 changes: 0 additions & 7 deletions app/models/project/Version.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ case class Version(override val id: Option[Int] = None,
this(versionString, dependencies, description, assets, projectId, -1, fileSize, hash)
}

/**
* Returns the project this version belongs to.
*
* @return Project
*/
def project: Project = Project.withId(this.projectId).get

/**
* Returns the channel this version belongs to.
*
Expand Down
9 changes: 9 additions & 0 deletions app/ore/permission/scope/ProjectScope.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ore.permission.scope

import models.project.Project

/**
* Represents a scope of a certain Project.
*/
Expand All @@ -12,6 +14,13 @@ trait ProjectScope extends Scope {
*/
def projectId: Int

/**
* Returns the [[Project]] of this [[Scope]].
*
* @return Project of scope
*/
def project: Project = Project.withId(this.projectId).get

override val parent: Option[Scope] = Some(GlobalScope)

}

0 comments on commit fed63cf

Please sign in to comment.