Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor

  • Loading branch information...
commit 8acbf0ebcc7e29818f46f2ef3a744a111fc64746 1 parent 38cf94b
@Dinduks authored
View
22 app/controllers/Application.scala
@@ -12,25 +12,37 @@ object Application extends Controller {
Ok(views.html.index())
}
- def search(keyword: String) = Action {
+ def getGitHubApiConfig: (String, String, String) = {
val githubApiUrl = Play.current.configuration.getString("github.apiUrl").get
val githubSearchReposPath = Play.current.configuration.getString("github.searchReposPath").get
+ val githubGetRepoCommitsPath = Play.current.configuration.getString("github.getRepoCommitsPath").get
+
+ (githubApiUrl, githubSearchReposPath, githubGetRepoCommitsPath)
+ }
+
+ def search(keyword: String) = Action {
+ val (githubApiUrl, githubSearchReposPath, githubGetRepoCommitsPath) = getGitHubApiConfig
Async {
Repository.searchRepo(keyword, githubApiUrl, githubSearchReposPath).map { repos =>
- Ok(views.html.searchResults(repos, keyword))
+ repos match {
+ case Nil => NotFound(views.html.searchResultsNotFound(keyword))
+ case _ => Ok(views.html.searchResultsOk(repos, keyword))
+ }
}
}
}
def repoInfo(username: String, repositoryName: String) = Action {
- val githubApiUrl = Play.current.configuration.getString("github.apiUrl").get
- val githubGetRepoCommitsPath = Play.current.configuration.getString("github.getRepoCommitsPath").get
+ val (githubApiUrl, githubSearchReposPath, githubGetRepoCommitsPath) = getGitHubApiConfig
val repo = new Repository(Some(username), Some(repositoryName), None, None)
Async {
repo.getAllInfo(githubApiUrl, githubGetRepoCommitsPath).map { repo =>
- Ok(views.html.showRepoInfo(repo))
+ repo match {
+ case None => NotFound(views.html.showRepoInfoNotFound())
+ case Some(repo) => Ok(views.html.showRepoInfoOk(repo))
+ }
}
}
}
View
12 app/models/Commit.scala
@@ -1,18 +1,8 @@
package models
-class Commit(
+case class Commit(
val message: Option[String] = None,
val sha: Option[String] = None,
val author: Option[User] = None
)
-object Commit {
- def apply(
- message: Option[String] = None,
- sha: Option[String] = None,
- author: Option[User] = None
- ) = {
- new Commit(message, sha, author)
- }
-}
-
View
38 app/models/Contributor.scala
@@ -2,42 +2,16 @@ package models
import revontulet.Util
-class Contributor(
- username: Option[String] = None,
- name: Option[String] = None,
- email: Option[String] = None,
- url: Option[String] = None,
- avatarUrl: Option[String] = None,
+case class Contributor(
+ override val username: Option[String] = None,
+ override val name: Option[String] = None,
+ override val email: Option[String] = None,
+ override val avatarUrl: Option[String] = None,
var contributionsCounter: Int = 0
) extends User(
username,
name,
email,
- url,
avatarUrl
-) {
-}
+)
-object Contributor {
- def apply(
- username: Option[String],
- name: Option[String],
- email: Option[String],
- avatarUrl: Option[String],
- contributionsCounter: Int
- ) = {
- val url = username match {
- case None => None
- case Some(username) => Some("https://github.com/%s" format Util.trim(username, '"'))
- }
-
- new Contributor(
- username,
- name,
- email,
- url,
- avatarUrl,
- contributionsCounter
- )
- }
-}
View
25 app/models/Repository.scala
@@ -4,7 +4,7 @@ import play.api.libs.ws._
import play.api.libs.json._
import play.api.libs.concurrent._
-class Repository(
+case class Repository(
val owner: Option[String] = None,
val name: Option[String] = None,
val language: Option[String] = None,
@@ -12,28 +12,21 @@ class Repository(
val commits: List[Commit] = List(),
val contributors: List[Contributor] = List()
) {
- def getAllInfo(githubApiUrl: String, githubGetRepoCommitsPath: String): Promise[Repository] = {
+ def getAllInfo(githubApiUrl: String, githubGetRepoCommitsPath: String): Promise[Option[Repository]] = {
WS.url(githubGetRepoCommitsPath.format(githubApiUrl, owner.get, name.get)).get().map { response =>
- val commits = Repository.parseJsonCommits(response.json.as[List[JsValue]])
- val contributors = Repository.computeContributors(commits)
+ if (response.json.toString == """{"message":"Not Found"}""") {
+ None
+ } else {
+ val commits = Repository.parseJsonCommits(response.json.as[List[JsValue]])
+ val contributors = Repository.computeContributors(commits)
- Repository(this.owner, this.name, this.language, this.description, commits, contributors)
+ Some(Repository(this.owner, this.name, this.language, this.description, commits, contributors))
+ }
}
}
}
object Repository {
- def apply(
- owner: Option[String] = None,
- name: Option[String] = None,
- language: Option[String] = None,
- description: Option[String] = None,
- commits: List[Commit] = List(),
- contributors: List[Contributor] = List()
- ) = {
- new Repository(owner, name, language, description, commits, contributors)
- }
-
def searchRepo(keyword: String, githubApiUrl: String, githubSearchReposPath: String): Promise[List[Repository]] = {
WS.url(githubSearchReposPath.format(githubApiUrl, keyword)).get().map { response =>
(response.json \ "repositories").as[List[JsValue]].map { repo =>
View
13 app/models/User.scala
@@ -6,24 +6,19 @@ class User(
val username: Option[String] = None,
val name: Option[String] = None,
val email: Option[String] = None,
- val url: Option[String] = None,
val avatarUrl: Option[String] = None
-)
+) {
+ lazy val url = username.map (u => "https://github.com/%s" format Util.trim(u, '"'))
+}
object User {
def apply(
username: Option[String] = None,
name: Option[String] = None,
email: Option[String] = None,
- url: Option[String] = None,
avatarUrl: Option[String] = None
) = {
- val url = username match {
- case None => None
- case Some(username) => Some("https://github.com/%s" format Util.trim(username, '"'))
- }
-
- new User(username, name, email, url, avatarUrl)
+ new User(username, name, email, avatarUrl)
}
}
View
2  app/views/helper/repositoryLink.scala.html
@@ -3,7 +3,7 @@
@import revontulet.Util
@defining(Util.trim(repo.owner.getOrElse("").toString, '"') + "/" + Util.trim(repo.name.getOrElse("").toString, '"')) { repositoryFullName =>
- <a href="/@repositoryFullName">
+ <a href="@routes.Application.repoInfo(repo.owner.get, repo.name.get)">
@repositoryFullName
</a>
}
View
15 app/views/searchResults.scala.html
@@ -1,15 +0,0 @@
-@(repos: List[Repository], keyword: String)
-
-@import views.html.helper._
-
-@main("Search results for " + keyword) {
-<h2>@("Search results for %s".format(keyword))</h2>
-<ul>
- @for(repo <- repos) {
- <li>
- @repositoryLink(repo)
- </li>
- }
-</ul>
-}
-
View
9 app/views/searchResultsNotFound.scala.html
@@ -0,0 +1,9 @@
+@(keyword: String)
+
+@import views.html.helper._
+
+@main("Search results for " + keyword) {
+ <h2>@("Search results for %s".format(keyword))</h2>
+ <p>No result was found. :(</p>
+}
+
View
15 app/views/searchResultsOk.scala.html
@@ -0,0 +1,15 @@
+@(repos: List[Repository], keyword: String)
+
+@import views.html.helper._
+
+@main("Search results for " + keyword) {
+ <h2>@("Search results for %s".format(keyword))</h2>
+ <ul>
+ @for(repo <- repos) {
+ <li>
+ @repositoryLink(repo)
+ </li>
+ }
+ </ul>
+}
+
View
18 app/views/showRepoInfo.scala.html
@@ -1,18 +0,0 @@
-@(repo: Repository)
-
-@import views.html.helper._
-
-@main("%s/%s - Repository info".format(repo.owner.get, repo.name.get)) {
-<h1>@("%s/%s - Repository info".format(repo.owner.get, repo.name.get))</h1>
- <h2>Contributors</h2>
- <div>
- <ul class="contributors-list">
- @for(contributor <- repo.contributors) {
- <li>
- @contributorInfo(contributor, repo.commits.length)
- </li>
- }
- </ul>
- </div>
-}
-
View
5 app/views/showRepoInfoNotFound.scala.html
@@ -0,0 +1,5 @@
+@import views.html.helper._
+
+@main("Repository not found") {
+ <h1>This repo was not found. :(</h1>
+}
View
18 app/views/showRepoInfoOk.scala.html
@@ -0,0 +1,18 @@
+@(repo: Repository)
+
+@import views.html.helper._
+
+@main("%s/%s - Repository info".format(repo.owner.get, repo.name.get)) {
+ <h1>@("%s/%s".format(repo.owner.get, repo.name.get)) - Repository info</h1>
+ <h2>Contributors</h2>
+ <div>
+ <ul class="contributors-list">
+ @for(contributor <- repo.contributors) {
+ <li>
+ @contributorInfo(contributor, repo.commits.length)
+ </li>
+ }
+ </ul>
+ </div>
+}
+
View
2  public/javascripts/revontulet.js
@@ -2,7 +2,7 @@
$(document).ready(function () {
$("#searchForm").submit(function () {
- window.location.href = "/search/" + $("#keyword").val();
+ if ($("#keyword").val()) window.location.href = "/search/" + $("#keyword").val();
return false;
});
});
View
5 public/stylesheets/main.css
@@ -26,3 +26,8 @@ ul li {
line-height: 1.5em;
}
+.no-repo-found-message {
+ font-size: 1.2em;
+ line-height: 1.5em;
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.