Add searching for tasks by index
This commit is contained in:
parent
294fc16593
commit
562ef9af36
3 changed files with 64 additions and 1 deletions
|
@ -1022,6 +1022,19 @@ func TestTaskCollection_ReadAll(t *testing.T) {
|
||||||
},
|
},
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "search for task index",
|
||||||
|
fields: fields{},
|
||||||
|
args: args{
|
||||||
|
search: "number #17",
|
||||||
|
a: &user.User{ID: 1},
|
||||||
|
page: 0,
|
||||||
|
},
|
||||||
|
want: []*Task{
|
||||||
|
task33, // has the index 17
|
||||||
|
},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|
|
@ -18,8 +18,10 @@ package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.vikunja.io/api/pkg/events"
|
"code.vikunja.io/api/pkg/events"
|
||||||
|
@ -237,6 +239,15 @@ func getFilterCondForSeparateTable(table string, concat taskFilterConcatinator,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getTaskIndexFromSearchString(s string) (index int64) {
|
||||||
|
re := regexp.MustCompile("#([0-9]+)")
|
||||||
|
in := re.FindString(s)
|
||||||
|
|
||||||
|
stringIndex := strings.ReplaceAll(in, "#", "")
|
||||||
|
index, _ = strconv.ParseInt(stringIndex, 10, 64)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
//nolint:gocyclo
|
//nolint:gocyclo
|
||||||
func getRawTasksForLists(s *xorm.Session, lists []*List, a web.Auth, opts *taskOptions) (tasks []*Task, resultCount int, totalItems int64, err error) {
|
func getRawTasksForLists(s *xorm.Session, lists []*List, a web.Auth, opts *taskOptions) (tasks []*Task, resultCount int, totalItems int64, err error) {
|
||||||
|
|
||||||
|
@ -356,7 +367,7 @@ func getRawTasksForLists(s *xorm.Session, lists []*List, a web.Auth, opts *taskO
|
||||||
// Then return all tasks for that lists
|
// Then return all tasks for that lists
|
||||||
var where builder.Cond
|
var where builder.Cond
|
||||||
|
|
||||||
if len(opts.search) > 0 {
|
if opts.search != "" {
|
||||||
// Postgres' is case sensitive by default.
|
// Postgres' is case sensitive by default.
|
||||||
// To work around this, we're using ILIKE as opposed to normal LIKE statements.
|
// To work around this, we're using ILIKE as opposed to normal LIKE statements.
|
||||||
// ILIKE is preferred over LOWER(text) LIKE for performance reasons.
|
// ILIKE is preferred over LOWER(text) LIKE for performance reasons.
|
||||||
|
@ -367,6 +378,11 @@ func getRawTasksForLists(s *xorm.Session, lists []*List, a web.Auth, opts *taskO
|
||||||
} else {
|
} else {
|
||||||
where = &builder.Like{"title", "%" + opts.search + "%"}
|
where = &builder.Like{"title", "%" + opts.search + "%"}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
searchIndex := getTaskIndexFromSearchString(opts.search)
|
||||||
|
if searchIndex > 0 {
|
||||||
|
where = builder.Or(where, builder.Eq{"`index`": searchIndex})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var listIDCond builder.Cond
|
var listIDCond builder.Cond
|
||||||
|
|
|
@ -761,3 +761,37 @@ func TestTask_ReadOne(t *testing.T) {
|
||||||
assert.False(t, task.IsFavorite)
|
assert.False(t, task.IsFavorite)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_getTaskIndexFromSearchString(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
wantIndex int64
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "task index in text",
|
||||||
|
args: args{s: "Task #12"},
|
||||||
|
wantIndex: 12,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no task index",
|
||||||
|
args: args{s: "Task"},
|
||||||
|
wantIndex: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "not numeric but with prefix",
|
||||||
|
args: args{s: "Task #aaaaa"},
|
||||||
|
wantIndex: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if gotIndex := getTaskIndexFromSearchString(tt.args.s); gotIndex != tt.wantIndex {
|
||||||
|
t.Errorf("getTaskIndexFromSearchString() = %v, want %v", gotIndex, tt.wantIndex)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue