Fix setting a saved filter as favorite

This commit is contained in:
kolaente 2021-07-20 21:32:25 +02:00
parent c7f337f303
commit d9b38b85f6
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
4 changed files with 46 additions and 19 deletions

View file

@ -464,6 +464,10 @@ func addListDetails(s *xorm.Session, lists []*List, a web.Auth) (err error) {
} }
for _, list := range lists { for _, list := range lists {
// Don't override the favorite state if it was already set from before (favorite saved filters do this)
if list.IsFavorite {
continue
}
list.IsFavorite = favs[list.ID] list.IsFavorite = favs[list.ID]
} }
@ -636,6 +640,7 @@ func (l *List) Update(s *xorm.Session, a web.Auth) (err error) {
f.Title = l.Title f.Title = l.Title
f.Description = l.Description f.Description = l.Description
f.IsFavorite = l.IsFavorite
err = f.Update(s, a) err = f.Update(s, a)
if err != nil { if err != nil {
return err return err

View file

@ -548,7 +548,7 @@ func (n *Namespace) ReadAll(s *xorm.Session, a web.Auth, search string, page int
} }
///////////////// /////////////////
// Add list details (favorite state, among other things // Add list details (favorite state, among other things)
err = addListDetails(s, lists, a) err = addListDetails(s, lists, a)
if err != nil { if err != nil {
return return

View file

@ -102,6 +102,7 @@ func (sf *SavedFilter) toList() *List {
Created: sf.Created, Created: sf.Created,
Updated: sf.Updated, Updated: sf.Updated,
Owner: sf.Owner, Owner: sf.Owner,
NamespaceID: SavedFiltersPseudoNamespace.ID,
} }
} }

View file

@ -92,25 +92,46 @@ func TestSavedFilter_ReadOne(t *testing.T) {
} }
func TestSavedFilter_Update(t *testing.T) { func TestSavedFilter_Update(t *testing.T) {
db.LoadAndAssertFixtures(t) t.Run("normal", func(t *testing.T) {
s := db.NewSession() db.LoadAndAssertFixtures(t)
defer s.Close() s := db.NewSession()
defer s.Close()
sf := &SavedFilter{ sf := &SavedFilter{
ID: 1, ID: 1,
Title: "NewTitle", Title: "NewTitle",
Description: "", // Explicitly reset the description Description: "", // Explicitly reset the description
Filters: &TaskCollection{}, Filters: &TaskCollection{},
} }
err := sf.Update(s, &user.User{ID: 1}) err := sf.Update(s, &user.User{ID: 1})
assert.NoError(t, err) assert.NoError(t, err)
err = s.Commit() err = s.Commit()
assert.NoError(t, err) assert.NoError(t, err)
db.AssertExists(t, "saved_filters", map[string]interface{}{ db.AssertExists(t, "saved_filters", map[string]interface{}{
"id": 1, "id": 1,
"title": "NewTitle", "title": "NewTitle",
"description": "", "description": "",
}, false) }, false)
})
t.Run("make favorite", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
sf := &SavedFilter{
ID: 1,
IsFavorite: true,
Filters: &TaskCollection{},
}
err := sf.Update(s, &user.User{ID: 1})
assert.NoError(t, err)
err = s.Commit()
assert.NoError(t, err)
db.AssertExists(t, "saved_filters", map[string]interface{}{
"id": 1,
"is_favorite": true,
}, false)
})
} }
func TestSavedFilter_Delete(t *testing.T) { func TestSavedFilter_Delete(t *testing.T) {