From 5988bc508f5be772727ef62913725802e0be3ad9 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 6 Sep 2020 19:56:01 +0200 Subject: [PATCH] Add getting tasks from a saved filter --- pkg/models/saved_filters.go | 14 ++++++++++++++ pkg/models/task_collection.go | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/pkg/models/saved_filters.go b/pkg/models/saved_filters.go index 2bfd41fce..07c950f9f 100644 --- a/pkg/models/saved_filters.go +++ b/pkg/models/saved_filters.go @@ -51,6 +51,17 @@ func (s *SavedFilter) TableName() string { return "saved_filters" } +func (s *SavedFilter) getTaskCollection() *TaskCollection { + // We're resetting the listID to return tasks from all lists + s.Filters.ListID = 0 + return s.Filters +} + +func getSavedFilterIDFromListID(listID int64) int64 { + // We get the id of the saved filter by multiplying the ListID with -1 and subtracting one + return listID*-1 - 1 +} + func (s *SavedFilter) Create(auth web.Auth) error { s.OwnerID = auth.GetID() _, err := x.Insert(s) @@ -69,7 +80,10 @@ func (s *SavedFilter) ReadOne() error { return err } +// ReadAll returns all tasks of a saved filter. +// Since saved filters are shown in a pseudo-namespace, we can use this function to get all tasks func (s *SavedFilter) ReadAll(auth web.Auth, search string, page int, perPage int) (result interface{}, resultCount int, numberOfTotalItems int64, err error) { + return } func (s *SavedFilter) Update() error { diff --git a/pkg/models/task_collection.go b/pkg/models/task_collection.go index a7eb1d184..00aaeb9fa 100644 --- a/pkg/models/task_collection.go +++ b/pkg/models/task_collection.go @@ -102,6 +102,17 @@ func validateTaskField(fieldName string) error { // @Router /lists/{listID}/tasks [get] func (tf *TaskCollection) ReadAll(a web.Auth, search string, page int, perPage int) (result interface{}, resultCount int, totalItems int64, err error) { + // If the list id is < -1 this means we're dealing with a saved filter - in that case we get and populate the filter + // -1 is the favorites list which works as intended + if tf.ListID < -1 { + s, err := getSavedFilterSimpleByID(getSavedFilterIDFromListID(tf.ListID)) + if err != nil { + return nil, 0, 0, err + } + + return s.getTaskCollection().ReadAll(a, search, page, perPage) + } + if len(tf.SortByArr) > 0 { tf.SortBy = append(tf.SortBy, tf.SortByArr...) }