Enable searching namespaces by their ids
continuous-integration/drone/pr Build was killed
Details
continuous-integration/drone/pr Build was killed
Details
Signed-off-by: kolaente <k@knt.li>
This commit is contained in:
parent
0c138a6ff5
commit
09498bb343
|
@ -17,7 +17,10 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"code.vikunja.io/api/pkg/log"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.vikunja.io/api/pkg/metrics"
|
"code.vikunja.io/api/pkg/metrics"
|
||||||
|
@ -51,6 +54,9 @@ type Namespace struct {
|
||||||
// A timestamp when this namespace was last updated. You cannot change this value.
|
// A timestamp when this namespace was last updated. You cannot change this value.
|
||||||
Updated time.Time `xorm:"updated not null" json:"updated"`
|
Updated time.Time `xorm:"updated not null" json:"updated"`
|
||||||
|
|
||||||
|
// If set to true, will only return the namespaces, not their lists.
|
||||||
|
NamespacesOnly bool `xorm:"-" json:"-" query:"namespaces_only"`
|
||||||
|
|
||||||
web.CRUDable `xorm:"-" json:"-"`
|
web.CRUDable `xorm:"-" json:"-"`
|
||||||
web.Rights `xorm:"-" json:"-"`
|
web.Rights `xorm:"-" json:"-"`
|
||||||
}
|
}
|
||||||
|
@ -181,6 +187,7 @@ type NamespaceWithLists struct {
|
||||||
// @Param per_page query int false "The maximum number of items per page. Note this parameter is limited by the configured maximum of items per page."
|
// @Param per_page query int false "The maximum number of items per page. Note this parameter is limited by the configured maximum of items per page."
|
||||||
// @Param s query string false "Search namespaces by name."
|
// @Param s query string false "Search namespaces by name."
|
||||||
// @Param is_archived query bool false "If true, also returns all archived namespaces."
|
// @Param is_archived query bool false "If true, also returns all archived namespaces."
|
||||||
|
// @Param namespaces_only query bool false "If true, also returns only namespaces without their lists."
|
||||||
// @Security JWTKeyAuth
|
// @Security JWTKeyAuth
|
||||||
// @Success 200 {array} models.NamespaceWithLists "The Namespaces."
|
// @Success 200 {array} models.NamespaceWithLists "The Namespaces."
|
||||||
// @Failure 500 {object} models.Message "Internal error"
|
// @Failure 500 {object} models.Message "Internal error"
|
||||||
|
@ -210,6 +217,24 @@ func (n *Namespace) ReadAll(a web.Auth, search string, page int, perPage int) (r
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var filterCond builder.Cond
|
||||||
|
vals := strings.Split(search, ",")
|
||||||
|
ids := []int64{}
|
||||||
|
for _, val := range vals {
|
||||||
|
v, err := strconv.ParseInt(val, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Debugf("Namespace search string part '%s' is not a number: %s", val, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ids = append(ids, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ids) > 0 {
|
||||||
|
filterCond = builder.In("namespaces.id", ids)
|
||||||
|
} else {
|
||||||
|
filterCond = &builder.Like{"namespaces.title", "%" + search + "%"}
|
||||||
|
}
|
||||||
|
|
||||||
limit, start := getLimitFromPageIndex(page, perPage)
|
limit, start := getLimitFromPageIndex(page, perPage)
|
||||||
query := x.Select("namespaces.*").
|
query := x.Select("namespaces.*").
|
||||||
Table("namespaces").
|
Table("namespaces").
|
||||||
|
@ -220,7 +245,7 @@ func (n *Namespace) ReadAll(a web.Auth, search string, page int, perPage int) (r
|
||||||
Or("namespaces.owner_id = ?", doer.ID).
|
Or("namespaces.owner_id = ?", doer.ID).
|
||||||
Or("users_namespace.user_id = ?", doer.ID).
|
Or("users_namespace.user_id = ?", doer.ID).
|
||||||
GroupBy("namespaces.id").
|
GroupBy("namespaces.id").
|
||||||
Where("namespaces.title LIKE ?", "%"+search+"%").
|
Where(filterCond).
|
||||||
Where(isArchivedCond)
|
Where(isArchivedCond)
|
||||||
if limit > 0 {
|
if limit > 0 {
|
||||||
query = query.Limit(limit, start)
|
query = query.Limit(limit, start)
|
||||||
|
@ -230,6 +255,22 @@ func (n *Namespace) ReadAll(a web.Auth, search string, page int, perPage int) (r
|
||||||
return nil, 0, 0, err
|
return nil, 0, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
numberOfTotalItems, err = x.
|
||||||
|
Table("namespaces").
|
||||||
|
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
|
||||||
|
Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id").
|
||||||
|
Join("LEFT", "users_namespace", "users_namespace.namespace_id = namespaces.id").
|
||||||
|
Where("team_members.user_id = ?", doer.ID).
|
||||||
|
Or("namespaces.owner_id = ?", doer.ID).
|
||||||
|
Or("users_namespace.user_id = ?", doer.ID).
|
||||||
|
And("namespaces.is_archived = false").
|
||||||
|
GroupBy("namespaces.id").
|
||||||
|
Where(filterCond).
|
||||||
|
Count(&NamespaceWithLists{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
// Make a list of namespace ids
|
// Make a list of namespace ids
|
||||||
var namespaceids []int64
|
var namespaceids []int64
|
||||||
var userIDs []int64
|
var userIDs []int64
|
||||||
|
@ -245,6 +286,21 @@ func (n *Namespace) ReadAll(a web.Auth, search string, page int, perPage int) (r
|
||||||
return nil, 0, 0, err
|
return nil, 0, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//////////////////////
|
||||||
|
// Put it all together (and sort it)
|
||||||
|
all := make([]*NamespaceWithLists, 0, len(namespaces))
|
||||||
|
for _, n := range namespaces {
|
||||||
|
n.Owner = userMap[n.OwnerID]
|
||||||
|
all = append(all, n)
|
||||||
|
}
|
||||||
|
sort.Slice(all, func(i, j int) bool {
|
||||||
|
return all[i].ID < all[j].ID
|
||||||
|
})
|
||||||
|
|
||||||
|
if n.NamespacesOnly {
|
||||||
|
return all, len(all), numberOfTotalItems, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Get all lists
|
// Get all lists
|
||||||
lists := []*List{}
|
lists := []*List{}
|
||||||
listQuery := x.
|
listQuery := x.
|
||||||
|
@ -258,22 +314,6 @@ func (n *Namespace) ReadAll(a web.Auth, search string, page int, perPage int) (r
|
||||||
return nil, 0, 0, err
|
return nil, 0, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
numberOfTotalItems, err = x.
|
|
||||||
Table("namespaces").
|
|
||||||
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
|
|
||||||
Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id").
|
|
||||||
Join("LEFT", "users_namespace", "users_namespace.namespace_id = namespaces.id").
|
|
||||||
Where("team_members.user_id = ?", doer.ID).
|
|
||||||
Or("namespaces.owner_id = ?", doer.ID).
|
|
||||||
Or("users_namespace.user_id = ?", doer.ID).
|
|
||||||
And("namespaces.is_archived = false").
|
|
||||||
GroupBy("namespaces.id").
|
|
||||||
Where("namespaces.title LIKE ?", "%"+search+"%").
|
|
||||||
Count(&NamespaceWithLists{})
|
|
||||||
if err != nil {
|
|
||||||
return nil, 0, 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////
|
///////////////
|
||||||
// Shared Lists
|
// Shared Lists
|
||||||
|
|
||||||
|
@ -395,17 +435,6 @@ func (n *Namespace) ReadAll(a web.Auth, search string, page int, perPage int) (r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////
|
|
||||||
// Put it all together (and sort it)
|
|
||||||
all := make([]*NamespaceWithLists, 0, len(namespaces))
|
|
||||||
for _, n := range namespaces {
|
|
||||||
n.Owner = userMap[n.OwnerID]
|
|
||||||
all = append(all, n)
|
|
||||||
}
|
|
||||||
sort.Slice(all, func(i, j int) bool {
|
|
||||||
return all[i].ID < all[j].ID
|
|
||||||
})
|
|
||||||
|
|
||||||
return all, len(all), numberOfTotalItems, nil
|
return all, len(all), numberOfTotalItems, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue