Exlicitly get the pseudonamespace with all shared lists (#32)

This commit is contained in:
konrad 2018-12-04 10:16:42 +00:00 committed by Gitea
parent f1fca3346b
commit 0c1d786ade
7 changed files with 55 additions and 16 deletions

View file

@ -4,7 +4,7 @@ Content-Type: application/json
{ {
"username": "user", "username": "user",
"password": "12345" "password": "1234"
} }
> {% client.global.set("auth_token", response.body.token); %} > {% client.global.set("auth_token", response.body.token); %}

View file

@ -1,5 +1,5 @@
# Get all lists # Get all lists
GET http://localhost:8080/api/v1/lists GET http://localhost:8080/api/v1/namespaces/1/lists
Authorization: Bearer {{auth_token}} Authorization: Bearer {{auth_token}}
### ###

View file

@ -39,8 +39,27 @@ type List struct {
} }
// GetListsByNamespaceID gets all lists in a namespace // GetListsByNamespaceID gets all lists in a namespace
func GetListsByNamespaceID(nID int64) (lists []*List, err error) { func GetListsByNamespaceID(nID int64, doer *User) (lists []*List, err error) {
err = x.Where("namespace_id = ?", nID).Find(&lists) 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 return lists, err
} }

View file

@ -27,7 +27,7 @@ func TestList_ReadAll(t *testing.T) {
//assert.NoError(t, PrepareTestDatabase()) //assert.NoError(t, PrepareTestDatabase())
// Get all lists for our namespace // Get all lists for our namespace
lists, err := GetListsByNamespaceID(1) lists, err := GetListsByNamespaceID(1, &User{})
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, len(lists), 2) assert.Equal(t, len(lists), 2)

View file

@ -37,6 +37,15 @@ type Namespace struct {
web.Rights `xorm:"-" json:"-"` 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 // TableName makes beautiful table names
func (Namespace) TableName() string { func (Namespace) TableName() string {
return "namespaces" return "namespaces"
@ -44,11 +53,17 @@ func (Namespace) TableName() string {
// GetNamespaceByID returns a namespace object by its ID // GetNamespaceByID returns a namespace object by its ID
func GetNamespaceByID(id int64) (namespace Namespace, err error) { func GetNamespaceByID(id int64) (namespace Namespace, err error) {
if id < 1 { if id == 0 {
return namespace, ErrNamespaceDoesNotExist{ID: id} return namespace, ErrNamespaceDoesNotExist{ID: id}
} }
namespace.ID = id namespace.ID = id
// Get the namesapce with shared lists
if id == -1 {
namespace = PseudoNamespace
return namespace, err
}
exists, err := x.Get(&namespace) exists, err := x.Get(&namespace)
if err != nil { if err != nil {
return namespace, err 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 // Create our pseudo-namespace to hold the shared lists
// We want this one at the beginning, which is why we create it here // We want this one at the beginning, which is why we create it here
pseudonamespace := PseudoNamespace
pseudonamespace.Owner = *doer
all = append(all, &NamespaceWithLists{ all = append(all, &NamespaceWithLists{
Namespace{ pseudonamespace,
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(),
},
[]*List{}, []*List{},
}) })

View file

@ -45,7 +45,7 @@ func (n *Namespace) Delete() (err error) {
} }
// Delete all lists with their tasks // Delete all lists with their tasks
lists, err := GetListsByNamespaceID(n.ID) lists, err := GetListsByNamespaceID(n.ID, &User{})
var listIDs []int64 var listIDs []int64
// We need to do that for here because we need the list ids to delete two times: // We need to do that for here because we need the list ids to delete two times:
// 1) to delete the lists itself // 1) to delete the lists itself

View file

@ -51,7 +51,12 @@ func GetListsByNamespaceID(c echo.Context) error {
} }
// Get the lists // 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 err != nil {
if models.IsErrNamespaceDoesNotExist(err) { if models.IsErrNamespaceDoesNotExist(err) {
return c.JSON(http.StatusNotFound, models.Message{"Namespace not found."}) 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 return
} }
if namespaceID == -1 {
namespace = models.PseudoNamespace
return
}
// Get the namespace // Get the namespace
namespace, err = models.GetNamespaceByID(namespaceID) namespace, err = models.GetNamespaceByID(namespaceID)
if err != nil { if err != nil {