1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +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:
Mirko Teodorovic 2020-10-01 17:03:35 +02:00 committed by GitHub
parent 9ed5f8334f
commit 42e4e054c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -153,16 +153,28 @@ func (gr groupRepository) RetrieveAllWithAncestors(ctx context.Context, groupID
if err != nil {
return users.GroupPage{}, errors.Wrap(errRetrieveDB, err)
}
if mq != "" {
mq = fmt.Sprintf("WHERE %s", mq)
}
q := 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 * FROM subordinates ORDER BY id LIMIT :limit OFFSET :offset`, mq)
cq := fmt.Sprintf("SELECT COUNT(*) FROM groups %s", mq)
sq := fmt.Sprintf("SELECT id, owner_id, parent_id, name, description, metadata FROM groups %s", mq)
q := fmt.Sprintf("%s ORDER BY id LIMIT :limit OFFSET :offset", sq)
if groupID != "" {
sq = 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
)`, 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)
if err != nil {
@ -173,7 +185,6 @@ func (gr groupRepository) RetrieveAllWithAncestors(ctx context.Context, groupID
if err != nil {
return users.GroupPage{}, errors.Wrap(errSelectDb, err)
}
defer rows.Close()
var items []users.Group
@ -189,16 +200,6 @@ func (gr groupRepository) RetrieveAllWithAncestors(ctx context.Context, groupID
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)
if err != nil {
return users.GroupPage{}, errors.Wrap(errSelectDb, err)
@ -222,9 +223,12 @@ func (gr groupRepository) Memberships(ctx context.Context, userID string, offset
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
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
%s ORDER BY id LIMIT :limit OFFSET :offset;`, mq)
params := map[string]interface{}{
@ -424,7 +428,7 @@ func getGroupsMetadataQuery(m users.Metadata) ([]byte, string, error) {
mq := ""
mb := []byte("{}")
if len(m) > 0 {
mq = ` AND groups.metadata @> :metadata`
mq = `groups.metadata @> :metadata`
b, err := json.Marshal(m)
if err != nil {