diff --git a/src/main/scala/app/IndexController.scala b/src/main/scala/app/IndexController.scala index 4abcf6d..c855255 100644 --- a/src/main/scala/app/IndexController.scala +++ b/src/main/scala/app/IndexController.scala @@ -8,6 +8,7 @@ import org.eclipse.jgit.revwalk.RevWalk import scala.collection.mutable.ListBuffer import org.eclipse.jgit.lib.FileMode +import model.Issue class IndexController extends IndexControllerBase with RepositoryService with AccountService with SystemSettingsService with ActivityService with IssuesService @@ -40,11 +41,18 @@ } get("/:owner/:repository/search")(referrersOnly { repository => + import RepositorySearch._ val query = params("q").trim val target = params.getOrElse("type", "code") + val page = try { + val i = params.getOrElse("page", "1").toInt + if(i <= 0) 1 else i + } catch { + case e: NumberFormatException => 1 + } - val issues = if(query.isEmpty) Nil else searchIssuesByKeyword(repository.owner, repository.name, query) - val files = if(query.isEmpty) Nil else searchRepositoryFiles(repository.owner, repository.name, query) + + val SearchResult(files, issues) = searchRepository(repository.owner, repository.name, query) target.toLowerCase match { case "issue" => @@ -56,19 +64,34 @@ issue.registeredDate, commentCount, getHighlightText(content, query)._1) - }, files.size, query, repository) + }, files.size, query, page, repository) case _ => JGitUtil.withGit(getRepositoryDir(repository.owner, repository.name)){ git => val commits = JGitUtil.getLatestCommitFromPaths(git, files.toList.map(_._1), "HEAD") search.html.code(files.toList.map { case (path, text) => val (highlightText, lineNumber) = getHighlightText(text, query) - FileSearchResult(path, commits(path).getCommitterIdent.getWhen, highlightText, lineNumber) - }, issues.size, query, repository) + FileSearchResult( + path, + commits(path).getCommitterIdent.getWhen, + highlightText, + lineNumber) + }, issues.size, query, page, repository) } } }) + case class SearchResult( + files: List[(String, String)], + issues: List[(Issue, Int, String)] + ) + + def searchRepository(owner: String, repository: String, query: String): SearchResult = { + val issues = if(query.isEmpty) Nil else searchIssuesByKeyword(owner, repository, query) + val files = if(query.isEmpty) Nil else searchRepositoryFiles(owner, repository, query) + SearchResult(files, issues) + } + private def searchRepositoryFiles(owner: String, repository: String, query: String): List[(String, String)] = { JGitUtil.withGit(getRepositoryDir(owner, repository)){ git => val revWalk = new RevWalk(git.getRepository) @@ -102,6 +125,7 @@ } } + private def getHighlightText(content: String, query: String): (String, Int) = { val keywords = StringUtil.splitWords(query.toLowerCase) val lowerText = content.toLowerCase @@ -120,7 +144,6 @@ } - case class IssueSearchResult( issueId: Int, title: String, @@ -133,4 +156,9 @@ path: String, lastModified: java.util.Date, highlightText: String, - highlightLineNumber: Int) \ No newline at end of file + highlightLineNumber: Int) + +object RepositorySearch extends IssuesService { + val CodeLimit = 10 + val IssueLimit = 10 +} diff --git a/src/main/twirl/helper/paginator.scala.html b/src/main/twirl/helper/paginator.scala.html index 4a37d81..0925004 100644 --- a/src/main/twirl/helper/paginator.scala.html +++ b/src/main/twirl/helper/paginator.scala.html @@ -1,32 +1,32 @@ @(page: Int, count: Int, limit: Int, width: Int, baseURL: String) -@defining(view.Pagination(page, count, service.IssuesService.IssueLimit, width)){ p => +@defining(view.Pagination(page, count, limit, width)){ p => @if(p.count > p.limit){
@Html(issue.highlightText)