mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-28 13:48:49 +08:00
NOISSUE - Fix group retrieval when parent id is not specified (#1247)
* fix group retrieval Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor for better readability Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
This commit is contained in:
parent
9ed5f8334f
commit
42e4e054c2
@ -153,16 +153,28 @@ func (gr groupRepository) RetrieveAllWithAncestors(ctx context.Context, groupID
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return users.GroupPage{}, errors.Wrap(errRetrieveDB, err)
|
return users.GroupPage{}, errors.Wrap(errRetrieveDB, err)
|
||||||
}
|
}
|
||||||
|
if mq != "" {
|
||||||
|
mq = fmt.Sprintf("WHERE %s", mq)
|
||||||
|
}
|
||||||
|
|
||||||
q := fmt.Sprintf(`WITH RECURSIVE subordinates AS (
|
cq := fmt.Sprintf("SELECT COUNT(*) FROM groups %s", mq)
|
||||||
SELECT id, owner_id, parent_id, name, description, metadata
|
sq := fmt.Sprintf("SELECT id, owner_id, parent_id, name, description, metadata FROM groups %s", mq)
|
||||||
FROM groups
|
q := fmt.Sprintf("%s ORDER BY id LIMIT :limit OFFSET :offset", sq)
|
||||||
WHERE id = :id
|
|
||||||
UNION
|
if groupID != "" {
|
||||||
SELECT groups.id, groups.owner_id, groups.parent_id, groups.name, groups.description, groups.metadata
|
sq = fmt.Sprintf(
|
||||||
FROM groups
|
`WITH RECURSIVE subordinates AS (
|
||||||
INNER JOIN subordinates s ON s.id = groups.parent_id %s
|
SELECT id, owner_id, parent_id, name, description, metadata
|
||||||
) SELECT * FROM subordinates ORDER BY id LIMIT :limit OFFSET :offset`, mq)
|
FROM groups
|
||||||
|
WHERE id = :id
|
||||||
|
UNION
|
||||||
|
SELECT groups.id, groups.owner_id, groups.parent_id, groups.name, groups.description, groups.metadata
|
||||||
|
FROM groups
|
||||||
|
INNER JOIN subordinates s ON s.id = groups.parent_id %s
|
||||||
|
)`, mq)
|
||||||
|
q = fmt.Sprintf("%s SELECT * FROM subordinates ORDER BY id LIMIT :limit OFFSET :offset", sq)
|
||||||
|
cq = fmt.Sprintf("%s SELECT COUNT(*) FROM subordinates", sq)
|
||||||
|
}
|
||||||
|
|
||||||
dbPage, err := toDBGroupPage("", groupID, offset, limit, gm)
|
dbPage, err := toDBGroupPage("", groupID, offset, limit, gm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -173,7 +185,6 @@ func (gr groupRepository) RetrieveAllWithAncestors(ctx context.Context, groupID
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return users.GroupPage{}, errors.Wrap(errSelectDb, err)
|
return users.GroupPage{}, errors.Wrap(errSelectDb, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
var items []users.Group
|
var items []users.Group
|
||||||
@ -189,16 +200,6 @@ func (gr groupRepository) RetrieveAllWithAncestors(ctx context.Context, groupID
|
|||||||
items = append(items, gr)
|
items = append(items, gr)
|
||||||
}
|
}
|
||||||
|
|
||||||
cq := fmt.Sprintf(`WITH RECURSIVE subordinates AS (
|
|
||||||
SELECT id, owner_id, parent_id, name, description, metadata
|
|
||||||
FROM groups
|
|
||||||
WHERE id = :id
|
|
||||||
UNION
|
|
||||||
SELECT groups.id, groups.owner_id, groups.parent_id, groups.name, groups.description, groups.metadata
|
|
||||||
FROM groups
|
|
||||||
INNER JOIN subordinates s ON s.id = groups.parent_id %s
|
|
||||||
) SELECT COUNT(*) FROM subordinates`, mq)
|
|
||||||
|
|
||||||
total, err := total(ctx, gr.db, cq, dbPage)
|
total, err := total(ctx, gr.db, cq, dbPage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return users.GroupPage{}, errors.Wrap(errSelectDb, err)
|
return users.GroupPage{}, errors.Wrap(errSelectDb, err)
|
||||||
@ -222,6 +223,9 @@ func (gr groupRepository) Memberships(ctx context.Context, userID string, offset
|
|||||||
return users.GroupPage{}, errors.Wrap(errRetrieveDB, err)
|
return users.GroupPage{}, errors.Wrap(errRetrieveDB, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if mq != "" {
|
||||||
|
mq = fmt.Sprintf("AND %s", mq)
|
||||||
|
}
|
||||||
q := fmt.Sprintf(`SELECT g.id, g.owner_id, g.parent_id, g.name, g.description, g.metadata
|
q := fmt.Sprintf(`SELECT g.id, g.owner_id, g.parent_id, g.name, g.description, g.metadata
|
||||||
FROM group_relations gr, groups g
|
FROM group_relations gr, groups g
|
||||||
WHERE gr.group_id = g.id and gr.user_id = :userID
|
WHERE gr.group_id = g.id and gr.user_id = :userID
|
||||||
@ -424,7 +428,7 @@ func getGroupsMetadataQuery(m users.Metadata) ([]byte, string, error) {
|
|||||||
mq := ""
|
mq := ""
|
||||||
mb := []byte("{}")
|
mb := []byte("{}")
|
||||||
if len(m) > 0 {
|
if len(m) > 0 {
|
||||||
mq = ` AND groups.metadata @> :metadata`
|
mq = `groups.metadata @> :metadata`
|
||||||
|
|
||||||
b, err := json.Marshal(m)
|
b, err := json.Marshal(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user