From 92e5e2db6a03a1bfae47c6b148e062a7cdcf2098 Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 15 May 2020 16:07:36 +0200 Subject: [PATCH] Make the _unix suffix optional when sorting tasks --- pkg/integrations/task_collection_test.go | 16 ++++++++++++++++ pkg/models/task_collection.go | 14 ++++++++++++++ pkg/models/task_collection_filter.go | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pkg/integrations/task_collection_test.go b/pkg/integrations/task_collection_test.go index 56983397..db134b0f 100644 --- a/pkg/integrations/task_collection_test.go +++ b/pkg/integrations/task_collection_test.go @@ -118,6 +118,22 @@ func TestTaskCollection(t *testing.T) { assert.NoError(t, err) assert.Contains(t, rec.Body.String(), `[{"id":5,"text":"task #5 higher due date","description":"","done":false,"done_at":null,"due_date":"2018-12-01T03:58:44Z","reminder_dates":null,"list_id":1,"repeat_after":0,"priority":0,"start_date":null,"end_date":null,"assignees":null,"labels":null,"hex_color":"","percent_done":0,"identifier":"test1-5","index":5,"related_tasks":{},"attachments":null,"created":"2018-12-01T01:12:04Z","updated":"2018-12-01T01:12:04Z","bucket_id":2,"position":0,"created_by":{"id":1,"username":"user1","created":null,"updated":null}},{"id":6,"text":"task #6 lower due date`) }) + // Due date without unix suffix + t.Run("by duedate asc without _unix suffix", func(t *testing.T) { + rec, err := testHandler.testReadAllWithUser(url.Values{"sort_by": []string{"due_date"}, "order_by": []string{"asc"}}, urlParams) + assert.NoError(t, err) + assert.Contains(t, rec.Body.String(), `{"id":6,"text":"task #6 lower due date","description":"","done":false,"done_at":null,"due_date":"2018-11-30T22:25:24Z","reminder_dates":null,"list_id":1,"repeat_after":0,"priority":0,"start_date":null,"end_date":null,"assignees":null,"labels":null,"hex_color":"","percent_done":0,"identifier":"test1-6","index":6,"related_tasks":{},"attachments":null,"created":"2018-12-01T01:12:04Z","updated":"2018-12-01T01:12:04Z","bucket_id":3,"position":0,"created_by":{"id":1,"username":"user1","created":null,"updated":null}},{"id":5,"text":"task #5 higher due date","description":"","done":false,"done_at":null,"due_date":"2018-12-01T03:58:44Z","reminder_dates":null,"list_id":1,"repeat_after":0,"priority":0,"start_date":null,"end_date":null,"assignees":null,"labels":null,"hex_color":"","percent_done":0,"identifier":"test1-5","index":5,"related_tasks":{},"attachments":null,"created":"2018-12-01T01:12:04Z","updated":"2018-12-01T01:12:04Z","bucket_id":2,"position":0,"created_by":{"id":1,"username":"user1","created":null,"updated":null}}]`) + }) + t.Run("by due_date without _unix suffix", func(t *testing.T) { + rec, err := testHandler.testReadAllWithUser(url.Values{"sort_by": []string{"due_date"}}, urlParams) + assert.NoError(t, err) + assert.Contains(t, rec.Body.String(), `{"id":6,"text":"task #6 lower due date","description":"","done":false,"done_at":null,"due_date":"2018-11-30T22:25:24Z","reminder_dates":null,"list_id":1,"repeat_after":0,"priority":0,"start_date":null,"end_date":null,"assignees":null,"labels":null,"hex_color":"","percent_done":0,"identifier":"test1-6","index":6,"related_tasks":{},"attachments":null,"created":"2018-12-01T01:12:04Z","updated":"2018-12-01T01:12:04Z","bucket_id":3,"position":0,"created_by":{"id":1,"username":"user1","created":null,"updated":null}},{"id":5,"text":"task #5 higher due date","description":"","done":false,"done_at":null,"due_date":"2018-12-01T03:58:44Z","reminder_dates":null,"list_id":1,"repeat_after":0,"priority":0,"start_date":null,"end_date":null,"assignees":null,"labels":null,"hex_color":"","percent_done":0,"identifier":"test1-5","index":5,"related_tasks":{},"attachments":null,"created":"2018-12-01T01:12:04Z","updated":"2018-12-01T01:12:04Z","bucket_id":2,"position":0,"created_by":{"id":1,"username":"user1","created":null,"updated":null}}]`) + }) + t.Run("by duedate desc without _unix suffix", func(t *testing.T) { + rec, err := testHandler.testReadAllWithUser(url.Values{"sort_by": []string{"due_date"}, "order_by": []string{"desc"}}, urlParams) + assert.NoError(t, err) + assert.Contains(t, rec.Body.String(), `[{"id":5,"text":"task #5 higher due date","description":"","done":false,"done_at":null,"due_date":"2018-12-01T03:58:44Z","reminder_dates":null,"list_id":1,"repeat_after":0,"priority":0,"start_date":null,"end_date":null,"assignees":null,"labels":null,"hex_color":"","percent_done":0,"identifier":"test1-5","index":5,"related_tasks":{},"attachments":null,"created":"2018-12-01T01:12:04Z","updated":"2018-12-01T01:12:04Z","bucket_id":2,"position":0,"created_by":{"id":1,"username":"user1","created":null,"updated":null}},{"id":6,"text":"task #6 lower due date`) + }) t.Run("by duedate asc", func(t *testing.T) { rec, err := testHandler.testReadAllWithUser(url.Values{"sort_by": []string{"due_date_unix"}, "order_by": []string{"asc"}}, urlParams) assert.NoError(t, err) diff --git a/pkg/models/task_collection.go b/pkg/models/task_collection.go index 6cc72b78..2abe2628 100644 --- a/pkg/models/task_collection.go +++ b/pkg/models/task_collection.go @@ -118,6 +118,20 @@ func (tf *TaskCollection) ReadAll(a web.Auth, search string, page int, perPage i if len(tf.OrderBy) > i { param.orderBy = getSortOrderFromString(tf.OrderBy[i]) } + + // Special case for pseudo date fields + // FIXME: This is really dirty, to fix this properly the db fields should be renamed + switch param.sortBy { + case "done_at": + param.sortBy = taskPropertyDoneAtUnix + case "due_date": + param.sortBy = taskPropertyDueDateUnix + case "start_date": + param.sortBy = taskPropertyStartDateUnix + case "end_date": + param.sortBy = taskPropertyEndDateUnix + } + // Param validation if err := param.validate(); err != nil { return nil, 0, 0, err diff --git a/pkg/models/task_collection_filter.go b/pkg/models/task_collection_filter.go index cfc6de10..839db636 100644 --- a/pkg/models/task_collection_filter.go +++ b/pkg/models/task_collection_filter.go @@ -90,7 +90,7 @@ func getTaskFiltersByCollections(c *TaskCollection) (filters []*taskFilter, err } // Special case for pseudo date fields - // FIXME: This is really dirty, to fix this the db fields should be renamed + // FIXME: This is really dirty, to fix this properly the db fields should be renamed if filter.field+"_unix" == taskPropertyDoneAtUnix || filter.field+"_unix" == taskPropertyDueDateUnix || filter.field+"_unix" == taskPropertyStartDateUnix ||