From 0c1d786adeb8f009de4917813fa90838957a0f29 Mon Sep 17 00:00:00 2001 From: konrad Date: Tue, 4 Dec 2018 10:16:42 +0000 Subject: [PATCH] Exlicitly get the pseudonamespace with all shared lists (#32) --- REST-Tests/auth.http | 2 +- REST-Tests/lists.http | 2 +- pkg/models/list.go | 23 +++++++++++++++++++-- pkg/models/list_read_test.go | 2 +- pkg/models/namespace.go | 28 +++++++++++++++++--------- pkg/models/namespace_delete.go | 2 +- pkg/routes/api/v1/list_by_namespace.go | 12 ++++++++++- 7 files changed, 55 insertions(+), 16 deletions(-) diff --git a/REST-Tests/auth.http b/REST-Tests/auth.http index 2744c0dee8..5c7e342816 100644 --- a/REST-Tests/auth.http +++ b/REST-Tests/auth.http @@ -4,7 +4,7 @@ Content-Type: application/json { "username": "user", - "password": "12345" + "password": "1234" } > {% client.global.set("auth_token", response.body.token); %} diff --git a/REST-Tests/lists.http b/REST-Tests/lists.http index e1703af1fc..aa0ee4f036 100644 --- a/REST-Tests/lists.http +++ b/REST-Tests/lists.http @@ -1,5 +1,5 @@ # Get all lists -GET http://localhost:8080/api/v1/lists +GET http://localhost:8080/api/v1/namespaces/1/lists Authorization: Bearer {{auth_token}} ### diff --git a/pkg/models/list.go b/pkg/models/list.go index 9b315e6a74..5cc7dde0fe 100644 --- a/pkg/models/list.go +++ b/pkg/models/list.go @@ -39,8 +39,27 @@ type List struct { } // GetListsByNamespaceID gets all lists in a namespace -func GetListsByNamespaceID(nID int64) (lists []*List, err error) { - err = x.Where("namespace_id = ?", nID).Find(&lists) +func GetListsByNamespaceID(nID int64, doer *User) (lists []*List, err error) { + if nID == -1 { + err = x.Select("l.*"). + Table("list"). + Alias("l"). + Join("LEFT", []string{"team_list", "tl"}, "l.id = tl.list_id"). + Join("LEFT", []string{"team_members", "tm"}, "tm.team_id = tl.team_id"). + Join("LEFT", []string{"users_list", "ul"}, "ul.list_id = l.id"). + Where("tm.user_id = ?", doer.ID). + Or("ul.user_id = ?", doer.ID). + GroupBy("l.id"). + Find(&lists) + if err != nil { + return nil, err + } + } else { + err = x.Where("namespace_id = ?", nID).Find(&lists) + } + + // get more list details + err = AddListDetails(lists) return lists, err } diff --git a/pkg/models/list_read_test.go b/pkg/models/list_read_test.go index 00662b6c11..c0f7b2ccd4 100644 --- a/pkg/models/list_read_test.go +++ b/pkg/models/list_read_test.go @@ -27,7 +27,7 @@ func TestList_ReadAll(t *testing.T) { //assert.NoError(t, PrepareTestDatabase()) // Get all lists for our namespace - lists, err := GetListsByNamespaceID(1) + lists, err := GetListsByNamespaceID(1, &User{}) assert.NoError(t, err) assert.Equal(t, len(lists), 2) diff --git a/pkg/models/namespace.go b/pkg/models/namespace.go index 86be4d3942..993abc1ca0 100644 --- a/pkg/models/namespace.go +++ b/pkg/models/namespace.go @@ -37,6 +37,15 @@ type Namespace struct { web.Rights `xorm:"-" json:"-"` } +// PseudoNamespace is a pseudo namespace used to hold shared lists +var PseudoNamespace = Namespace{ + ID: -1, + Name: "Shared Lists", + Description: "Lists of other users shared with you via teams or directly.", + Created: time.Now().Unix(), + Updated: time.Now().Unix(), +} + // TableName makes beautiful table names func (Namespace) TableName() string { return "namespaces" @@ -44,11 +53,17 @@ func (Namespace) TableName() string { // GetNamespaceByID returns a namespace object by its ID func GetNamespaceByID(id int64) (namespace Namespace, err error) { - if id < 1 { + if id == 0 { return namespace, ErrNamespaceDoesNotExist{ID: id} } namespace.ID = id + // Get the namesapce with shared lists + if id == -1 { + namespace = PseudoNamespace + return namespace, err + } + exists, err := x.Get(&namespace) if err != nil { return namespace, err @@ -112,15 +127,10 @@ func (n *Namespace) ReadAll(search string, a web.Auth, page int) (interface{}, e // Create our pseudo-namespace to hold the shared lists // We want this one at the beginning, which is why we create it here + pseudonamespace := PseudoNamespace + pseudonamespace.Owner = *doer all = append(all, &NamespaceWithLists{ - Namespace{ - ID: -1, - Name: "Shared Lists", - Description: "Lists of other users shared with you via teams or directly.", - Owner: *doer, - Created: time.Now().Unix(), - Updated: time.Now().Unix(), - }, + pseudonamespace, []*List{}, }) diff --git a/pkg/models/namespace_delete.go b/pkg/models/namespace_delete.go index cb31974f16..a8746f5225 100644 --- a/pkg/models/namespace_delete.go +++ b/pkg/models/namespace_delete.go @@ -45,7 +45,7 @@ func (n *Namespace) Delete() (err error) { } // Delete all lists with their tasks - lists, err := GetListsByNamespaceID(n.ID) + lists, err := GetListsByNamespaceID(n.ID, &User{}) var listIDs []int64 // We need to do that for here because we need the list ids to delete two times: // 1) to delete the lists itself diff --git a/pkg/routes/api/v1/list_by_namespace.go b/pkg/routes/api/v1/list_by_namespace.go index dfeda22bd5..995e75fe8b 100644 --- a/pkg/routes/api/v1/list_by_namespace.go +++ b/pkg/routes/api/v1/list_by_namespace.go @@ -51,7 +51,12 @@ func GetListsByNamespaceID(c echo.Context) error { } // Get the lists - lists, err := models.GetListsByNamespaceID(namespace.ID) + doer, err := models.GetCurrentUser(c) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"An error occurred."}) + } + + lists, err := models.GetListsByNamespaceID(namespace.ID, doer) if err != nil { if models.IsErrNamespaceDoesNotExist(err) { return c.JSON(http.StatusNotFound, models.Message{"Namespace not found."}) @@ -70,6 +75,11 @@ func getNamespace(c echo.Context) (namespace models.Namespace, err error) { return } + if namespaceID == -1 { + namespace = models.PseudoNamespace + return + } + // Get the namespace namespace, err = models.GetNamespaceByID(namespaceID) if err != nil {