diff --git a/src/main/scala/model/BasicTemplate.scala b/src/main/scala/model/BasicTemplate.scala index 971a5db..2d92e64 100644 --- a/src/main/scala/model/BasicTemplate.scala +++ b/src/main/scala/model/BasicTemplate.scala @@ -31,4 +31,14 @@ def byLabel(userName: Column[String], repositoryName: Column[String], labelId: Column[Int]) = byRepository(userName, repositoryName) && (this.labelId is labelId) +} + +protected[model] trait MilestoneTemplate extends BasicTemplate { self: Table[_] => + def milestoneId = column[Int]("MILESTONE_ID") + + def byMilestone(owner: String, repository: String, milestoneId: Int) = + byRepository(owner, repository) && (this.milestoneId is milestoneId.bind) + + def byMilestone(userName: Column[String], repositoryName: Column[String], milestoneId: Column[Int]) = + byRepository(userName, repositoryName) && (this.milestoneId is milestoneId) } \ No newline at end of file diff --git a/src/main/scala/model/Collaborator.scala b/src/main/scala/model/Collaborator.scala index 7e8c3de..f56c313 100644 --- a/src/main/scala/model/Collaborator.scala +++ b/src/main/scala/model/Collaborator.scala @@ -6,7 +6,8 @@ def collaboratorName = column[String]("COLLABORATOR_NAME") def * = userName ~ repositoryName ~ collaboratorName <> (Collaborator, Collaborator.unapply _) - def byPrimaryKey = byRepository _ + def byPrimaryKey(owner: String, repository: String, collaborator: String) = + byRepository(owner, repository) && (collaboratorName is collaborator.bind) } case class Collaborator( diff --git a/src/main/scala/model/Issue.scala b/src/main/scala/model/Issue.scala index b9e63c2..ab989ce 100644 --- a/src/main/scala/model/Issue.scala +++ b/src/main/scala/model/Issue.scala @@ -7,9 +7,8 @@ def byPrimaryKey = byRepository _ } -object Issues extends Table[Issue]("ISSUE") with IssueTemplate with Functions { +object Issues extends Table[Issue]("ISSUE") with IssueTemplate with MilestoneTemplate with Functions { def openedUserName = column[String]("OPENED_USER_NAME") - def milestoneId = column[Int]("MILESTONE_ID") def assignedUserName = column[String]("ASSIGNED_USER_NAME") def title = column[String]("TITLE") def content = column[String]("CONTENT") diff --git a/src/main/scala/model/Milestone.scala b/src/main/scala/model/Milestone.scala index ebb7d13..25f50a5 100644 --- a/src/main/scala/model/Milestone.scala +++ b/src/main/scala/model/Milestone.scala @@ -2,8 +2,7 @@ import scala.slick.driver.H2Driver.simple._ -object Milestones extends Table[Milestone]("MILESTONE") with BasicTemplate with Functions { - def milestoneId = column[Int]("MILESTONE_ID", O AutoInc) +object Milestones extends Table[Milestone]("MILESTONE") with MilestoneTemplate with Functions { def title = column[String]("TITLE") def description = column[String]("DESCRIPTION") def dueDate = column[java.util.Date]("DUE_DATE") @@ -11,9 +10,7 @@ def * = userName ~ repositoryName ~ milestoneId ~ title ~ description.? ~ dueDate.? ~ closedDate.? <> (Milestone, Milestone.unapply _) def autoInc = userName ~ repositoryName ~ title ~ description.? ~ dueDate.? ~ closedDate.? returning milestoneId - // TODO create a template? - def byPrimaryKey(owner: String, repository: String, milestoneId: Int) = - byRepository(owner, repository) && (this.milestoneId is milestoneId.bind) + def byPrimaryKey = byMilestone _ } case class Milestone( diff --git a/src/main/scala/service/AccountService.scala b/src/main/scala/service/AccountService.scala index 85d8b56..a811f80 100644 --- a/src/main/scala/service/AccountService.scala +++ b/src/main/scala/service/AccountService.scala @@ -37,7 +37,6 @@ account.lastLoginDate) def updateLastLoginDate(userName: String): Unit = - Query(Accounts).filter(_.userName is userName.bind).map(_.lastLoginDate) - .update(currentDate) + Query(Accounts).filter(_.userName is userName.bind).map(_.lastLoginDate).update(currentDate) } diff --git a/src/main/scala/service/LabelsService.scala b/src/main/scala/service/LabelsService.scala index 25f0938..6db0872 100644 --- a/src/main/scala/service/LabelsService.scala +++ b/src/main/scala/service/LabelsService.scala @@ -8,33 +8,21 @@ trait LabelsService { def getLabels(owner: String, repository: String): List[Label] = - Query(Labels) - .filter(t => (t.userName is owner.bind) && (t.repositoryName is repository.bind)) - .sortBy(_.labelName asc) - .list + Query(Labels).filter(_.byRepository(owner, repository)).sortBy(_.labelName asc).list def getLabel(owner: String, repository: String, labelId: Int): Option[Label] = - Query(Labels) - .filter(t => (t.userName is owner.bind) && (t.repositoryName is repository.bind) && (t.labelId is labelId.bind)) - .firstOption + Query(Labels).filter(_.byPrimaryKey(owner, repository, labelId)).firstOption def createLabel(owner: String, repository: String, labelName: String, color: String): Unit = Labels.ins insert (owner, repository, labelName, color) def updateLabel(owner: String, repository: String, labelId: Int, labelName: String, color: String): Unit = - Query(Labels) - .filter { t => (t.userName is owner.bind) && (t.repositoryName is repository.bind) && (t.labelId is labelId.bind)} - .map { t => t.labelName ~ t.color } - .update (labelName, color) + Query(Labels).filter(_.byPrimaryKey(owner, repository, labelId)).map(t => t.labelName ~ t.color) + .update(labelName, color) def deleteLabel(owner: String, repository: String, labelId: Int): Unit = { - Query(IssueLabels) - .filter { t => (t.userName is owner.bind) && (t.repositoryName is repository.bind) && (t.labelId is labelId.bind)} - .delete - - Query(Labels) - .filter { t => (t.userName is owner.bind) && (t.repositoryName is repository.bind) && (t.labelId is labelId.bind)} - .delete + Query(IssueLabels).filter(_.byLabel(owner, repository, labelId)).delete + Query(Labels).filter(_.byPrimaryKey(owner, repository, labelId)).delete } } diff --git a/src/main/scala/service/MilestonesService.scala b/src/main/scala/service/MilestonesService.scala index 70862b5..09f3d12 100644 --- a/src/main/scala/service/MilestonesService.scala +++ b/src/main/scala/service/MilestonesService.scala @@ -8,76 +8,35 @@ trait MilestonesService { - def createMilestone(owner: String, repository: String, - title: String, description: Option[String], dueDate: Option[java.util.Date]) = + def createMilestone(owner: String, repository: String, title: String, description: Option[String], + dueDate: Option[java.util.Date]) = Milestones.autoInc insert (owner, repository, title, description, dueDate, None) def updateMilestone(milestone: Milestone): Unit = Query(Milestones) - .filter { m => - (m.userName is milestone.userName.bind) && - (m.repositoryName is milestone.repositoryName.bind) && - (m.milestoneId is milestone.milestoneId.bind) - } - .map { m => - m.title ~ m.description.? ~ m.dueDate.? ~ m.closedDate.? - } - .update ( - milestone.title, - milestone.description, - milestone.dueDate, - milestone.closedDate) + .filter (t => t.byPrimaryKey(milestone.userName, milestone.repositoryName, milestone.milestoneId)) + .map (t => t.title ~ t.description.? ~ t.dueDate.? ~ t.closedDate.?) + .update (milestone.title, milestone.description, milestone.dueDate, milestone.closedDate) def openMilestone(milestone: Milestone): Unit = updateMilestone(milestone.copy(closedDate = None)) def closeMilestone(milestone: Milestone): Unit = updateMilestone(milestone.copy(closedDate = Some(currentDate))) def deleteMilestone(owner: String, repository: String, milestoneId: Int): Unit = { - Query(Issues) - .filter { t => - (t.userName is owner.bind) && - (t.repositoryName is repository.bind) && - (t.milestoneId is milestoneId.bind) - } - .map(_.milestoneId.?) - .update(None) - - Query(Milestones) - .filter { t => - (t.userName is owner.bind) && - (t.repositoryName is repository.bind) && - (t.milestoneId is milestoneId.bind) - } - .delete + Query(Issues).filter(_.byMilestone(owner, repository, milestoneId)).map(_.milestoneId.?).update(None) + Query(Milestones).filter(_.byPrimaryKey(owner, repository, milestoneId)).delete } def getMilestone(owner: String, repository: String, milestoneId: Int): Option[Milestone] = - Query(Milestones) - .filter { m => - (m.userName is owner.bind) && - (m.repositoryName is repository.bind) && - (m.milestoneId is milestoneId.bind) - } - .sortBy(_.milestoneId desc) - .firstOption + Query(Milestones).filter(_.byPrimaryKey(owner, repository, milestoneId)).firstOption def getMilestonesWithIssueCount(owner: String, repository: String): List[(Milestone, Int, Int)] = { val counts = Issues - .filter { t => - (t.userName is owner.bind) && - (t.repositoryName is repository.bind) && - (t.milestoneId isNotNull) - } - .groupBy { t => - t.milestoneId ~ t.closed - } - .map { case (t1, t2) => - t1._1 ~ t1._2 ~ t2.length - } + .filter { t => (t.byRepository(owner, repository)) && (t.milestoneId isNotNull) } + .groupBy { t => t.milestoneId ~ t.closed } + .map { case (t1, t2) => t1._1 ~ t1._2 ~ t2.length } .list - .map { case (milestoneId, closed, count) => - (milestoneId, closed) -> count - } + .map { case (milestoneId, closed, count) => (milestoneId, closed) -> count } .toMap getMilestones(owner, repository).map { milestone => @@ -86,10 +45,5 @@ } def getMilestones(owner: String, repository: String): List[Milestone] = - Query(Milestones) - .filter { t => - (t.userName is owner.bind) && (t.repositoryName is repository.bind) - } - .sortBy(_.milestoneId desc) - .list + Query(Milestones).filter(_.byRepository(owner, repositoryName)).sortBy(_.milestoneId desc).list } diff --git a/src/main/scala/service/RepositoryService.scala b/src/main/scala/service/RepositoryService.scala index 2a2a9c4..e54b0ae 100644 --- a/src/main/scala/service/RepositoryService.scala +++ b/src/main/scala/service/RepositoryService.scala @@ -39,21 +39,10 @@ } def deleteRepository(userName: String, repositoryName: String): Unit = { - Collaborators - .filter { t => (t.userName is userName.bind) && (t.repositoryName is repositoryName.bind) } - .delete - - IssueId - .filter { t => (t.userName is userName.bind) && (t.repositoryName is repositoryName.bind) } - .delete - - Issues - .filter { t => (t.userName is userName.bind) && (t.repositoryName is repositoryName.bind) } - .delete - - Repositories - .filter { t => (t.userName is userName.bind) && (t.repositoryName is repositoryName.bind) } - .delete + Collaborators .filter(_.byRepository(userName, repositoryName)).delete + IssueId .filter(_.byRepository(userName, repositoryName)).delete + Issues .filter(_.byRepository(userName, repositoryName)).delete + Repositories .filter(_.byRepository(userName, repositoryName)).delete } /** @@ -75,21 +64,21 @@ */ def getVisibleRepositories(userName: String, baseUrl: String, loginUserName: Option[String]): List[RepositoryInfo] = { val q1 = Repositories - .filter { r => r.userName is userName.bind } + .filter { t => t.userName is userName.bind } .map { r => r } val q2 = Collaborators - .innerJoin(Repositories).on((c, r) => (c.userName is r.userName) && (c.repositoryName is r.repositoryName)) - .filter{ case (c, r) => c.collaboratorName is userName.bind} - .map { case (c, r) => r } + .innerJoin(Repositories).on((t1, t2) => t1.byRepository(t2.userName, t2.repositoryName)) + .filter{ case (t1, t2) => t1.collaboratorName is userName.bind} + .map { case (t1, t2) => t2 } - def visibleFor(r: Repositories.type, loginUserName: Option[String]) = { + def visibleFor(t: Repositories.type, loginUserName: Option[String]) = { loginUserName match { - case Some(x) => (r.isPrivate is false.bind) || ( - (r.isPrivate is true.bind) && ((r.userName is x.bind) || (Collaborators.filter { c => - (c.repositoryName is r.repositoryName) && (c.userName is r.userName) && (c.collaboratorName is x.bind) + case Some(x) => (t.isPrivate is false.bind) || ( + (t.isPrivate is true.bind) && ((t.userName is x.bind) || (Collaborators.filter { c => + c.byRepository(t.userName, t.repositoryName) && (c.collaboratorName is x.bind) }.exists))) - case None => (r.isPrivate is false.bind) + case None => (t.isPrivate is false.bind) } } @@ -108,9 +97,7 @@ * @return the repository information */ def getRepository(userName: String, repositoryName: String, baseUrl: String): Option[RepositoryInfo] = { - (Query(Repositories) filter { repository => - (repository.userName is userName.bind) && (repository.repositoryName is repositoryName.bind) - } firstOption) map { repository => + (Query(Repositories) filter { t => t.byRepository(userName, repositoryName) } firstOption) map { repository => val repositoryInfo = JGitUtil.getRepositoryInfo(repository.userName, repository.repositoryName, baseUrl) RepositoryInfo(repositoryInfo.owner, repositoryInfo.name, repositoryInfo.url, repository, repositoryInfo.branchList, repositoryInfo.tags) } @@ -154,19 +141,15 @@ * Updates the last activity date of the repository. */ def updateLastActivityDate(userName: String, repositoryName: String): Unit = - Query(Repositories) - .filter { r => (r.userName is userName.bind) && (r.repositoryName is repositoryName.bind) } - .map { _.lastActivityDate } - .update (currentDate) + Query(Repositories).filter(_.byRepository(userName, repositoryName)).map(_.lastActivityDate).update(currentDate) /** * Save repository options. */ def saveRepositoryOptions(userName: String, repositoryName: String, description: Option[String], defaultBranch: String, isPrivate: Boolean): Unit = - Query(Repositories) - .filter { r => (r.userName is userName.bind) && (r.repositoryName is repositoryName.bind) } - .map { r => r.description.? ~ r.defaultBranch ~ r.isPrivate ~ r.updatedDate } + Query(Repositories).filter(_.byRepository(userName, repositoryName)) + .map { r => r.description.? ~ r.defaultBranch ~ r.isPrivate ~ r.updatedDate } .update (description, defaultBranch, isPrivate, currentDate) /** @@ -187,9 +170,7 @@ * @param collaboratorName the collaborator name */ def removeCollaborator(userName: String, repositoryName: String, collaboratorName: String): Unit = - (Query(Collaborators) filter { c => - (c.userName is userName.bind) && (c.repositoryName is repositoryName.bind) && (c.collaboratorName is collaboratorName.bind) - }).delete + Query(Collaborators).filter(_.byPrimaryKey(userName, repositoryName, collaboratorName)).delete /** @@ -200,9 +181,7 @@ * @return the list of collaborators name */ def getCollaborators(userName: String, repositoryName: String): List[String] = - (Query(Collaborators) filter { c => - (c.userName is userName.bind) && (c.repositoryName is repositoryName.bind) - } sortBy(_.collaboratorName) list) map(_.collaboratorName) + Query(Collaborators).filter(_.byRepository(userName, repositoryName)).sortBy(_.collaboratorName).list.map(_.collaboratorName) def isWritable(owner: String, repository: String, loginAccount: Option[Account]): Boolean = { loginAccount match { @@ -217,6 +196,7 @@ object RepositoryService { - case class RepositoryInfo(owner: String, name: String, url: String, repository: Repository, branchList: List[String], tags: List[util.JGitUtil.TagInfo]) + case class RepositoryInfo(owner: String, name: String, url: String, repository: Repository, + branchList: List[String], tags: List[util.JGitUtil.TagInfo]) } \ No newline at end of file