diff options
Diffstat (limited to 'pkg/handler')
| -rw-r--r-- | pkg/handler/git/handler.go | 28 | 
1 files changed, 27 insertions, 1 deletions
diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index 436d364..9c7ba5b 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -9,8 +9,10 @@ import (  	"net/http"  	"os"  	"path/filepath" +	"sort"  	"strings" +	"git.gabrielgio.me/cerrado/pkg/config"  	"git.gabrielgio.me/cerrado/pkg/ext"  	"git.gabrielgio.me/cerrado/pkg/service"  	"git.gabrielgio.me/cerrado/pkg/u" @@ -34,6 +36,7 @@ type (  	configurationRepository interface {  		GetRootReadme() string  		GetSyntaxHighlight() string +		GetOrderBy() config.OrderBy  	}  ) @@ -79,7 +82,7 @@ func (g *GitHandler) List(w http.ResponseWriter, r *http.Request) error {  	bs = markdown.Render(doc, renderer)  	gitList := &templates.GitListPage{ -		Respositories: repos, +		Respositories: orderBy(repos, g.config.GetOrderBy()),  		About:         bs,  	}  	templates.WritePageTemplate(w, gitList, r.Context()) @@ -390,3 +393,26 @@ func GetLexers(filename string) chroma.Lexer {  func isPublic(r *service.Repository) bool {  	return r.Public  } + +func orderBy(repos []*service.Repository, order config.OrderBy) []*service.Repository { +	switch order { +	case config.AlphabeticalAsc: +		sort.Slice(repos, func(i, j int) bool { +			return repos[i].Name < repos[j].Name +		}) +	case config.AlphabeticalDesc: +		sort.Slice(repos, func(i, j int) bool { +			return repos[i].Name > repos[j].Name +		}) +	case config.LastCommitAsc: +		sort.Slice(repos, func(i, j int) bool { +			return repos[i].LastCommit.Committer.When.Before(repos[j].LastCommit.Committer.When) +		}) +	case config.LastCommitDesc: +		sort.Slice(repos, func(i, j int) bool { +			return repos[i].LastCommit.Committer.When.After(repos[j].LastCommit.Committer.When) +		}) +	} + +	return repos +}  | 
