From 6ce9527e3a7c8da766b0d04d170d903531bdb6d8 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 20 Dec 2020 13:00:58 +0100 Subject: [PATCH 1/2] Add task filter for labels --- pkg/models/task_collection_test.go | 41 ++++++++++++++++++------------ pkg/models/tasks.go | 15 +++++++++++ 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/pkg/models/task_collection_test.go b/pkg/models/task_collection_test.go index 3a3623d83..664ab6386 100644 --- a/pkg/models/task_collection_test.go +++ b/pkg/models/task_collection_test.go @@ -62,6 +62,15 @@ func TestTaskCollection_ReadAll(t *testing.T) { loc := config.GetTimeZone() + label4 := &Label{ + ID: 4, + Title: "Label #4 - visible via other task", + CreatedByID: 2, + CreatedBy: user2, + Created: testCreatedTime, + Updated: testUpdatedTime, + } + // We use individual variables for the tasks here to be able to rearrange or remove ones more easily task1 := &Task{ ID: 1, @@ -75,14 +84,7 @@ func TestTaskCollection_ReadAll(t *testing.T) { BucketID: 1, IsFavorite: true, Labels: []*Label{ - { - ID: 4, - Title: "Label #4 - visible via other task", - CreatedByID: 2, - CreatedBy: user2, - Created: testCreatedTime, - Updated: testUpdatedTime, - }, + label4, }, RelatedTasks: map[RelationKind][]*Task{ RelationKindSubtask: { @@ -137,14 +139,7 @@ func TestTaskCollection_ReadAll(t *testing.T) { ListID: 1, BucketID: 1, Labels: []*Label{ - { - ID: 4, - Title: "Label #4 - visible via other task", - CreatedByID: 2, - CreatedBy: user2, - Created: testCreatedTime, - Updated: testUpdatedTime, - }, + label4, }, RelatedTasks: map[RelationKind][]*Task{}, Created: time.Unix(1543626724, 0).In(loc), @@ -932,6 +927,20 @@ func TestTaskCollection_ReadAll(t *testing.T) { }, wantErr: false, }, + { + name: "filter labels", + fields: fields{ + FilterBy: []string{"labels"}, + FilterValue: []string{"4"}, + FilterComparator: []string{"equals"}, + }, + args: defaultArgs, + want: []*Task{ + task1, + task2, + }, + wantErr: false, + }, } for _, tt := range tests { diff --git a/pkg/models/tasks.go b/pkg/models/tasks.go index f52352d6f..5224c2644 100644 --- a/pkg/models/tasks.go +++ b/pkg/models/tasks.go @@ -270,6 +270,7 @@ func getRawTasksForLists(lists []*List, a web.Auth, opts *taskOptions) (tasks [] // Some filters need a special treatment since they are in a separate table reminderFilters := []builder.Cond{} assigneeFilters := []builder.Cond{} + labelFilters := []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. @@ -294,6 +295,16 @@ func getRawTasksForLists(lists []*List, a web.Auth, opts *taskOptions) (tasks [] continue } + if f.field == "labels" { + f.field = "label_id" + filter, err := getFilterCond(f, opts.filterIncludeNulls) + if err != nil { + return nil, 0, 0, err + } + labelFilters = append(labelFilters, filter) + continue + } + filter, err := getFilterCond(f, opts.filterIncludeNulls) if err != nil { return nil, 0, 0, err @@ -354,6 +365,10 @@ func getRawTasksForLists(lists []*List, a web.Auth, opts *taskOptions) (tasks [] filters = append(filters, getFilterCondForSeparateTable("task_assignees", opts.filterConcat, assigneeFilters)) } + if len(labelFilters) > 0 { + filters = append(filters, getFilterCondForSeparateTable("label_task", opts.filterConcat, labelFilters)) + } + query = query.Where(listCond) queryCount = queryCount.Where(listCond) -- 2.40.1 From 636b9c95ccbdaea595bc99434d4f3e501bdbbe86 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 20 Dec 2020 13:03:46 +0100 Subject: [PATCH 2/2] Update docs --- pkg/models/task_collection.go | 2 +- pkg/models/tasks.go | 2 +- pkg/swagger/docs.go | 4 ++-- pkg/swagger/swagger.json | 4 ++-- pkg/swagger/swagger.yaml | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/models/task_collection.go b/pkg/models/task_collection.go index 0aedeb30b..13b42e681 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 `labels`, `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 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_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/tasks.go b/pkg/models/tasks.go index 5224c2644..6e4f1ddae 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 `labels`, `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 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_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/swagger/docs.go b/pkg/swagger/docs.go index 734509c23..22ef34cad 100644 --- a/pkg/swagger/docs.go +++ b/pkg/swagger/docs.go @@ -1892,7 +1892,7 @@ var doc = `{ }, { "type": "string", - "description": "The name of the field to filter by. Allowed values are all task properties except ` + "`" + `labels` + "`" + `, ` + "`" + `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.", + "description": "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.", "name": "filter_by", "in": "query" }, @@ -4006,7 +4006,7 @@ var doc = `{ }, { "type": "string", - "description": "The name of the field to filter by. Allowed values are all task properties except ` + "`" + `labels` + "`" + `, ` + "`" + `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.", + "description": "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.", "name": "filter_by", "in": "query" }, diff --git a/pkg/swagger/swagger.json b/pkg/swagger/swagger.json index f1579ca9b..e500da9e6 100644 --- a/pkg/swagger/swagger.json +++ b/pkg/swagger/swagger.json @@ -1875,7 +1875,7 @@ }, { "type": "string", - "description": "The name of the field to filter by. Allowed values are all task properties except `labels`, `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.", + "description": "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.", "name": "filter_by", "in": "query" }, @@ -3989,7 +3989,7 @@ }, { "type": "string", - "description": "The name of the field to filter by. Allowed values are all task properties except `labels`, `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.", + "description": "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.", "name": "filter_by", "in": "query" }, diff --git a/pkg/swagger/swagger.yaml b/pkg/swagger/swagger.yaml index ab165aa2e..e520a2ead 100644 --- a/pkg/swagger/swagger.yaml +++ b/pkg/swagger/swagger.yaml @@ -2443,7 +2443,7 @@ paths: in: query name: order_by type: string - - description: The name of the field to filter by. Allowed values are all task properties except `labels`, `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. + - description: 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. in: query name: filter_by type: string @@ -4467,7 +4467,7 @@ paths: in: query name: order_by type: string - - description: The name of the field to filter by. Allowed values are all task properties except `labels`, `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. + - description: 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. in: query name: filter_by type: string -- 2.40.1