diff --git a/pkg/models/task_collection.go b/pkg/models/task_collection.go index 13b42e681..a0c56a15f 100644 --- a/pkg/models/task_collection.go +++ b/pkg/models/task_collection.go @@ -91,7 +91,7 @@ func validateTaskField(fieldName string) error { // @Param s query string false "Search tasks by task text." // @Param sort_by query string false "The sorting parameter. You can pass this multiple times to get the tasks ordered by multiple different parametes, along with `order_by`. Possible values to sort by are `id`, `title`, `description`, `done`, `done_at`, `due_date`, `created_by_id`, `list_id`, `repeat_after`, `priority`, `start_date`, `end_date`, `hex_color`, `percent_done`, `uid`, `created`, `updated`. Default is `id`." // @Param order_by query string false "The ordering parameter. Possible values to order by are `asc` or `desc`. Default is `asc`." -// @Param filter_by query string false "The name of the field to filter by. Allowed values are all task properties except `list` and `namespace`. Task properties which are their own object require passing in the id of that entity. Accepts an array for multiple filters which will be chanied together, all supplied filter must match." +// @Param filter_by query string false "The name of the field to filter by. Allowed values are all task properties. Task properties which are their own object require passing in the id of that entity. Accepts an array for multiple filters which will be chanied together, all supplied filter must match." // @Param filter_value query string false "The value to filter for." // @Param filter_comparator query string false "The comparator to use for a filter. Available values are `equals`, `greater`, `greater_equals`, `less`, `less_equals`, `like` and `in`. `in` expects comma-separated values in `filter_value`. Defaults to `equals`" // @Param filter_concat query string false "The concatinator to use for filters. Available values are `and` or `or`. Defaults to `or`." diff --git a/pkg/models/task_collection_filter.go b/pkg/models/task_collection_filter.go index e7cec10c3..aed3b1c30 100644 --- a/pkg/models/task_collection_filter.go +++ b/pkg/models/task_collection_filter.go @@ -190,6 +190,24 @@ func getNativeValueForTaskField(fieldName string, comparator taskFilterComparato realFieldName := strings.ReplaceAll(strcase.ToCamel(fieldName), "Id", "ID") + if realFieldName == "Namespace" { + if comparator == taskFilterComparatorIn { + vals := strings.Split(value, ",") + valueSlice := []interface{}{} + for _, val := range vals { + v, err := strconv.ParseInt(val, 10, 64) + if err != nil { + return nil, err + } + valueSlice = append(valueSlice, v) + } + return valueSlice, nil + } + + nativeValue, err = strconv.ParseInt(value, 10, 64) + return + } + field, ok := reflect.TypeOf(&Task{}).Elem().FieldByName(realFieldName) if !ok { return nil, ErrInvalidTaskField{TaskField: fieldName} diff --git a/pkg/models/task_collection_test.go b/pkg/models/task_collection_test.go index 4e20b985e..b5925ab9b 100644 --- a/pkg/models/task_collection_test.go +++ b/pkg/models/task_collection_test.go @@ -954,6 +954,33 @@ func TestTaskCollection_ReadAll(t *testing.T) { }, wantErr: false, }, + { + name: "filter namespace", + fields: fields{ + FilterBy: []string{"namespace"}, + FilterValue: []string{"7"}, + FilterComparator: []string{"equals"}, + }, + args: defaultArgs, + want: []*Task{ + task21, + }, + wantErr: false, + }, + { + name: "filter namespace in", + fields: fields{ + FilterBy: []string{"namespace"}, + FilterValue: []string{"7,8"}, + FilterComparator: []string{"in"}, + }, + args: defaultArgs, + want: []*Task{ + task21, + task22, + }, + wantErr: false, + }, } for _, tt := range tests { diff --git a/pkg/models/tasks.go b/pkg/models/tasks.go index 6e4f1ddae..dab48ac8a 100644 --- a/pkg/models/tasks.go +++ b/pkg/models/tasks.go @@ -144,7 +144,7 @@ type taskOptions struct { // @Param s query string false "Search tasks by task text." // @Param sort_by query string false "The sorting parameter. You can pass this multiple times to get the tasks ordered by multiple different parametes, along with `order_by`. Possible values to sort by are `id`, `title`, `description`, `done`, `done_at`, `due_date`, `created_by_id`, `list_id`, `repeat_after`, `priority`, `start_date`, `end_date`, `hex_color`, `percent_done`, `uid`, `created`, `updated`. Default is `id`." // @Param order_by query string false "The ordering parameter. Possible values to order by are `asc` or `desc`. Default is `asc`." -// @Param filter_by query string false "The name of the field to filter by. Allowed values are all task properties except `list` and `namespace`. Task properties which are their own object require passing in the id of that entity. Accepts an array for multiple filters which will be chanied together, all supplied filter must match." +// @Param filter_by query string false "The name of the field to filter by. Allowed values are all task properties. Task properties which are their own object require passing in the id of that entity. Accepts an array for multiple filters which will be chanied together, all supplied filter must match." // @Param filter_value query string false "The value to filter for." // @Param filter_comparator query string false "The comparator to use for a filter. Available values are `equals`, `greater`, `greater_equals`, `less`, `less_equals`, `like` and `in`. `in` expects comma-separated values in `filter_value`. Defaults to `equals`" // @Param filter_concat query string false "The concatinator to use for filters. Available values are `and` or `or`. Defaults to `or`." @@ -271,6 +271,7 @@ func getRawTasksForLists(lists []*List, a web.Auth, opts *taskOptions) (tasks [] reminderFilters := []builder.Cond{} assigneeFilters := []builder.Cond{} labelFilters := []builder.Cond{} + namespaceFilters := []builder.Cond{} var filters = make([]builder.Cond, 0, len(opts.filters)) // To still find tasks with nil values, we exclude 0s when comparing with >/< values. @@ -305,6 +306,16 @@ func getRawTasksForLists(lists []*List, a web.Auth, opts *taskOptions) (tasks [] continue } + if f.field == "namespace" { + f.field = "namespace_id" + filter, err := getFilterCond(f, opts.filterIncludeNulls) + if err != nil { + return nil, 0, 0, err + } + namespaceFilters = append(namespaceFilters, filter) + continue + } + filter, err := getFilterCond(f, opts.filterIncludeNulls) if err != nil { return nil, 0, 0, err @@ -369,6 +380,25 @@ func getRawTasksForLists(lists []*List, a web.Auth, opts *taskOptions) (tasks [] filters = append(filters, getFilterCondForSeparateTable("label_task", opts.filterConcat, labelFilters)) } + if len(namespaceFilters) > 0 { + var filtercond builder.Cond + if opts.filterConcat == filterConcatOr { + filtercond = builder.Or(namespaceFilters...) + } + if opts.filterConcat == filterConcatAnd { + filtercond = builder.And(namespaceFilters...) + } + + cond := builder.In( + "list_id", + builder. + Select("id"). + From("list"). + Where(filtercond), + ) + filters = append(filters, cond) + } + query = query.Where(listCond) queryCount = queryCount.Where(listCond)