Skip to content

Commit

Permalink
fix: Ordering repository by name instead of creation date (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ViBiOh authored Apr 20, 2020
1 parent ba1577e commit da675f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/renderer/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func (a app) getData(r *http.Request) (interface{}, error) {
targets, _, err := a.targetApp.List(r.Context(), 1, 100, "", false, nil)
targets, _, err := a.targetApp.List(r.Context(), 1, 100, "repository", true, nil)

return targets, err
}
Expand Down
13 changes: 9 additions & 4 deletions pkg/target/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package target
import (
"database/sql"
"fmt"
"regexp"

"github.com/ViBiOh/httputils/v3/pkg/db"
"github.com/ViBiOh/ketchup/pkg/model"
)

var (
sortKeyMatcher = regexp.MustCompile(`[A-Za-z0-9]+`)
)

func scanTarget(row model.RowScanner) (Target, error) {
var target Target

Expand Down Expand Up @@ -53,7 +58,7 @@ SELECT
count(id) OVER() AS full_count
FROM
target
ORDER BY $3
ORDER BY %s
LIMIT $1
OFFSET $2
`
Expand All @@ -62,16 +67,16 @@ OFFSET $2
func (a app) listTargets(page, pageSize uint, sortKey string, sortAsc bool) ([]Target, uint, error) {
order := "creation_date DESC"

if sortKey != "" {
if sortKeyMatcher.MatchString(sortKey) {
order = sortKey
}
if !sortAsc {
order = fmt.Sprintf("%s DESC", order)
order += " DESC"
}

offset := (page - 1) * pageSize

rows, err := a.db.Query(listQuery, pageSize, offset, order)
rows, err := a.db.Query(fmt.Sprintf(listQuery, order), pageSize, offset)
if err != nil {
return nil, 0, err
}
Expand Down

0 comments on commit da675f5

Please sign in to comment.