Permalink
Browse files

Add a Repository model

  • Loading branch information...
1 parent e3c3358 commit fcce1ac8ba62d91806da9da419e7b6b984152875 @Dinduks committed Sep 13, 2012
@@ -17,9 +17,8 @@ object Application extends Controller {
val githubSearchReposPath = Play.current.configuration.getString("github.searchReposPath").get
Async {
- WS.url(githubSearchReposPath.format(githubApiUrl, keyword)).get().map { response =>
- val repos = (response.json \ "repositories")
- Ok(views.html.searchResults(repos.as[List[JsValue]], keyword))
+ Repository.searchRepo(keyword, githubApiUrl, githubSearchReposPath).map { repos =>
+ Ok(views.html.searchResults(repos, keyword))
}
}
}
View
@@ -0,0 +1,3 @@
+package models
+
+class Commit
@@ -0,0 +1,45 @@
+package models
+
+import play.api.libs.ws._
+import play.api.libs.json._
+import play.api.libs.concurrent._
+
+class Repository(
+ val owner: Option[String] = None,
+ val name: Option[String] = None,
+ val language: Option[String] = None,
+ val description: Option[String] = None,
+ val commits: Option[Commit] = None,
+ val contributors: Option[Contributor] = None
+)
+
+object Repository {
+ def apply(
+ owner: Option[String] = None,
+ name: Option[String] = None,
+ language: Option[String] = None,
+ description: Option[String] = None
+ ) = {
+ new Repository(owner, name, language, description, None, None)
+ }
+
+ def searchRepo(keyword: String, githubApiUrl: String, githubSearchReposPath: String): Promise[List[Repository]] = {
+ WS.url(githubSearchReposPath.format(githubApiUrl, keyword)).get().map { response =>
+ val json = response.json \ "repositories"
+ val owner = json \ "owner"
+ val name = json \ "name"
+ val language = json \ "language"
+ val description = json \ "description"
+
+ (response.json \ "repositories").as[List[JsValue]].map { repo =>
+ Repository(
+ (repo \ "owner").asOpt[String],
+ (repo \ "name").asOpt[String],
+ (repo \ "language").asOpt[String],
+ (repo \ "description").asOpt[String]
+ )
+ }
+ }
+ }
+}
+
@@ -1,8 +1,8 @@
-@(username: play.api.libs.json.JsValue, repositoryName: play.api.libs.json.JsValue)
+@(repo: Repository)
@import revontulet.Util
-@defining(Util.trim(username.toString, '"') + "/" + Util.trim(repositoryName.toString, '"')) { repositoryFullName =>
+@defining(Util.trim(repo.owner.get.toString, '"') + "/" + Util.trim(repo.name.get.toString, '"')) { repositoryFullName =>
<a href="/@repositoryFullName">
@repositoryFullName
</a>
@@ -1,12 +1,12 @@
-@(repos: List[play.api.libs.json.JsValue], keyword: String)
+@(repos: List[Repository], keyword: String)
@import views.html.helper._
@main("Search results for " + keyword) {
<ul>
@for(repo <- repos) {
<li>
- @repositoryLink(repo \ "owner", repo \ "name")
+ @repositoryLink(repo)
</li>
}
</ul>

0 comments on commit fcce1ac

Please sign in to comment.