@@ -9,35 +9,34 @@ import gitbucket.gist.model.Profile.dateColumnType
99
1010trait GistService {
1111
12- def getRecentGists ( userName : String , offset : Int , limit : Int )(implicit s : Session ): Seq [Gist ] =
13- Gists .filter(_.userName === userName.bind ).sortBy(_.registeredDate desc).drop(offset).take(limit).list
12+ def getVisibleGists ( offset : Int , limit : Int , account : Option [ Account ] )(implicit s : Session ): Seq [Gist ] =
13+ visibleHistsQuery(account ).sortBy(_.registeredDate desc).drop(offset).take(limit).list
1414
15- def getVisibleGists (offset : Int , limit : Int , account : Option [Account ])(implicit s : Session ): Seq [Gist ] = {
16- val query = account.map { x =>
15+ def countVisibleGists (account : Option [Account ])(implicit s : Session ): Int =
16+ Query (visibleHistsQuery(account).length).first
17+
18+ private def visibleHistsQuery (account : Option [Account ]): Query [Gists , Gists # TableElementType , Seq ] = {
19+ account.map { x =>
1720 Gists .filter { t => (t.mode === " PUBLIC" .bind) || (t.userName === x.userName.bind) }
1821 } getOrElse {
1922 Gists .filter { t => (t.mode === " PUBLIC" .bind) }
2023 }
21- query.sortBy(_.registeredDate desc).drop(offset).take(limit).list
2224 }
2325
24- def countPublicGists ()(implicit s : Session ): Int =
25- Query (Gists .filter(_.mode === " PUBLIC" .bind).length).first
26-
2726 def getUserGists (userName : String , loginUserName : Option [String ], offset : Int , limit : Int )(implicit s : Session ): Seq [Gist ] =
28- (if (loginUserName.isDefined){
29- Gists filter(t => (t.userName === userName.bind) && ((t.userName === loginUserName.bind) || (t.mode === " PUBLIC" .bind)))
30- } else {
31- Gists filter(t => (t.userName === userName.bind) && (t.mode === " PUBLIC" .bind))
32- }).sortBy(_.registeredDate desc).drop(offset).take(limit).list
27+ userGistsQuery(userName, loginUserName).sortBy(_.registeredDate desc).drop(offset).take(limit).list
3328
3429
3530 def countUserGists (userName : String , loginUserName : Option [String ])(implicit s : Session ): Int =
36- Query ((if (loginUserName.isDefined){
37- Gists .filter(t => (t.userName === userName.bind) && ((t.userName === loginUserName.bind) || (t.mode === " PUBLIC" .bind)))
31+ Query (userGistsQuery(userName, loginUserName).length).first
32+
33+ private def userGistsQuery (userName : String , loginUserName : Option [String ]): Query [Gists , Gists # TableElementType , Seq ] = {
34+ if (loginUserName.isDefined) {
35+ Gists filter (t => (t.userName === userName.bind) && ((t.userName === loginUserName.bind) || (t.mode === " PUBLIC" .bind)))
3836 } else {
39- Gists .filter(t => (t.userName === userName.bind) && (t.mode === " PUBLIC" .bind))
40- }).length).first
37+ Gists filter (t => (t.userName === userName.bind) && (t.mode === " PUBLIC" .bind))
38+ }
39+ }
4140
4241 def getGist (userName : String , repositoryName : String )(implicit s : Session ): Option [Gist ] =
4342 Gists .filter(t => (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind)).firstOption
0 commit comments